From 65335cb4b25650f7025b689ae033abf11528f2f4 Mon Sep 17 00:00:00 2001 From: kim Date: Thu, 21 Nov 2024 17:56:36 +0000 Subject: [PATCH] ensure a default value is always set --- .../20241121121623_enum_strings_to_ints.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/internal/db/bundb/migrations/20241121121623_enum_strings_to_ints.go b/internal/db/bundb/migrations/20241121121623_enum_strings_to_ints.go index 4375e5268..6f3d96718 100644 --- a/internal/db/bundb/migrations/20241121121623_enum_strings_to_ints.go +++ b/internal/db/bundb/migrations/20241121121623_enum_strings_to_ints.go @@ -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 }