Skip to content

Commit 9a19eae

Browse files
committed
workload/schemachanger: fix dependency detection for DROP COLUMN
Previously, the DROP COLUMN operation in the schema changer could fail, since it detected self referential foreign key dependencies as external ones. To address this, this patch excludes them from the detection. Fixes: #160318 Fixes: #159952 Release note: None
1 parent 7b9a1c7 commit 9a19eae

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

pkg/workload/schemachange/error_screening.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func (og *operationGenerator) tableHasDependencies(
150150
}
151151

152152
func (og *operationGenerator) columnIsDependedOn(
153-
ctx context.Context, tx pgx.Tx, tableName *tree.TableName, columnName tree.Name,
153+
ctx context.Context, tx pgx.Tx, tableName *tree.TableName, columnName tree.Name, includeFKs bool,
154154
) (bool, error) {
155155
// To see if a column is depended on, the ordinal_position of the column is looked up in
156156
// information_schema.columns. Then, this position is used to see if that column has view dependencies
@@ -187,6 +187,7 @@ func (og *operationGenerator) columnIsDependedOn(
187187
WHERE fd.descriptor_id
188188
= $1::REGCLASS
189189
AND fd.dependedonby_type != 'sequence'
190+
AND ($5::BOOL || fd.dependedonby_type != 'fk')
190191
)
191192
UNION (
192193
SELECT unnest(confkey) AS column_id
@@ -208,7 +209,7 @@ func (og *operationGenerator) columnIsDependedOn(
208209
AND column_name = $4
209210
) AS source ON source.column_id = cons.column_id
210211
)
211-
`, tableName.String(), tableName.Schema(), tableName.Object(), columnName)
212+
`, tableName.String(), tableName.Schema(), tableName.Object(), columnName, includeFKs)
212213
}
213214

214215
// colIsRefByComputed determines if a column is referenced by a computed column.

pkg/workload/schemachange/operation_generator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,7 +1713,7 @@ func (og *operationGenerator) dropColumn(ctx context.Context, tx pgx.Tx) (*opStm
17131713
if err != nil {
17141714
return nil, err
17151715
}
1716-
columnIsDependedOn, err := og.columnIsDependedOn(ctx, tx, tableName, columnName)
1716+
columnIsDependedOn, err := og.columnIsDependedOn(ctx, tx, tableName, columnName, false /* includeFKs */)
17171717
if err != nil {
17181718
return nil, err
17191719
}
@@ -2675,7 +2675,7 @@ func (og *operationGenerator) setColumnType(ctx context.Context, tx pgx.Tx) (*op
26752675
return nil, err
26762676
}
26772677

2678-
columnHasDependencies, err := og.columnIsDependedOn(ctx, tx, tableName, columnForTypeChange.name)
2678+
columnHasDependencies, err := og.columnIsDependedOn(ctx, tx, tableName, columnForTypeChange.name, true /* includeFKs */)
26792679
if err != nil {
26802680
return nil, err
26812681
}

0 commit comments

Comments
 (0)