Skip to content

Commit

Permalink
Add sequence support (#35)
Browse files Browse the repository at this point in the history
Add sequence support
  • Loading branch information
bplunkett-stripe authored Jul 7, 2023
1 parent fda9588 commit a0d7b84
Show file tree
Hide file tree
Showing 21 changed files with 1,423 additions and 121 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pg-schema-diff plan --dsn "postgres://postgres:postgres@localhost:5432/postgres"
- Indexes
- Partitions
- Functions/Triggers (functions created by extensions are ignored)
- Sequences

*A comprehensive set of features to ensure the safety of planned migrations:*
- Dangerous operations are flagged as hazards and must be approved before a migration can be applied.
Expand Down Expand Up @@ -130,7 +131,6 @@ Note, the library only currently supports diffing the *public* schema. Support f
*Unsupported*:
- (On roadmap) Foreign key constraints
- (On roadmap) Diffing schemas other than "public"
- (On roadmap) Serials and sequences
- (On roadmap) Unique constraints (unique indexes are supported but not unique constraints)
- (On roadmap) Adding and remove partitions from an existing partitioned table
- (On roadmap) Check constraints localized to specific partitions
Expand Down
6 changes: 6 additions & 0 deletions internal/migration_acceptance_tests/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type (
//
// If no outputState is specified, the newSchemaDDL will be used
outputState []string
// empty asserts the plan is empty
empty bool
}

acceptanceTestCase struct {
Expand Down Expand Up @@ -128,6 +130,10 @@ func (suite *acceptanceTestSuite) runSubtest(tc acceptanceTestCase, expects expe
}
suite.Require().NoError(err)

if expects.empty {
// It shouldn't be necessary, but we'll run all checks below this point just in case rather than exiting early
suite.Empty(plan.Statements)
}
suite.ElementsMatch(tc.expectedHazardTypes, getUniqueHazardTypesFromStatements(plan.Statements), prettySprintPlan(plan))

// Apply the plan
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ var checkConstraintCases = []acceptanceTestCase{
);
`,
},
vanillaExpectations: expectations{
empty: true,
},
dataPackingExpectations: expectations{
empty: true,
},
},
{
name: "Add check constraint",
Expand Down
73 changes: 68 additions & 5 deletions internal/migration_acceptance_tests/column_cases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ var columnAcceptanceTestCases = []acceptanceTestCase{
);
`,
},
vanillaExpectations: expectations{
empty: true,
},
dataPackingExpectations: expectations{
empty: true,
},
},
{
name: "Add one column with default",
Expand Down Expand Up @@ -82,6 +88,24 @@ var columnAcceptanceTestCases = []acceptanceTestCase{
`,
},
},
{
name: "Add one column with serial",
oldSchemaDDL: []string{
`
CREATE TABLE foobar(
id INT PRIMARY KEY
);
`,
},
newSchemaDDL: []string{
`
CREATE TABLE foobar(
id INT PRIMARY KEY,
my_new_column SERIAL NOT NULL
);
`,
},
},
{
name: "Add one column with all options",
oldSchemaDDL: []string{
Expand Down Expand Up @@ -177,26 +201,65 @@ var columnAcceptanceTestCases = []acceptanceTestCase{
},
},
{
name: "Modify data type (varchar -> char)",
name: "Delete one column with serial",
oldSchemaDDL: []string{
`
CREATE TABLE "Foobar"(
id BIGSERIAL PRIMARY KEY
);
`,
},
newSchemaDDL: []string{
`
CREATE TABLE "Foobar"(
);
`,
},
expectedHazardTypes: []diff.MigrationHazardType{
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
diff.MigrationHazardTypeDeletesData,
diff.MigrationHazardTypeIndexDropped,
},
},
{
name: "Modify data type (int -> serial)",
oldSchemaDDL: []string{
`
CREATE TABLE foobar(
id INT PRIMARY KEY,
foobar VARCHAR(255) NOT NULL
foobar INT NOT NULL
);
`,
},
newSchemaDDL: []string{
`
CREATE TABLE foobar(
id INT PRIMARY KEY,
foobar CHAR NOT NULL
foobar SERIAL NOT NULL
);
`,
},
},
{
name: "Modify data type (serial -> int)",
oldSchemaDDL: []string{
`
CREATE TABLE foobar(
id INT PRIMARY KEY,
foobar SERIAL NOT NULL
);
`,
},
newSchemaDDL: []string{
`
CREATE TABLE foobar(
id INT PRIMARY KEY,
foobar INT NOT NULL
);
`,
},
expectedHazardTypes: []diff.MigrationHazardType{
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
diff.MigrationHazardTypeImpactsDatabasePerformance,
diff.MigrationHazardTypeDeletesData,
},
},
{
Expand Down
6 changes: 6 additions & 0 deletions internal/migration_acceptance_tests/function_cases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ var functionAcceptanceTestCases = []acceptanceTestCase{
$$ LANGUAGE plpgsql;
`,
},
vanillaExpectations: expectations{
empty: true,
},
dataPackingExpectations: expectations{
empty: true,
},
},
{
name: "Create functions (with conflicting names)",
Expand Down
6 changes: 6 additions & 0 deletions internal/migration_acceptance_tests/index_cases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ var indexAcceptanceTestCases = []acceptanceTestCase{
CREATE UNIQUE INDEX some_other_idx ON foobar (bar DESC, fizz);
`,
},
vanillaExpectations: expectations{
empty: true,
},
dataPackingExpectations: expectations{
empty: true,
},
},
{
name: "Add a normal index",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ var localPartitionIndexAcceptanceTestCases = []acceptanceTestCase{
CREATE UNIQUE INDEX foobar_2_some_unique_idx ON foobar_2 (foo);
`,
},
vanillaExpectations: expectations{
empty: true,
},
dataPackingExpectations: expectations{
empty: true,
},
},
{
name: "Add local indexes",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ var partitionedIndexAcceptanceTestCases = []acceptanceTestCase{
CREATE UNIQUE INDEX some_other_idx ON foobar(foo DESC, fizz);
`,
},
vanillaExpectations: expectations{
empty: true,
},
dataPackingExpectations: expectations{
empty: true,
},
},
{
name: "Add a normal partitioned index",
Expand Down
Loading

0 comments on commit a0d7b84

Please sign in to comment.