Repository navigation
fix: drop a type after the columns of modified tables that use it (#710) #711
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2011,13 +2011,13 @@ func (d *ddlDiff) collectMigrationSQL(targetSchema string, collector *diffCollec | |
| } | ||
|
|
||
| // First: Drop operations (in reverse dependency order) | ||
| d.generateDropSQL(targetSchema, collector, preDroppedViews) | ||
| typeDropsAfterTables := d.generateDropSQL(targetSchema, collector, preDroppedViews) | ||
|
|
||
| // Then: Create operations (in dependency order) | ||
| d.generateCreateSQL(targetSchema, collector) | ||
|
|
||
| // Finally: Modify operations | ||
| d.generateModifySQL(targetSchema, collector, preDroppedViews) | ||
| d.generateModifySQL(targetSchema, collector, preDroppedViews, typeDropsAfterTables) | ||
| } | ||
|
|
||
| // generatePreDropMaterializedViewsSQL drops materialized views that depend on | ||
|
|
@@ -3085,7 +3085,7 @@ func (d *ddlDiff) generateCreateSQL(targetSchema string, collector *diffCollecto | |
|
|
||
| // generateModifySQL generates ALTER statements | ||
| // preDroppedViews contains views that were already dropped in the pre-drop phase | ||
| func (d *ddlDiff) generateModifySQL(targetSchema string, collector *diffCollector, preDroppedViews map[string]bool) { | ||
| func (d *ddlDiff) generateModifySQL(targetSchema string, collector *diffCollector, preDroppedViews map[string]bool, typeDropsAfterTables []droppedTypeOrFunction) { | ||
| // Modify schemas | ||
| // Note: Schema modification is out of scope for schema-level comparisons | ||
|
|
||
|
|
@@ -3105,6 +3105,10 @@ func (d *ddlDiff) generateModifySQL(targetSchema string, collector *diffCollecto | |
| // Columns inheritance descendants drop once every parent has (see inherit.go) | ||
| generateInheritanceChildDropsSQL(d.inheritanceChildEffects, targetSchema, collector) | ||
|
|
||
| // Drop the types held back by generateDropSQL, now that the columns using | ||
| // them have been dropped or changed to another type (issue #710) | ||
| generateDropTypesAndFunctionsSQL(typeDropsAfterTables, nil, targetSchema, collector) | ||
|
|
||
| // Attach OWNED BY for explicitly created sequences whose owning column was | ||
| // created by this migration, either with a new table (create phase) or by | ||
| // ALTER TABLE ... ADD COLUMN just above. The sequence itself was created | ||
|
|
@@ -3207,7 +3211,11 @@ func (d *ddlDiff) generateModifySQL(targetSchema string, collector *diffCollecto | |
|
|
||
| // generateDropSQL generates DROP statements in reverse dependency order | ||
| // preDroppedViews contains views that were already dropped in the pre-drop phase | ||
| func (d *ddlDiff) generateDropSQL(targetSchema string, collector *diffCollector, preDroppedViews map[string]bool) { | ||
| // generateDropSQL emits the drop phase. It returns the type drops (with the | ||
| // functions those types reference) that must wait for the modify phase, | ||
| // because a column of a modified table still uses the type until that table's | ||
| // ALTER TABLE has run (issue #710). | ||
| func (d *ddlDiff) generateDropSQL(targetSchema string, collector *diffCollector, preDroppedViews map[string]bool) []droppedTypeOrFunction { | ||
|
|
||
| // REVOKE privileges BEFORE dropping objects (objects must exist for REVOKE to succeed) | ||
| // Skip REVOKEs on relations already dropped in the pre-drop phase: REVOKE has | ||
|
|
@@ -3297,7 +3305,32 @@ func (d *ddlDiff) generateDropSQL(targetSchema string, collector *diffCollector, | |
| // Drop types, interleaved with the held-back functions in dependency | ||
| // order: a range before its subtype_diff function, that function before | ||
| // the subtype its signature takes, a domain before its CHECK function. | ||
| for _, node := range orderTypeAndFunctionDrops(droppedTypes, functionsAfterTypes) { | ||
| // A dropped type that a modified table's column still uses waits, along | ||
| // with whatever must be dropped after it, until that table has been | ||
| // altered (issue #710). The old definition of a replaced type cannot | ||
| // wait: its new definition is created in the create phase. | ||
| usedByColumns := typesUsedByAlteredColumns(d.droppedTypes, d.modifiedTables) | ||
| now, held := orderTypeAndFunctionDrops(droppedTypes, functionsAfterTypes, usedByColumns) | ||
| var afterTables []droppedTypeOrFunction | ||
| for _, node := range held { | ||
| if _, ok := replacedOld[node.typeObj]; ok && node.typeObj != nil { | ||
| now = append(now, node) | ||
| } else { | ||
| afterTables = append(afterTables, node) | ||
|
Comment on lines
+3315
to
+3319
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Knowledge Base Used: Migration planning and diffing
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This scenario isn't made worse here: before this PR the domain was dropped ahead of the column and failed the same way. Ordering it correctly needs the old range dropped after the modify phase but before its replacement is created, which the current phase layout can't express. I'd rather leave that for a follow-up than widen this fix.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That’s a fair distinction. This dependency chain already failed before the PR because Tip: You can customize Greptile's behavior for this repo with |
||
| } | ||
| } | ||
| generateDropTypesAndFunctionsSQL(now, replacedOld, targetSchema, collector) | ||
|
|
||
| // Drop schemas | ||
| // Note: Schema deletion is out of scope for schema-level comparisons | ||
|
|
||
| return afterTables | ||
| } | ||
|
|
||
| // generateDropTypesAndFunctionsSQL emits the drops in the given order. The | ||
| // old definition of a replaced type is reported against its diff. | ||
| func generateDropTypesAndFunctionsSQL(nodes []droppedTypeOrFunction, replacedOld map[*ir.Type]*typeDiff, targetSchema string, collector *diffCollector) { | ||
| for _, node := range nodes { | ||
| if node.function != nil { | ||
| generateDropFunctionsSQL([]*ir.Function{node.function}, targetSchema, collector) | ||
| continue | ||
|
|
@@ -3308,9 +3341,41 @@ func (d *ddlDiff) generateDropSQL(targetSchema string, collector *diffCollector, | |
| } | ||
| generateDropTypeSQL(node.typeObj, source, targetSchema, collector) | ||
| } | ||
| } | ||
|
|
||
| // Drop schemas | ||
| // Note: Schema deletion is out of scope for schema-level comparisons | ||
| // typesUsedByAlteredColumns returns the dropped types that a column of a | ||
| // modified table uses in the current state: a column the migration drops, or | ||
| // one it changes to another type. Such a type can only be dropped once the | ||
| // table has been altered. | ||
| func typesUsedByAlteredColumns(droppedTypes []*ir.Type, modifiedTables []*tableDiff) map[*ir.Type]bool { | ||
| if len(droppedTypes) == 0 || len(modifiedTables) == 0 { | ||
| return nil | ||
| } | ||
| byKey := make(map[string]*ir.Type, len(droppedTypes)) | ||
| for _, t := range droppedTypes { | ||
| byKey[typeGraphKey(t.Schema, t.Name)] = t | ||
| // A column typed as a range's multirange depends on the range. | ||
| if t.Kind == ir.TypeKindRange && t.MultirangeTypeName != "" { | ||
| if key := extractTypeName(t.MultirangeTypeName, t.Schema); key != "" { | ||
| byKey[key] = t | ||
| } | ||
| } | ||
| } | ||
| used := make(map[*ir.Type]bool) | ||
| mark := func(table *ir.Table, column *ir.Column) { | ||
| if t, ok := byKey[extractTypeName(column.DataType, table.Schema)]; ok { | ||
|
Comment on lines
+3354
to
+3366
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Knowledge Base Used: Migration dependency ordering
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here: this combination already failed before the PR (the type drop ran before the column drop). Fixing it means deferring creates that reuse a dropped type's name, which is out of scope for this fix. Happy to open a follow-up issue.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense. This combination already failed before this PR, and handling creates that reuse a dropped type’s name would require broader phase reordering, so I agree it’s out of scope here. The updated lookup does cover multirange names (including arrays); I’ll withdraw this finding and leave the create-ordering case for a follow-up. |
||
| used[t] = true | ||
| } | ||
| } | ||
| for _, td := range modifiedTables { | ||
| for _, column := range td.DroppedColumns { | ||
| mark(td.Table, column) | ||
| } | ||
| for _, columnDiff := range td.ModifiedColumns { | ||
| mark(td.Table, columnDiff.Old) | ||
| } | ||
| } | ||
| return used | ||
| } | ||
|
|
||
| // filterPreDroppedViews returns views that haven't been pre-dropped | ||
|
|
@@ -4756,7 +4821,10 @@ type droppedTypeOrFunction struct { | |
| // signature uses (that subtype_diff takes the subtype), and a caller before | ||
| // its callee. Ties break with types first, then by name, so the output stays | ||
| // deterministic. | ||
| func orderTypeAndFunctionDrops(types []*ir.Type, functions []*ir.Function) []droppedTypeOrFunction { | ||
| // | ||
| // The held types, and everything that has to be dropped after one of them, | ||
| // are returned separately (in the same order) for the caller to emit later. | ||
| func orderTypeAndFunctionDrops(types []*ir.Type, functions []*ir.Function, held map[*ir.Type]bool) (now, later []droppedTypeOrFunction) { | ||
| typeMap, _, typeEdges := buildTypeGraph(types) | ||
| typeKey := func(t *ir.Type) string { return "t\x00" + typeGraphKey(t.Schema, t.Name) } | ||
| fnKey := func(f *ir.Function) string { return "f\x00" + f.Schema + "." + f.Name + "(" + f.GetArguments() + ")" } | ||
|
|
@@ -4817,16 +4885,35 @@ func orderTypeAndFunctionDrops(types []*ir.Type, functions []*ir.Function) []dro | |
| } | ||
| } | ||
|
|
||
| // Holding a node holds everything that must be dropped after it. | ||
| heldKeys := make(map[string]bool) | ||
| var queue []string | ||
| for _, t := range types { | ||
| if held[t] { | ||
| heldKeys[typeKey(t)] = true | ||
| queue = append(queue, typeKey(t)) | ||
| } | ||
| } | ||
| for len(queue) > 0 { | ||
| current := queue[0] | ||
| queue = queue[1:] | ||
| for _, next := range after[current] { | ||
| if _, exists := nodes[next]; exists && !heldKeys[next] { | ||
| heldKeys[next] = true | ||
| queue = append(queue, next) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| var ready []string | ||
| for key := range nodes { | ||
| if inDegree[key] == 0 { | ||
| ready = append(ready, key) | ||
| } | ||
| } | ||
| sort.Strings(ready) // "f\x00" sorts before "t\x00"; reversed below so types lead | ||
| var result []droppedTypeOrFunction | ||
| done := make(map[string]bool, len(nodes)) | ||
| for len(result) < len(nodes) { | ||
| for len(done) < len(nodes) { | ||
| if len(ready) == 0 { | ||
| // A cycle cannot occur in PostgreSQL's catalog; keep going | ||
| // deterministically rather than drop nodes on the floor. | ||
|
|
@@ -4845,7 +4932,11 @@ func orderTypeAndFunctionDrops(types []*ir.Type, functions []*ir.Function) []dro | |
| continue | ||
| } | ||
| done[current] = true | ||
| result = append(result, nodes[current]) | ||
| if heldKeys[current] { | ||
| later = append(later, nodes[current]) | ||
| } else { | ||
| now = append(now, nodes[current]) | ||
| } | ||
| for _, next := range after[current] { | ||
| inDegree[next]-- | ||
| if inDegree[next] <= 0 && !done[next] { | ||
|
|
@@ -4854,7 +4945,7 @@ func orderTypeAndFunctionDrops(types []*ir.Type, functions []*ir.Function) []dro | |
| } | ||
| } | ||
| } | ||
| return result | ||
| return now, later | ||
| } | ||
|
|
||
| // typeReferencesNewFunction determines if a domain references any newly added | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,11 @@ | ||
| ALTER TABLE tickets DROP COLUMN priority; | ||
|
|
||
| ALTER TABLE tickets DROP COLUMN busy; | ||
|
|
||
| ALTER TABLE tickets ALTER COLUMN severity TYPE text USING severity::text; | ||
|
|
||
| DROP TYPE IF EXISTS severity RESTRICT; | ||
|
|
||
| DROP TYPE IF EXISTS priority RESTRICT; | ||
|
|
||
| DROP TYPE IF EXISTS period RESTRICT; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,11 @@ | ||
| ALTER TABLE tickets DROP COLUMN priority; | ||
|
|
||
| ALTER TABLE tickets DROP COLUMN busy; | ||
|
|
||
| ALTER TABLE tickets ALTER COLUMN severity TYPE text USING severity::text; | ||
|
|
||
| DROP TYPE IF EXISTS severity RESTRICT; | ||
|
|
||
| DROP TYPE IF EXISTS priority RESTRICT; | ||
|
|
||
| DROP TYPE IF EXISTS period RESTRICT; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
priorityand columntickets.priority, a valid desired schema can remove that column and enum and add tablepriority. The plan now tries to create tableprioritywhile the enum still owns that type name. PostgreSQL rejects the creation before the delayed drop can run. Defer creations that reuse these names until the columns and types have been removed.Knowledge Base Used: Migration dependency ordering
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch — fixed in 84f8ecb. A dropped range is now also held for columns typed as its multirange (arrays included), and the
drop_typecase covers it.