Skip to content

Commit df2bdee

Browse files
Tweak
1 parent f7d8c2c commit df2bdee

File tree

14 files changed

+89
-109
lines changed

14 files changed

+89
-109
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,6 @@ Apply the schema. Any hazards in the generated plan must be approved
152152
pg-schema-diff apply --from-dsn "postgres://postgres:postgres@localhost:5432/postgres" --to-dir schema --allow-hazards INDEX_BUILD
153153
```
154154

155-
# Stateful migrations
156-
- Adding a brand new `NOT NULL` column without a constant `DEFAULT` requires backfilling existing rows. pg-schema-diff will emit the `NEW_NOT_NULL_COLUMN_REQUIRES_BACKFILL` hazard and annotate the plan with an online sequence: add the column as nullable, backfill in batches, then rerun pg-schema-diff to flip to `NOT NULL` via the online CHECK/VALIDATE/SET NOT NULL flow.
157-
158155
# Using Library
159156
Docs to use the library can be found [here](https://pkg.go.dev/github.com/stripe/pg-schema-diff). Check out [the CLI](https://github.com/stripe/pg-schema-diff/tree/main/cmd/pg-schema-diff)
160157
for an example implementation with the library

cmd/pg-schema-diff/apply_cmd_test.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"github.com/stripe/pg-schema-diff/internal/pgdump"
55
"github.com/stripe/pg-schema-diff/internal/pgengine"
6-
"github.com/stripe/pg-schema-diff/pkg/diff"
76
)
87

98
func (suite *cmdTestSuite) TestApplyCmd() {
@@ -88,21 +87,3 @@ func (suite *cmdTestSuite) TestApplyCmd() {
8887
})
8988
}
9089
}
91-
92-
func (suite *cmdTestSuite) TestFailIfHazardsNotAllowed() {
93-
plan := diff.Plan{
94-
Statements: []diff.Statement{{
95-
DDL: "ALTER TABLE public.t ADD COLUMN city_name text NOT NULL",
96-
Hazards: []diff.MigrationHazard{{
97-
Type: diff.MigrationHazardTypeNewNotNullColumnRequiresBackfill,
98-
}},
99-
}},
100-
}
101-
102-
err := failIfHazardsNotAllowed(plan, nil)
103-
suite.Error(err)
104-
suite.Contains(err.Error(), string(diff.MigrationHazardTypeNewNotNullColumnRequiresBackfill))
105-
106-
err = failIfHazardsNotAllowed(plan, []string{string(diff.MigrationHazardTypeNewNotNullColumnRequiresBackfill)})
107-
suite.NoError(err)
108-
}

internal/migration_acceptance_tests/acceptance_test.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
stdlog "log"
88
"os"
9-
"strings"
109
"testing"
1110

1211
"github.com/google/uuid"
@@ -63,9 +62,6 @@ type (
6362
//
6463
// If no expectedDBSchemaDDL is specified, the newSchemaDDL will be used
6564
expectedDBSchemaDDL []string
66-
// expectedHazardMessages is a list of substrings that must be found within the set of hazard messages generated
67-
// by the plan.
68-
expectedHazardMessages []string
6965
}
7066
)
7167

@@ -182,19 +178,6 @@ func runTest(t *testing.T, tc acceptanceTestCase) {
182178
}
183179
assert.ElementsMatch(t, tc.expectedHazardTypes, getUniqueHazardTypesFromStatements(plan.Statements), prettySprintPlan(plan))
184180

185-
if len(tc.expectedHazardMessages) > 0 {
186-
var hazardMsgs []string
187-
for _, stmt := range plan.Statements {
188-
for _, hazard := range stmt.Hazards {
189-
hazardMsgs = append(hazardMsgs, hazard.Message)
190-
}
191-
}
192-
joinedHazardMsgs := strings.Join(hazardMsgs, "\n")
193-
for _, expectedHazardMsg := range tc.expectedHazardMessages {
194-
assert.Contains(t, joinedHazardMsgs, expectedHazardMsg, prettySprintPlan(plan))
195-
}
196-
}
197-
198181
// Apply the plan
199182
require.NoError(t, applyPlan(oldDb, plan), prettySprintPlan(plan))
200183

internal/migration_acceptance_tests/column_cases_test.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ var columnAcceptanceTestCases = []acceptanceTestCase{
4949
);
5050
`,
5151
},
52+
expectedHazardTypes: []diff.MigrationHazardType{
53+
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
54+
},
5255
},
5356
{
5457
name: "Add one column with quoted names",
@@ -67,6 +70,9 @@ var columnAcceptanceTestCases = []acceptanceTestCase{
6770
);
6871
`,
6972
},
73+
expectedHazardTypes: []diff.MigrationHazardType{
74+
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
75+
},
7076
},
7177
{
7278
name: "Add one column with nullability",
@@ -85,9 +91,6 @@ var columnAcceptanceTestCases = []acceptanceTestCase{
8591
);
8692
`,
8793
},
88-
expectedHazardTypes: []diff.MigrationHazardType{
89-
diff.MigrationHazardTypeNewNotNullColumnRequiresBackfill,
90-
},
9194
},
9295
{
9396
name: "Add one column with serial",
@@ -106,6 +109,9 @@ var columnAcceptanceTestCases = []acceptanceTestCase{
106109
);
107110
`,
108111
},
112+
expectedHazardTypes: []diff.MigrationHazardType{
113+
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
114+
},
109115
},
110116
{
111117
name: "Add one column with all options",
@@ -124,6 +130,9 @@ var columnAcceptanceTestCases = []acceptanceTestCase{
124130
);
125131
`,
126132
},
133+
expectedHazardTypes: []diff.MigrationHazardType{
134+
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
135+
},
127136
},
128137
{
129138
name: "Add one column and change ordering",
@@ -149,6 +158,9 @@ var columnAcceptanceTestCases = []acceptanceTestCase{
149158
my_new_column VARCHAR(255) NOT NULL DEFAULT 'a'
150159
)
151160
`},
161+
expectedHazardTypes: []diff.MigrationHazardType{
162+
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
163+
},
152164
},
153165
{
154166
name: "Add identity column - always no cycle",
@@ -167,6 +179,9 @@ var columnAcceptanceTestCases = []acceptanceTestCase{
167179
);
168180
`,
169181
},
182+
expectedHazardTypes: []diff.MigrationHazardType{
183+
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
184+
},
170185
},
171186
{
172187
name: "Add identity column - default cycle",
@@ -185,6 +200,9 @@ var columnAcceptanceTestCases = []acceptanceTestCase{
185200
);
186201
`,
187202
},
203+
expectedHazardTypes: []diff.MigrationHazardType{
204+
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
205+
},
188206
},
189207
{
190208
name: "Delete one column",
@@ -1210,6 +1228,9 @@ var columnAcceptanceTestCases = []acceptanceTestCase{
12101228
);
12111229
`,
12121230
},
1231+
expectedHazardTypes: []diff.MigrationHazardType{
1232+
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
1233+
},
12131234
},
12141235
{
12151236
name: "Drop generated column",
@@ -1273,6 +1294,9 @@ var columnAcceptanceTestCases = []acceptanceTestCase{
12731294
);
12741295
`,
12751296
},
1297+
expectedHazardTypes: []diff.MigrationHazardType{
1298+
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
1299+
},
12761300
},
12771301
{
12781302
name: "Generated column with index",
@@ -1299,6 +1323,7 @@ var columnAcceptanceTestCases = []acceptanceTestCase{
12991323
`,
13001324
},
13011325
expectedHazardTypes: []diff.MigrationHazardType{
1326+
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
13021327
diff.MigrationHazardTypeIndexBuild,
13031328
},
13041329
},

