Skip to content

Commit

Permalink
Updte test expectations for changing column type
Browse files Browse the repository at this point in the history
Previously, we tested that changing a column type preserved the default
and check constraints on the column. We now want to reverse this
decision and test that they are *not* preserved.
  • Loading branch information
andrew-farries committed May 3, 2024
1 parent 6a9a2ae commit 081d516
Showing 1 changed file with 32 additions and 30 deletions.
62 changes: 32 additions & 30 deletions pkg/migrations/op_change_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func TestChangeColumnType(t *testing.T) {
},
},
{
name: "changing column type preserves any defaults on the column",
name: "changing column type removes any default on the column",
migrations: []migrations.Migration{
{
Name: "01_add_table",
Expand All @@ -239,9 +239,9 @@ func TestChangeColumnType(t *testing.T) {
Pk: ptr(true),
},
{
Name: "username",
Name: "age",
Type: "text",
Default: ptr("'alice'"),
Default: ptr("'0'"),
Nullable: ptr(true),
},
},
Expand All @@ -253,10 +253,10 @@ func TestChangeColumnType(t *testing.T) {
Operations: migrations.Operations{
&migrations.OpAlterColumn{
Table: "users",
Column: "username",
Type: ptr("varchar(255)"),
Up: "username",
Down: "username",
Column: "age",
Type: ptr("integer"),
Up: "CAST(age AS integer)",
Down: "CAST(age AS text)",
},
},
},
Expand All @@ -267,10 +267,10 @@ func TestChangeColumnType(t *testing.T) {
"id": "1",
})

// The newly inserted row respects the default value of the column.
// The newly inserted row contains a NULL instead of the old default value.
rows := MustSelect(t, db, schema, "02_change_type", "users")
assert.Equal(t, []map[string]any{
{"id": 1, "username": "alice"},
{"id": 1, "age": nil},
}, rows)
},
afterRollback: func(t *testing.T, db *sql.DB, schema string) {
Expand All @@ -281,16 +281,16 @@ func TestChangeColumnType(t *testing.T) {
"id": "2",
})

// The newly inserted row respects the default value of the column.
// The newly inserted row contains a NULL instead of the old default value.
rows := MustSelect(t, db, schema, "02_change_type", "users")
assert.Equal(t, []map[string]any{
{"id": 1, "username": "alice"},
{"id": 2, "username": "alice"},
{"id": 1, "age": nil},
{"id": 2, "age": nil},
}, rows)
},
},
{
name: "changing column type preserves any check constraints on the column",
name: "changing column type removes any check constraints on the column",
migrations: []migrations.Migration{
{
Name: "01_add_table",
Expand All @@ -304,12 +304,12 @@ func TestChangeColumnType(t *testing.T) {
Pk: ptr(true),
},
{
Name: "username",
Name: "age",
Type: "text",
Nullable: ptr(true),
Check: &migrations.CheckConstraint{
Name: "username_length",
Constraint: "length(username) > 3",
Name: "age_length",
Constraint: "length(age) < 3",
},
},
},
Expand All @@ -321,29 +321,31 @@ func TestChangeColumnType(t *testing.T) {
Operations: migrations.Operations{
&migrations.OpAlterColumn{
Table: "users",
Column: "username",
Type: ptr("varchar(255)"),
Up: "username",
Down: "username",
Column: "age",
Type: ptr("integer"),
Up: "CAST(age AS integer)",
Down: "(SELECT CASE WHEN age < 100 THEN age::text ELSE '0' END)",
},
},
},
},
afterStart: func(t *testing.T, db *sql.DB, schema string) {
// Inserting a row that violates the check constraint should fail.
MustNotInsert(t, db, schema, "02_change_type", "users", map[string]string{
"id": "1",
"username": "a",
}, testutils.CheckViolationErrorCode)
// Inserting a row into the new schema that violates the check
// constraint on the old schema should succeed.
MustInsert(t, db, schema, "02_change_type", "users", map[string]string{
"id": "1",
"age": "1111",
})
},
afterRollback: func(t *testing.T, db *sql.DB, schema string) {
},
afterComplete: func(t *testing.T, db *sql.DB, schema string) {
// Inserting a row that violates the check constraint should fail.
MustNotInsert(t, db, schema, "02_change_type", "users", map[string]string{
"id": "2",
"username": "b",
}, testutils.CheckViolationErrorCode)
// Inserting a row into the new schema that violates the check
// constraint on the old schema should succeed.
MustInsert(t, db, schema, "02_change_type", "users", map[string]string{
"id": "2",
"age": "2222",
})
},
},
{
Expand Down

0 comments on commit 081d516

Please sign in to comment.