Skip to content

Commit

Permalink
Add test: ensure default preserved on type change
Browse files Browse the repository at this point in the history
Check that changing a column's type preserves a default value defined on
the column.
  • Loading branch information
andrew-farries committed Jan 15, 2024
1 parent 6a79ca0 commit b6cf79d
Showing 1 changed file with 65 additions and 0 deletions.
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 b6cf79d

Please sign in to comment.