internal/migration_acceptance_tests/database_schema_source_cases_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ var databaseSchemaSourceTestCases = []acceptanceTestCase{
155155
},
156156

157157
expectedHazardTypes: []diff.MigrationHazardType{
158+
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
158159
diff.MigrationHazardTypeIndexBuild,
159-
diff.MigrationHazardTypeNewNotNullColumnRequiresBackfill,
160160
},
161161
expectedDBSchemaDDL: []string{
162162
`

internal/migration_acceptance_tests/foreign_key_constraint_cases_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,6 @@ var foreignKeyConstraintCases = []acceptanceTestCase{
866866

867867
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
868868
diff.MigrationHazardTypeAcquiresShareRowExclusiveLock,
869-
diff.MigrationHazardTypeNewNotNullColumnRequiresBackfill,
870869
diff.MigrationHazardTypeIndexBuild,
871870
diff.MigrationHazardTypeIndexDropped,
872871
},

internal/migration_acceptance_tests/procedure_cases_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ var procedureAcceptanceTestCases = []acceptanceTestCase{
9696
`,
9797
},
9898
expectedHazardTypes: []diff.MigrationHazardType{
99+
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
99100
diff.MigrationHazardTypeHasUntrackableDependencies,
100-
diff.MigrationHazardTypeNewNotNullColumnRequiresBackfill,
101101
},
102102
},
103103
{

internal/migration_acceptance_tests/table_cases_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,6 @@ var tableAcceptanceTestCases = []acceptanceTestCase{
541541
diff.MigrationHazardTypeAuthzUpdate,
542542
diff.MigrationHazardTypeDeletesData,
543543
diff.MigrationHazardTypeIndexDropped,
544-
diff.MigrationHazardTypeNewNotNullColumnRequiresBackfill,
545544
diff.MigrationHazardTypeIndexBuild,
546545
},
547546
},
@@ -571,7 +570,7 @@ var tableAcceptanceTestCases = []acceptanceTestCase{
571570
},
572571
},
573572
{
574-
name: "Add NOT NULL column without default emits backfill hazard",
573+
name: "Add NOT NULL column without default",
575574
oldSchemaDDL: []string{
576575
`
577576
CREATE TABLE t(
@@ -588,12 +587,7 @@ var tableAcceptanceTestCases = []acceptanceTestCase{
588587
`,
589588
},
590589
expectedHazardTypes: []diff.MigrationHazardType{
591-
diff.MigrationHazardTypeNewNotNullColumnRequiresBackfill,
592-
},
593-
expectedHazardMessages: []string{
594-
"backfill",
595-
"Add the column as NULLABLE",
596-
"SET NOT NULL",
590+
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
597591
},
598592
},
599593
{

internal/queries/queries.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ WITH identity_col_seq AS (
9090
SELECT
9191
a.attname::TEXT AS column_name,
9292
a.attnotnull AS is_not_null,
93+
a.atthasmissing AS has_missing_val_optimization,
9394
a.attlen AS column_size,
9495
a.attidentity::TEXT AS identity_type,
9596
identity_col_seq.seqstart AS start_value,

internal/queries/queries.sql.go

Lines changed: 19 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)