Skip to content

Add support for alter_table change type operations in multi-operation migrations #661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 208 additions & 0 deletions pkg/migrations/op_change_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,214 @@ func TestChangeColumnType(t *testing.T) {
})
}

func TestChangeTypeInMultiOperationMigrations(t *testing.T) {
t.Parallel()

ExecuteTests(t, TestCases{
{
name: "rename table, change type",
migrations: []migrations.Migration{
{
Name: "01_create_table",
Operations: migrations.Operations{
&migrations.OpCreateTable{
Name: "items",
Columns: []migrations.Column{
{
Name: "id",
Type: "int",
Pk: true,
},
{
Name: "name",
Type: "varchar(3)",
Nullable: true,
},
},
},
},
},
{
Name: "02_multi_operation",
Operations: migrations.Operations{
&migrations.OpRenameTable{
From: "items",
To: "products",
},
&migrations.OpAlterColumn{
Table: "products",
Column: "name",
Type: ptr("text"),
Up: "name",
Down: "CASE WHEN LENGTH(name) > 3 THEN 'xxx' ELSE name END",
},
},
},
},
afterStart: func(t *testing.T, db *sql.DB, schema string) {
// The new column has the expected type
// The table hasn't been physically renamed yet, so we need to use the old name
ColumnMustHaveType(t, db, schema, "items", migrations.TemporaryName("name"), "text")

// Can insert a row into the new schema
MustInsert(t, db, schema, "02_multi_operation", "products", map[string]string{
"id": "1",
"name": "apple",
})

// Can insert a row into the old schema
MustInsert(t, db, schema, "01_create_table", "items", map[string]string{
"id": "2",
"name": "abc",
})

// The new view has the expected rows
rows := MustSelect(t, db, schema, "02_multi_operation", "products")
assert.Equal(t, []map[string]any{
{"id": 1, "name": "apple"},
{"id": 2, "name": "abc"},
}, rows)

// The old view has the expected rows
rows = MustSelect(t, db, schema, "01_create_table", "items")
assert.Equal(t, []map[string]any{
{"id": 1, "name": "xxx"},
{"id": 2, "name": "abc"},
}, rows)
},
afterRollback: func(t *testing.T, db *sql.DB, schema string) {
// The table has been cleaned up
TableMustBeCleanedUp(t, db, schema, "items", "name")
},
afterComplete: func(t *testing.T, db *sql.DB, schema string) {
// The new column has the expected type
ColumnMustHaveType(t, db, schema, "products", "name", "text")

// Can insert a row into the new schema
MustInsert(t, db, schema, "02_multi_operation", "products", map[string]string{
"id": "3",
"name": "carrot",
})

// The new view has the expected rows
rows := MustSelect(t, db, schema, "02_multi_operation", "products")
assert.Equal(t, []map[string]any{
{"id": 1, "name": "xxx"},
{"id": 2, "name": "abc"},
{"id": 3, "name": "carrot"},
}, rows)

// The table has been cleaned up
TableMustBeCleanedUp(t, db, schema, "products", "name")
},
},
{
name: "rename table, rename column, change type",
migrations: []migrations.Migration{
{
Name: "01_create_table",
Operations: migrations.Operations{
&migrations.OpCreateTable{
Name: "items",
Columns: []migrations.Column{
{
Name: "id",
Type: "int",
Pk: true,
},
{
Name: "name",
Type: "varchar(3)",
Nullable: true,
},
},
},
},
},
{
Name: "02_multi_operation",
Operations: migrations.Operations{
&migrations.OpRenameTable{
From: "items",
To: "products",
},
&migrations.OpRenameColumn{
Table: "products",
From: "name",
To: "item_name",
},
&migrations.OpAlterColumn{
Table: "products",
Column: "item_name",
Type: ptr("text"),
Up: "item_name",
Down: "CASE WHEN LENGTH(item_name) > 3 THEN 'xxx' ELSE item_name END",
},
},
},
},
afterStart: func(t *testing.T, db *sql.DB, schema string) {
// The new column has the expected type
// The table hasn't been physically renamed yet, so we need to use the old name
ColumnMustHaveType(t, db, schema, "items", migrations.TemporaryName("item_name"), "text")

// Can insert a row into the new schema
MustInsert(t, db, schema, "02_multi_operation", "products", map[string]string{
"id": "1",
"item_name": "apple",
})

// Can insert a row into the old schema
MustInsert(t, db, schema, "01_create_table", "items", map[string]string{
"id": "2",
"name": "abc",
})

// The new view has the expected rows
rows := MustSelect(t, db, schema, "02_multi_operation", "products")
assert.Equal(t, []map[string]any{
{"id": 1, "item_name": "apple"},
{"id": 2, "item_name": "abc"},
}, rows)

// The old view has the expected rows
rows = MustSelect(t, db, schema, "01_create_table", "items")
assert.Equal(t, []map[string]any{
{"id": 1, "name": "xxx"},
{"id": 2, "name": "abc"},
}, rows)
},
afterRollback: func(t *testing.T, db *sql.DB, schema string) {
// The table has been cleaned up
TableMustBeCleanedUp(t, db, schema, "items", "name")
TableMustBeCleanedUp(t, db, schema, "items", "item_name")
},
afterComplete: func(t *testing.T, db *sql.DB, schema string) {
// The new column has the expected type
ColumnMustHaveType(t, db, schema, "products", "item_name", "text")

// Can insert a row into the new schema
MustInsert(t, db, schema, "02_multi_operation", "products", map[string]string{
"id": "3",
"item_name": "carrot",
})

// The new view has the expected rows
rows := MustSelect(t, db, schema, "02_multi_operation", "products")
assert.Equal(t, []map[string]any{
{"id": 1, "item_name": "xxx"},
{"id": 2, "item_name": "abc"},
{"id": 3, "item_name": "carrot"},
}, rows)

// The table has been cleaned up
TableMustBeCleanedUp(t, db, schema, "products", "name")
TableMustBeCleanedUp(t, db, schema, "products", "item_name")
},
},
})
}

func TestChangeColumnTypeValidation(t *testing.T) {
t.Parallel()

Expand Down