From b6cf79d93a06407fe7686dc7a2572a80a4c92848 Mon Sep 17 00:00:00 2001 From: Andrew Farries Date: Mon, 15 Jan 2024 11:53:17 +0000 Subject: [PATCH] Add test: ensure default preserved on type change Check that changing a column's type preserves a default value defined on the column. --- pkg/migrations/op_change_type_test.go | 65 +++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/pkg/migrations/op_change_type_test.go b/pkg/migrations/op_change_type_test.go index b8208048f..cea81bbcd 100644 --- a/pkg/migrations/op_change_type_test.go +++ b/pkg/migrations/op_change_type_test.go @@ -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) + }, + }, }) }