Skip to content

Commit

Permalink
Preserve default on column duplication when changing column type (#234)
Browse files Browse the repository at this point in the history
When duplicating a column for backfilling to change a column's type,
ensure that any default on the column is preserved on the duplicated
column.

This fixes the issue where changing type would lose any `DEFAULT`
defined on the column

This is part of a larger class of issues where duplicated columns are
not faithfully preserving all properties of the original, tracked in
#227.
  • Loading branch information
andrew-farries committed Jan 16, 2024
1 parent fc7370f commit d803689
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/migrations/duplicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
65 changes: 65 additions & 0 deletions pkg/migrations/op_change_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,71 @@ func TestChangeColumnType(t *testing.T) {
ConstraintMustExist(t, db, "public", "employees", "fk_employee_department")
},
},
{
name: "changing column type preserves any defaults on the column",
migrations: []migrations.Migration{
{
Name: "01_add_table",
Operations: migrations.Operations{
&migrations.OpCreateTable{
Name: "users",
Columns: []migrations.Column{
{
Name: "id",
Type: "integer",
Pk: true,
},
{
Name: "username",
Type: "text",
Default: ptr("'alice'"),
Nullable: true,
},
},
},
},
},
{
Name: "02_change_type",
Operations: migrations.Operations{
&migrations.OpAlterColumn{
Table: "users",
Column: "username",
Type: "varchar(255)",
Up: "username",
Down: "username",
},
},
},
},
afterStart: func(t *testing.T, db *sql.DB) {
// A row can be inserted into the new version of the table.
MustInsert(t, db, "public", "02_change_type", "users", map[string]string{
"id": "1",
})

// The newly inserted row respects the default value of the column.
rows := MustSelect(t, db, "public", "02_change_type", "users")
assert.Equal(t, []map[string]any{
{"id": 1, "username": "alice"},
}, rows)
},
afterRollback: func(t *testing.T, db *sql.DB) {
},
afterComplete: func(t *testing.T, db *sql.DB) {
// A row can be inserted into the new version of the table.
MustInsert(t, db, "public", "02_change_type", "users", map[string]string{
"id": "2",
})

// The newly inserted row respects the default value of the column.
rows := MustSelect(t, db, "public", "02_change_type", "users")
assert.Equal(t, []map[string]any{
{"id": 1, "username": "alice"},
{"id": 2, "username": "alice"},
}, rows)
},
},
})
}

Expand Down

0 comments on commit d803689

Please sign in to comment.