Skip to content

Commit

Permalink
Add support for create_index operations in multi-operation migratio…
Browse files Browse the repository at this point in the history
…ns (#662)

Ensure that multi-operation migrations combining `create_index`
operations work in combination with other operations.

Add testcases for:

* rename table, create index
* rename table, rename column, create index

Part of #239
  • Loading branch information
andrew-farries authored Feb 7, 2025
1 parent 5b42f80 commit ee84b88
Showing 1 changed file with 108 additions and 1 deletion.
109 changes: 108 additions & 1 deletion pkg/migrations/op_create_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,117 @@ func TestCreateIndexOnMultipleColumns(t *testing.T) {
}})
}

func TestCreateIndexOnObjectsCreatedInSameMigration(t *testing.T) {
func TestCreateIndexInMultiOperationMigrations(t *testing.T) {
t.Parallel()

ExecuteTests(t, TestCases{
{
name: "rename table, create index",
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(255)",
Nullable: true,
},
},
},
},
},
{
Name: "02_multi_operation",
Operations: migrations.Operations{
&migrations.OpRenameTable{
From: "items",
To: "products",
},
&migrations.OpCreateIndex{
Table: "products",
Columns: []string{"name"},
Name: "idx_products_name",
},
},
},
},
afterStart: func(t *testing.T, db *sql.DB, schema string) {
// The index has been created on the underlying table.
IndexMustExist(t, db, schema, "items", "idx_products_name")
},
afterRollback: func(t *testing.T, db *sql.DB, schema string) {
// The index has been dropped from the the underlying table.
IndexMustNotExist(t, db, schema, "items", "idx_products_name")
},
afterComplete: func(t *testing.T, db *sql.DB, schema string) {
// The index remains on the underlying table.
IndexMustExist(t, db, schema, "products", "idx_products_name")
},
},
{
name: "rename table, rename column, create index",
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(255)",
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.OpCreateIndex{
Table: "products",
Columns: []string{"item_name"},
Name: "idx_products_item_name",
},
},
},
},
afterStart: func(t *testing.T, db *sql.DB, schema string) {
// The index has been created on the underlying table.
IndexMustExist(t, db, schema, "items", "idx_products_item_name")
},
afterRollback: func(t *testing.T, db *sql.DB, schema string) {
// The index has been dropped from the the underlying table.
IndexMustNotExist(t, db, schema, "items", "idx_products_item_name")
},
afterComplete: func(t *testing.T, db *sql.DB, schema string) {
// The index remains on the underlying table.
IndexMustExist(t, db, schema, "products", "idx_products_item_name")
},
},
{
name: "create index on newly created table",
migrations: []migrations.Migration{
Expand Down

0 comments on commit ee84b88

Please sign in to comment.