From 1153576f7c453f293a7d13cccda6f91293757248 Mon Sep 17 00:00:00 2001 From: Andrew Farries Date: Mon, 15 Jan 2024 09:28:35 +0000 Subject: [PATCH] Preserve default value when duplicating a column --- pkg/migrations/duplicate.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkg/migrations/duplicate.go b/pkg/migrations/duplicate.go index d35b3f80..39c0af4f 100644 --- a/pkg/migrations/duplicate.go +++ b/pkg/migrations/duplicate.go @@ -42,14 +42,22 @@ func (d *Duplicator) WithType(t string) *Duplicator { func (d *Duplicator) Duplicate(ctx context.Context) error { const ( cAlterTableSQL = `ALTER TABLE %s ADD COLUMN %s %s` + cSetDefaultSQL = `ALTER COLUMN %s SET DEFAULT %s` cAddForeignKeySQL = `ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)` ) + // Generate SQL to duplicate the column's name and type sql := fmt.Sprintf(cAlterTableSQL, pq.QuoteIdentifier(d.table.Name), pq.QuoteIdentifier(d.asName), d.withType) + // Generate SQL to duplicate the column's default value + if d.column.Default != nil { + sql += fmt.Sprintf(", "+cSetDefaultSQL, d.asName, *d.column.Default) + } + + // Generate SQL to duplicate any foreign key constraints on the column for _, fk := range d.table.ForeignKeys { if slices.Contains(fk.Columns, d.column.Name) { sql += fmt.Sprintf(", "+cAddForeignKeySQL,