Skip to content

Commit

Permalink
sql: check FK back-references in ALTER COLUMN TYPE
Browse files Browse the repository at this point in the history
Previously, ALTER TABLE ... ALTER COLUMN ... TYPE ... statements would
execute regardless if the column is also the target of a foreign key
constraint on another table. This commit fixes this bug.

Fixes cockroachdb#71709.

Release note (bug fix): fixes a bug in which an ALTER TABLE ... ALTER
COLUMN ... TYPE ... statement could execute despite the column being the
target of a foreign key constraint on another table.
  • Loading branch information
Marius Posta committed Oct 19, 2021
1 parent 0e2e645 commit 8a8a32d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
12 changes: 11 additions & 1 deletion pkg/sql/alter_column_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func alterColumnTypeGeneral(

// Disallow ALTER COLUMN TYPE general for columns that have a foreign key
// constraint.
for _, fk := range tableDesc.AllActiveAndInactiveForeignKeys() {
checkFK := func(fk *descpb.ForeignKeyConstraint) error {
if fk.OriginTableID == tableDesc.GetID() {
for _, id := range fk.OriginColumnIDs {
if col.GetID() == id {
Expand All @@ -217,6 +217,16 @@ func alterColumnTypeGeneral(
}
}
}
return nil
}

for _, fk := range tableDesc.AllActiveAndInactiveForeignKeys() {
if err := checkFK(fk); err != nil {
return err
}
}
if err := tableDesc.ForeachInboundFK(checkFK); err != nil {
return err
}

// Disallow ALTER COLUMN TYPE general for columns that are
Expand Down
5 changes: 4 additions & 1 deletion pkg/sql/logictest/testdata/logic_test/alter_column_type
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ statement ok
CREATE TABLE t18 (x INT NOT NULL PRIMARY KEY);
CREATE TABLE t19 (y INT NOT NULL REFERENCES t18 (x), INDEX(y));

statement error pq: unimplemented: ALTER COLUMN TYPE requiring rewrite of on-disk data is currently not supported for columns that are part of an index
statement error pq: unimplemented: ALTER COLUMN TYPE for a column that has a constraint is currently not supported
ALTER TABLE t18 ALTER COLUMN x TYPE STRING

statement error pq: unimplemented: ALTER COLUMN TYPE for a column that has a constraint is currently not supported
Expand Down Expand Up @@ -529,6 +529,9 @@ CREATE TABLE child_71089 (a INT, b INT REFERENCES parent_71089 (id) NOT NULL)
statement ok
ALTER TABLE child_71089 ALTER COLUMN a TYPE FLOAT;

statement error pq: unimplemented: ALTER COLUMN TYPE for a column that has a constraint is currently not supported
ALTER TABLE parent_71089 ALTER COLUMN id TYPE uuid USING gen_random_uuid();

# ColumnConversionValidate should error out if the conversion is not valid.
# STRING -> BYTES is a ColumnConversionValidate type conversion, it should
# try the conversion and error out if the cast cannot be applied.
Expand Down

0 comments on commit 8a8a32d

Please sign in to comment.