yt-gen-app/lib/templ/tmpl/backend/db/migrate.tmpl
Ymnuk 972bc84701
All checks were successful
continuous-integration/drone/tag Build is passing
Добавление функций обновления справочников
2023-08-15 13:28:01 +03:00

44 lines
989 B
Cheetah

package db
import (
"{{.Name}}/db/model"
uuid "github.com/satori/go.uuid"
)
{{ range $index, $item := .DB.Tables }}
{{ if $item.Values }}
func update{{ fieldNamePrepare $item.Name}}() {
records:=[]model.{{ fieldNamePrepare $item.Name }} {
{{ range $i, $field := $item.Values }}
{
{{ range $name, $value := $field }}
{{ if eq $name "id" }}
Base: model.Base{
ID: uuid.FromStringOrNil("{{ $value }}"),
},
{{ end }}
{{ if ne $name "id" }}
{{ fieldNamePrepare $name }}: &[]string{"{{ $value }}"}[0],
{{ end }}
{{ end }}
},
{{ end }}
}
var err error
tx := BeginTransation()
defer func() {
EndTransaction(tx, err)
}()
tx.Delete(&model.{{ fieldNamePrepare $item.Name}}{}, "1 = 1")
if res := tx.Create(&records); res.RowsAffected == 0 {
panic(err)
}
}
{{ end }}
{{ end }}