Skip to content
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

Simplify testing and update UUID format #86

Merged
merged 3 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion internal/migration_acceptance_tests/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,12 @@ func (suite *acceptanceTestSuite) runSubtest(tc acceptanceTestCase, expects expe
for _, stmt := range plan.Statements {
generatedDDL = append(generatedDDL, stmt.DDL)
}
suite.Equal(tc.ddl, generatedDDL)
// In the future, we might want to allow users to assert expectations for vanilla options and data packing
//
// We can also make the system more advanced by using tokens in place of the "randomly" generated UUIDs, such
// the test case doesn't need to be updated if the UUID generation changes. If we built this functionality, we
// should also integrate it with the schema_migration_plan_test.go tests.
suite.Equal(tc.ddl, generatedDDL, "data packing can change the the generated UUID and DDL")
}

// Make sure no diff is found if we try to regenerate a plan
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var checkConstraintCases = []acceptanceTestCase{
},
},
{
name: "Add check constraint",
name: "Add check constraint (validate constraint added online)",
oldSchemaDDL: []string{
`
CREATE TABLE foobar(
Expand All @@ -52,6 +52,10 @@ var checkConstraintCases = []acceptanceTestCase{
);
`,
},
ddl: []string{
"ALTER TABLE \"public\".\"foobar\" ADD CONSTRAINT \"foobar_check\" CHECK((bar > id)) NOT VALID",
"ALTER TABLE \"public\".\"foobar\" VALIDATE CONSTRAINT \"foobar_check\"",
},
},
{
name: "Add check constraint with UDF dependency should error",
Expand Down Expand Up @@ -383,7 +387,7 @@ var checkConstraintCases = []acceptanceTestCase{
},
},
{
name: "Alter an invalid check constraint to be valid",
name: "Alter an invalid check constraint to be valid (validate constraint isn't dropped and re-added)",
oldSchemaDDL: []string{
`
CREATE TABLE foobar(
Expand All @@ -404,6 +408,9 @@ var checkConstraintCases = []acceptanceTestCase{
ALTER TABLE foobar ADD CONSTRAINT bar_check CHECK ( bar > id );
`,
},
ddl: []string{
"ALTER TABLE \"public\".\"foobar\" VALIDATE CONSTRAINT \"bar_check\"",
},
},
{
name: "Alter a valid check constraint to be invalid",
Expand Down
82 changes: 42 additions & 40 deletions internal/migration_acceptance_tests/column_cases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,33 @@ var columnAcceptanceTestCases = []acceptanceTestCase{
diff.MigrationHazardTypeDeletesData,
},
},
{
name: "Change BIGINT to TIMESTAMP (validate conversion and ANALYZE)",
oldSchemaDDL: []string{
`
CREATE TABLE "Foobar"(
id INT PRIMARY KEY,
some_time_col BIGINT
);
`,
},
newSchemaDDL: []string{
`
CREATE TABLE "Foobar"(
id INT PRIMARY KEY,
some_time_col TIMESTAMP
);
`,
},
ddl: []string{
"ALTER TABLE \"public\".\"Foobar\" ALTER COLUMN \"some_time_col\" SET DATA TYPE timestamp without time zone using to_timestamp(\"some_time_col\" / 1000)",
"ANALYZE \"Foobar\" (\"some_time_col\")",
},
expectedHazardTypes: []diff.MigrationHazardType{
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
diff.MigrationHazardTypeImpactsDatabasePerformance,
},
},
{
name: "Modify data type (varchar -> TEXT) with compatible default",
oldSchemaDDL: []string{
Expand Down Expand Up @@ -353,7 +380,7 @@ var columnAcceptanceTestCases = []acceptanceTestCase{
},
},
{
name: "Modify collation (default -> non-default)",
name: "Modify collation (default -> non-default) (validate ANALYZE is run)",
oldSchemaDDL: []string{
`
CREATE TABLE foobar(
Expand All @@ -370,6 +397,10 @@ var columnAcceptanceTestCases = []acceptanceTestCase{
);
`,
},
ddl: []string{
"ALTER TABLE \"public\".\"foobar\" ALTER COLUMN \"foobar\" SET DATA TYPE character varying(255) COLLATE \"pg_catalog\".\"POSIX\" using \"foobar\"::character varying(255)",
"ANALYZE \"foobar\" (\"foobar\")",
},
expectedHazardTypes: []diff.MigrationHazardType{
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
diff.MigrationHazardTypeImpactsDatabasePerformance,
Expand Down Expand Up @@ -473,12 +504,7 @@ var columnAcceptanceTestCases = []acceptanceTestCase{
);
`,
},
ddl: []string{
"ALTER TABLE \"public\".\"foobar\" ADD CONSTRAINT \"pgschemadiff_tmpnn_10111213-1415-4617-9819-1a1b1c1d1e1f\" CHECK(\"foobar\" IS NOT NULL) NOT VALID",
"ALTER TABLE \"public\".\"foobar\" VALIDATE CONSTRAINT \"pgschemadiff_tmpnn_10111213-1415-4617-9819-1a1b1c1d1e1f\"",
"ALTER TABLE \"public\".\"foobar\" ALTER COLUMN \"foobar\" SET NOT NULL",
"ALTER TABLE \"public\".\"foobar\" DROP CONSTRAINT \"pgschemadiff_tmpnn_10111213-1415-4617-9819-1a1b1c1d1e1f\"",
},
ddl: []string{"ALTER TABLE \"public\".\"foobar\" ADD CONSTRAINT \"pgschemadiff_tmpnn_EBESExQVRheYGRobHB0eHw\" CHECK(\"foobar\" IS NOT NULL) NOT VALID", "ALTER TABLE \"public\".\"foobar\" VALIDATE CONSTRAINT \"pgschemadiff_tmpnn_EBESExQVRheYGRobHB0eHw\"", "ALTER TABLE \"public\".\"foobar\" ALTER COLUMN \"foobar\" SET NOT NULL", "ALTER TABLE \"public\".\"foobar\" DROP CONSTRAINT \"pgschemadiff_tmpnn_EBESExQVRheYGRobHB0eHw\""},
},
{
name: "Set NOT NULL (add invalid CC)",
Expand All @@ -500,11 +526,11 @@ var columnAcceptanceTestCases = []acceptanceTestCase{
`,
},
ddl: []string{
"ALTER TABLE \"public\".\"foobar\" ADD CONSTRAINT \"pgschemadiff_tmpnn_10111213-1415-4617-9819-1a1b1c1d1e1f\" CHECK(\"foobar\" IS NOT NULL) NOT VALID",
"ALTER TABLE \"public\".\"foobar\" VALIDATE CONSTRAINT \"pgschemadiff_tmpnn_10111213-1415-4617-9819-1a1b1c1d1e1f\"",
"ALTER TABLE \"public\".\"foobar\" ADD CONSTRAINT \"pgschemadiff_tmpnn_EBESExQVRheYGRobHB0eHw\" CHECK(\"foobar\" IS NOT NULL) NOT VALID",
"ALTER TABLE \"public\".\"foobar\" VALIDATE CONSTRAINT \"pgschemadiff_tmpnn_EBESExQVRheYGRobHB0eHw\"",
"ALTER TABLE \"public\".\"foobar\" ALTER COLUMN \"foobar\" SET NOT NULL",
"ALTER TABLE \"public\".\"foobar\" ADD CONSTRAINT \"foobar\" CHECK((foobar IS NOT NULL)) NOT VALID",
"ALTER TABLE \"public\".\"foobar\" DROP CONSTRAINT \"pgschemadiff_tmpnn_10111213-1415-4617-9819-1a1b1c1d1e1f\"",
"ALTER TABLE \"public\".\"foobar\" DROP CONSTRAINT \"pgschemadiff_tmpnn_EBESExQVRheYGRobHB0eHw\"",
},
},

Expand All @@ -529,10 +555,10 @@ var columnAcceptanceTestCases = []acceptanceTestCase{
`,
},
ddl: []string{
"ALTER TABLE \"public\".\"foobar\" ADD CONSTRAINT \"pgschemadiff_tmpnn_10111213-1415-4617-9819-1a1b1c1d1e1f\" CHECK(\"foobar\" IS NOT NULL) NOT VALID",
"ALTER TABLE \"public\".\"foobar\" VALIDATE CONSTRAINT \"pgschemadiff_tmpnn_10111213-1415-4617-9819-1a1b1c1d1e1f\"",
"ALTER TABLE \"public\".\"foobar\" ADD CONSTRAINT \"pgschemadiff_tmpnn_EBESExQVRheYGRobHB0eHw\" CHECK(\"foobar\" IS NOT NULL) NOT VALID",
"ALTER TABLE \"public\".\"foobar\" VALIDATE CONSTRAINT \"pgschemadiff_tmpnn_EBESExQVRheYGRobHB0eHw\"",
"ALTER TABLE \"public\".\"foobar\" ALTER COLUMN \"foobar\" SET NOT NULL",
"ALTER TABLE \"public\".\"foobar\" DROP CONSTRAINT \"pgschemadiff_tmpnn_10111213-1415-4617-9819-1a1b1c1d1e1f\"",
"ALTER TABLE \"public\".\"foobar\" DROP CONSTRAINT \"pgschemadiff_tmpnn_EBESExQVRheYGRobHB0eHw\"",
},
},
{
Expand Down Expand Up @@ -683,17 +709,16 @@ var columnAcceptanceTestCases = []acceptanceTestCase{
diff.MigrationHazardTypeImpactsDatabasePerformance,
},
ddl: []string{
"ALTER TABLE \"public\".\"foobar\" ADD CONSTRAINT \"pgschemadiff_tmpnn_10111213-1415-4617-9819-1a1b1c1d1e1f\" CHECK(\"foobar\" IS NOT NULL) NOT VALID",
"ALTER TABLE \"public\".\"foobar\" VALIDATE CONSTRAINT \"pgschemadiff_tmpnn_10111213-1415-4617-9819-1a1b1c1d1e1f\"",
"ALTER TABLE \"public\".\"foobar\" ADD CONSTRAINT \"pgschemadiff_tmpnn_EBESExQVRheYGRobHB0eHw\" CHECK(\"foobar\" IS NOT NULL) NOT VALID",
"ALTER TABLE \"public\".\"foobar\" VALIDATE CONSTRAINT \"pgschemadiff_tmpnn_EBESExQVRheYGRobHB0eHw\"",
"ALTER TABLE \"public\".\"foobar\" ALTER COLUMN \"foobar\" SET NOT NULL",
"ALTER TABLE \"public\".\"foobar\" ALTER COLUMN \"foobar\" SET DATA TYPE integer using \"foobar\"::integer",
"ANALYZE \"foobar\" (\"foobar\")",
"ALTER TABLE \"public\".\"foobar\" ADD CONSTRAINT \"foobar_foobar_check\" CHECK((foobar > 0)) NOT VALID",
"ALTER TABLE \"public\".\"foobar\" VALIDATE CONSTRAINT \"foobar_foobar_check\"",
"ALTER TABLE \"public\".\"foobar\" DROP CONSTRAINT \"pgschemadiff_tmpnn_10111213-1415-4617-9819-1a1b1c1d1e1f\"",
"ALTER TABLE \"public\".\"foobar\" DROP CONSTRAINT \"pgschemadiff_tmpnn_EBESExQVRheYGRobHB0eHw\"",
},
},
// TODO(bplunkett) Add not null migration where valid cc is being dropped
{
name: "Remove NOT NULL",
oldSchemaDDL: []string{
Expand Down Expand Up @@ -918,29 +943,6 @@ var columnAcceptanceTestCases = []acceptanceTestCase{
diff.MigrationHazardTypeImpactsDatabasePerformance,
},
},
{
name: "Change BIGINT to TIMESTAMP, nullability (NOT NULL), and default with current_timestamp",
oldSchemaDDL: []string{
`
CREATE TABLE "Foobar"(
id INT PRIMARY KEY,
some_time_col BIGINT
);
`,
},
newSchemaDDL: []string{
`
CREATE TABLE "Foobar"(
id INT PRIMARY KEY,
some_time_col TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
);
`,
},
expectedHazardTypes: []diff.MigrationHazardType{
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
diff.MigrationHazardTypeImpactsDatabasePerformance,
},
},
}

func (suite *acceptanceTestSuite) TestColumnAcceptanceTestCases() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ var foreignKeyConstraintCases = []acceptanceTestCase{
},
},
{
name: "Alter FK (not valid to valid)",
name: "Alter FK not valid to valid (validate FK isn't dropped and re-added)",
oldSchemaDDL: []string{
`
CREATE TABLE foobar(
Expand Down Expand Up @@ -319,6 +319,9 @@ var foreignKeyConstraintCases = []acceptanceTestCase{
FOREIGN KEY (fk_id) REFERENCES foobar(id);
`,
},
ddl: []string{
"ALTER TABLE \"public\".\"foobar fk\" VALIDATE CONSTRAINT \"some_fk\"",
},
},
{
name: "Alter FK (valid to not valid)",
Expand Down
57 changes: 57 additions & 0 deletions internal/migration_acceptance_tests/index_cases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,33 @@ var indexAcceptanceTestCases = []acceptanceTestCase{
diff.MigrationHazardTypeIndexDropped,
},
},
{
name: "Delete a normal index and columns (index dropped concurrently first)",
oldSchemaDDL: []string{
`
CREATE TABLE foobar(
id INT PRIMARY KEY,
foo VARCHAR(255) NOT NULL
);
CREATE INDEX some_idx ON foobar(foo);
`,
},
newSchemaDDL: []string{
`
CREATE TABLE foobar(
id INT PRIMARY KEY
);
`,
},
expectedHazardTypes: []diff.MigrationHazardType{
diff.MigrationHazardTypeDeletesData,
diff.MigrationHazardTypeIndexDropped,
},
ddl: []string{
"DROP INDEX CONCURRENTLY \"some_idx\"",
"ALTER TABLE \"public\".\"foobar\" DROP COLUMN \"foo\"",
},
},
{
name: "Delete a normal index with quoted names",
oldSchemaDDL: []string{
Expand Down Expand Up @@ -655,6 +682,36 @@ var indexAcceptanceTestCases = []acceptanceTestCase{
diff.MigrationHazardTypeIndexBuild,
},
},
{
name: "Alter index columns (index replacement and prioritized builds)",
oldSchemaDDL: []string{`
CREATE TABLE foobar(
foo TEXT,
bar INT
);
CREATE INDEX some_idx ON foobar(foo);
CREATE INDEX old_idx ON foobar(bar);
`},
newSchemaDDL: []string{`
CREATE TABLE foobar(
foo TEXT,
bar INT
);
CREATE INDEX some_idx ON foobar(foo, bar);
CREATE INDEX new_idx ON foobar(bar);
`},
ddl: []string{
"ALTER INDEX \"some_idx\" RENAME TO \"pgschemadiff_tmpidx_some_idx_EBESExQVRheYGRobHB0eHw\"",
"CREATE INDEX CONCURRENTLY new_idx ON public.foobar USING btree (bar)",
"CREATE INDEX CONCURRENTLY some_idx ON public.foobar USING btree (foo, bar)",
"DROP INDEX CONCURRENTLY \"old_idx\"",
"DROP INDEX CONCURRENTLY \"pgschemadiff_tmpidx_some_idx_EBESExQVRheYGRobHB0eHw\"",
},
expectedHazardTypes: []diff.MigrationHazardType{
diff.MigrationHazardTypeIndexBuild,
diff.MigrationHazardTypeIndexDropped,
},
},
}

func (suite *acceptanceTestSuite) TestIndexAcceptanceTestCases() {
Expand Down
Loading
Loading