mirror of
1
Fork 0

ensure a default value is always set

This commit is contained in:
kim 2024-11-21 17:56:36 +00:00
parent 25a4758557
commit 65335cb4b2
1 changed files with 7 additions and 10 deletions

View File

@ -146,21 +146,18 @@ func convertEnums[OldType ~string, NewType ~int](
table, column,
)
var columnExpr string
var columnArgs []any
// Build new column expr with args.
columnExpr = "? INTEGER NOT NULL"
columnArgs = []any{bun.Ident(newColumn)}
if defaultValue != nil {
columnExpr += " DEFAULT ?"
columnArgs = append(columnArgs, *defaultValue)
// Ensure a default value.
if defaultValue == nil {
var zero NewType
defaultValue = &zero
}
// Add new column to database.
if _, err := tx.NewAddColumn().
Table(table).
ColumnExpr(columnExpr, columnArgs...).
ColumnExpr("? INTEGER NOT NULL DEFAULT ?",
bun.Ident(newColumn),
*defaultValue).
Exec(ctx); err != nil {
return err
}