Repository navigation
fix: drop a type after the columns of modified tables that use it (#710) - #711
augustoribeiro wants to merge 2 commits into
Conversation
…plex#710) A type dropped together with a column that uses it (or with that column changing to another type) was dropped in the drop phase, before the ALTER TABLE of the modify phase, and failed with SQLSTATE 2BP01. Such types, and whatever has to be dropped after them, now wait until the modified tables have been altered. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
[Medium impact] The change affects generated database migration plans when altered tables still use dropped types. The drop ordering needs fixes before merging so these valid schema changes can be applied. Findings
SummaryDefers type drops until after existing tables release their columns, while preserving the order of related function drops.
Diagram%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Find types still used by changing columns] --> B[Drop other obsolete objects]
B --> C[Create new objects]
C --> D[Alter existing tables]
D --> E[Drop delayed types and related functions]
E --> F[Finish remaining changes]
Reviews (1) · Last reviewed commit: "fix: drop a type after the columns of mo..." · Reviewed by Greptile |
| byKey := make(map[string]*ir.Type, len(droppedTypes)) | ||
| for _, t := range droppedTypes { | ||
| byKey[typeGraphKey(t.Schema, t.Name)] = 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 { |
There was a problem hiding this comment.
Multirange columns still block drops.
byKey contains only each range's name, but the inspector stores multirange columns under MultirangeTypeName. For example, removing a period_multirange column and its owning range period never marks period as held. The plan still emits DROP TYPE period before ALTER TABLE ... DROP COLUMN, causing the dependency error this PR fixes. Map multirange names, including their array forms, to their owning range before looking up columns.
Knowledge Base Used: Migration dependency ordering
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| for _, node := range held { | ||
| if _, ok := replacedOld[node.typeObj]; ok && node.typeObj != nil { | ||
| now = append(now, node) | ||
| } else { | ||
| afterTables = append(afterTables, node) |
There was a problem hiding this comment.
Replaced ranges drop too early. Moving a replaced range back into
now breaks the order established by orderTypeAndFunctionDrops. For example, an old domain slot can wrap range span, with a table column using slot. If the desired schema removes that column and domain while changing span's subtype, both types enter held, but this branch moves only span back. The plan drops span while slot still depends on it and fails. Keep the column removal, dependent drops, and range replacement in dependency order rather than moving the range alone.
Knowledge Base Used: Migration planning and diffing
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
That’s a fair distinction. This dependency chain already failed before the PR because slot was dropped while the column still used it; the new ordering changes where it fails, but doesn’t introduce a newly failing scenario. Fixing it properly would require a phase that drops the old span after the table alteration and before creating its replacement, which is beyond this change’s scope. I’ll withdraw the comment and leave it for a follow-up.
Tip: You can customize Greptile's behavior for this repo with .greptile/rules.md and .greptile/config.json.
|
|
||
| // 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) |
There was a problem hiding this comment.
Reused type names block tables. The delayed drops run after every new table is created. If the old schema has enum
priority and column tickets.priority, a valid desired schema can remove that column and enum and add table priority. The plan now tries to create table priority while 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.
Good catch — fixed in 84f8ecb. A dropped range is now also held for columns typed as its multirange (arrays included), and the drop_type case covers it.
…#710) Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A type dropped together with a column that uses it (or with that column changing to another type) was dropped in the drop phase, before the ALTER TABLE of the modify phase, and failed with SQLSTATE 2BP01. Such types, and whatever has to be dropped after them, now wait until the modified tables have been altered.
Fixes #710