Skip to content
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
2 changes: 1 addition & 1 deletion internal/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -1644,7 +1644,7 @@ func (d *ddlDiff) generateModifySQL(targetSchema string, collector *diffCollecto
generateModifySequencesSQL(d.modifiedSequences, targetSchema, collector)

// Modify tables
generateModifyTablesSQL(d.modifiedTables, targetSchema, collector)
generateModifyTablesSQL(d.modifiedTables, d.droppedTables, targetSchema, collector)

// Find views that depend on views being recreated (issue #268, #308)
// Handles both materialized views and regular views with RequiresRecreate
Expand Down
23 changes: 20 additions & 3 deletions internal/diff/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,11 +536,17 @@ func generateDeferredConstraintsSQL(deferred []*deferredConstraint, targetSchema
}

// generateModifyTablesSQL generates ALTER TABLE statements
func generateModifyTablesSQL(diffs []*tableDiff, targetSchema string, collector *diffCollector) {
func generateModifyTablesSQL(diffs []*tableDiff, droppedTables []*ir.Table, targetSchema string, collector *diffCollector) {
// Build a set of tables being dropped (CASCADE will remove their dependent FK constraints)
droppedTableSet := make(map[string]bool, len(droppedTables))
for _, t := range droppedTables {
droppedTableSet[t.Schema+"."+t.Name] = true
}

// Diffs are already sorted by the Diff operation
for _, diff := range diffs {
// Pass collector to generateAlterTableStatements to collect with proper context
diff.generateAlterTableStatements(targetSchema, collector)
diff.generateAlterTableStatements(targetSchema, collector, droppedTableSet)
}
}

Expand Down Expand Up @@ -654,9 +660,20 @@ func shouldDeferConstraint(table *ir.Table, constraint *ir.Constraint, currentKe
// generateAlterTableStatements generates SQL statements for table modifications
// Note: DroppedTriggers are skipped here because they are already processed in the DROP phase
// (see generateDropTriggersFromModifiedTables in trigger.go)
func (td *tableDiff) generateAlterTableStatements(targetSchema string, collector *diffCollector) {
// droppedTableSet contains "schema.table" keys for tables being dropped with CASCADE;
// FK constraints referencing these tables are skipped since CASCADE already removes them.
func (td *tableDiff) generateAlterTableStatements(targetSchema string, collector *diffCollector, droppedTableSet map[string]bool) {
// Drop constraints first (before dropping columns) - already sorted by the Diff operation
for _, constraint := range td.DroppedConstraints {
// Skip FK constraints whose referenced table is being dropped with CASCADE,
// since the CASCADE will already remove the constraint. (#382)
if constraint.Type == ir.ConstraintTypeForeignKey && constraint.ReferencedTable != "" {
refKey := constraint.ReferencedSchema + "." + constraint.ReferencedTable
if droppedTableSet[refKey] {
continue
}
Comment thread
tianzhou marked this conversation as resolved.
}
Comment thread
tianzhou marked this conversation as resolved.

tableName := getTableNameWithSchema(td.Table.Schema, td.Table.Name, targetSchema)
sql := fmt.Sprintf("ALTER TABLE %s DROP CONSTRAINT %s;", tableName, ir.QuoteIdentifier(constraint.Name))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DROP TABLE IF EXISTS b CASCADE;

ALTER TABLE a DROP COLUMN b_id;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE a (id bigint PRIMARY KEY);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE a (id bigint PRIMARY KEY, b_id bigint);

CREATE TABLE b (id bigint PRIMARY KEY);

ALTER TABLE a ADD CONSTRAINT "a_b_id_fkey" FOREIGN KEY ("b_id") REFERENCES "b" ("id");
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"version": "1.0.0",
"pgschema_version": "1.8.0",
"created_at": "1970-01-01T00:00:00Z",
"source_fingerprint": {
"hash": "62dd4ce9d3f9c6521a16cdbf81cf9511ce4c5a18c65272f5dfa306bfe476cad3"
},
"groups": [
{
"steps": [
{
"sql": "DROP TABLE IF EXISTS b CASCADE;",
"type": "table",
"operation": "drop",
"path": "public.b"
},
{
"sql": "ALTER TABLE a DROP COLUMN b_id;",
"type": "table.column",
"operation": "drop",
"path": "public.a.b_id"
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DROP TABLE IF EXISTS b CASCADE;

ALTER TABLE a DROP COLUMN b_id;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Plan: 1 to modify, 1 to drop.

Summary by type:
tables: 1 to modify, 1 to drop

Tables:
~ a
- b_id (column)
- b

DDL to be executed:
--------------------------------------------------

DROP TABLE IF EXISTS b CASCADE;

ALTER TABLE a DROP COLUMN b_id;
Loading