From 3b8c56e13edd19442dd248ea7fad11669612de61 Mon Sep 17 00:00:00 2001 From: Andrew Farries Date: Thu, 5 Dec 2024 11:09:38 +0000 Subject: [PATCH] Rename and move test variables relating to `sql2pgroll` (#512) * Rename the test variables to match their operation type. * Rename files to match the operation type. Part of #504 --- pkg/sql2pgroll/alter_table_test.go | 12 ++-- pkg/sql2pgroll/expect/alter_column.go | 42 ++++++++++++++ pkg/sql2pgroll/expect/alter_table.go | 66 ---------------------- pkg/sql2pgroll/expect/create_constraint.go | 32 +++++++++++ pkg/sql2pgroll/rename_test.go | 4 +- 5 files changed, 82 insertions(+), 74 deletions(-) create mode 100644 pkg/sql2pgroll/expect/alter_column.go delete mode 100644 pkg/sql2pgroll/expect/alter_table.go create mode 100644 pkg/sql2pgroll/expect/create_constraint.go diff --git a/pkg/sql2pgroll/alter_table_test.go b/pkg/sql2pgroll/alter_table_test.go index a0280b9c..a6c89cd7 100644 --- a/pkg/sql2pgroll/alter_table_test.go +++ b/pkg/sql2pgroll/alter_table_test.go @@ -21,27 +21,27 @@ func TestConvertAlterTableStatements(t *testing.T) { }{ { sql: "ALTER TABLE foo ALTER COLUMN a SET NOT NULL", - expectedOp: expect.AlterTableOp1, + expectedOp: expect.AlterColumnOp1, }, { sql: "ALTER TABLE foo ALTER COLUMN a DROP NOT NULL", - expectedOp: expect.AlterTableOp2, + expectedOp: expect.AlterColumnOp2, }, { sql: "ALTER TABLE foo ALTER COLUMN a SET DATA TYPE text", - expectedOp: expect.AlterTableOp3, + expectedOp: expect.AlterColumnOp3, }, { sql: "ALTER TABLE foo ALTER COLUMN a TYPE text", - expectedOp: expect.AlterTableOp3, + expectedOp: expect.AlterColumnOp3, }, { sql: "ALTER TABLE foo ADD CONSTRAINT bar UNIQUE (a)", - expectedOp: expect.AlterTableOp4, + expectedOp: expect.CreateConstraintOp1, }, { sql: "ALTER TABLE foo ADD CONSTRAINT bar UNIQUE (a, b)", - expectedOp: expect.AlterTableOp5, + expectedOp: expect.CreateConstraintOp2, }, } diff --git a/pkg/sql2pgroll/expect/alter_column.go b/pkg/sql2pgroll/expect/alter_column.go new file mode 100644 index 00000000..ec9b0acc --- /dev/null +++ b/pkg/sql2pgroll/expect/alter_column.go @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: Apache-2.0 + +package expect + +import ( + "github.com/xataio/pgroll/pkg/migrations" + "github.com/xataio/pgroll/pkg/sql2pgroll" +) + +var AlterColumnOp1 = &migrations.OpAlterColumn{ + Table: "foo", + Column: "a", + Nullable: ptr(false), + Up: sql2pgroll.PlaceHolderSQL, + Down: sql2pgroll.PlaceHolderSQL, +} + +var AlterColumnOp2 = &migrations.OpAlterColumn{ + Table: "foo", + Column: "a", + Nullable: ptr(true), + Up: sql2pgroll.PlaceHolderSQL, + Down: sql2pgroll.PlaceHolderSQL, +} + +var AlterColumnOp3 = &migrations.OpAlterColumn{ + Table: "foo", + Column: "a", + Type: ptr("text"), + Up: sql2pgroll.PlaceHolderSQL, + Down: sql2pgroll.PlaceHolderSQL, +} + +var AlterColumnOp4 = &migrations.OpAlterColumn{ + Table: "foo", + Column: "a", + Name: ptr("b"), +} + +func ptr[T any](v T) *T { + return &v +} diff --git a/pkg/sql2pgroll/expect/alter_table.go b/pkg/sql2pgroll/expect/alter_table.go deleted file mode 100644 index e912a157..00000000 --- a/pkg/sql2pgroll/expect/alter_table.go +++ /dev/null @@ -1,66 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -package expect - -import ( - "github.com/xataio/pgroll/pkg/migrations" - "github.com/xataio/pgroll/pkg/sql2pgroll" -) - -var AlterTableOp1 = &migrations.OpAlterColumn{ - Table: "foo", - Column: "a", - Nullable: ptr(false), - Up: sql2pgroll.PlaceHolderSQL, - Down: sql2pgroll.PlaceHolderSQL, -} - -var AlterTableOp2 = &migrations.OpAlterColumn{ - Table: "foo", - Column: "a", - Nullable: ptr(true), - Up: sql2pgroll.PlaceHolderSQL, - Down: sql2pgroll.PlaceHolderSQL, -} - -var AlterTableOp3 = &migrations.OpAlterColumn{ - Table: "foo", - Column: "a", - Type: ptr("text"), - Up: sql2pgroll.PlaceHolderSQL, - Down: sql2pgroll.PlaceHolderSQL, -} - -var AlterTableOp4 = &migrations.OpCreateConstraint{ - Type: migrations.OpCreateConstraintTypeUnique, - Name: "bar", - Table: "foo", - Columns: []string{"a"}, - Down: map[string]string{"a": sql2pgroll.PlaceHolderSQL}, - Up: map[string]string{"a": sql2pgroll.PlaceHolderSQL}, -} - -var AlterTableOp5 = &migrations.OpCreateConstraint{ - Type: migrations.OpCreateConstraintTypeUnique, - Name: "bar", - Table: "foo", - Columns: []string{"a", "b"}, - Down: map[string]string{ - "a": sql2pgroll.PlaceHolderSQL, - "b": sql2pgroll.PlaceHolderSQL, - }, - Up: map[string]string{ - "a": sql2pgroll.PlaceHolderSQL, - "b": sql2pgroll.PlaceHolderSQL, - }, -} - -var AlterTableOp6 = &migrations.OpAlterColumn{ - Table: "foo", - Column: "a", - Name: ptr("b"), -} - -func ptr[T any](v T) *T { - return &v -} diff --git a/pkg/sql2pgroll/expect/create_constraint.go b/pkg/sql2pgroll/expect/create_constraint.go new file mode 100644 index 00000000..a408fcae --- /dev/null +++ b/pkg/sql2pgroll/expect/create_constraint.go @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: Apache-2.0 + +package expect + +import ( + "github.com/xataio/pgroll/pkg/migrations" + "github.com/xataio/pgroll/pkg/sql2pgroll" +) + +var CreateConstraintOp1 = &migrations.OpCreateConstraint{ + Type: migrations.OpCreateConstraintTypeUnique, + Name: "bar", + Table: "foo", + Columns: []string{"a"}, + Down: map[string]string{"a": sql2pgroll.PlaceHolderSQL}, + Up: map[string]string{"a": sql2pgroll.PlaceHolderSQL}, +} + +var CreateConstraintOp2 = &migrations.OpCreateConstraint{ + Type: migrations.OpCreateConstraintTypeUnique, + Name: "bar", + Table: "foo", + Columns: []string{"a", "b"}, + Down: map[string]string{ + "a": sql2pgroll.PlaceHolderSQL, + "b": sql2pgroll.PlaceHolderSQL, + }, + Up: map[string]string{ + "a": sql2pgroll.PlaceHolderSQL, + "b": sql2pgroll.PlaceHolderSQL, + }, +} diff --git a/pkg/sql2pgroll/rename_test.go b/pkg/sql2pgroll/rename_test.go index 217cb145..3295fa4a 100644 --- a/pkg/sql2pgroll/rename_test.go +++ b/pkg/sql2pgroll/rename_test.go @@ -21,11 +21,11 @@ func TestConvertRenameColumnStatements(t *testing.T) { }{ { sql: "ALTER TABLE foo RENAME COLUMN a TO b", - expectedOp: expect.AlterTableOp6, + expectedOp: expect.AlterColumnOp4, }, { sql: "ALTER TABLE foo RENAME a TO b", - expectedOp: expect.AlterTableOp6, + expectedOp: expect.AlterColumnOp4, }, }