Skip to content

Commit

Permalink
sql: ban alter column type on view-depended cols
Browse files Browse the repository at this point in the history
Previously, it was legal to edit the column type of columns that were
depended on by views. This is not a good idea for various reasons, and
is not Postgres compatible to boot.

Now, it is illegal to alter the column type of columns that are depended
on views.

Release note (sql change): prevent column type modification of columns
that are depended on by views.
  • Loading branch information
jordanlewis committed Nov 3, 2020
1 parent 460c497 commit bc642d0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/sql/alter_column_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ func AlterColumnType(
cmds tree.AlterTableCmds,
tn *tree.TableName,
) error {
for _, tableRef := range tableDesc.DependedOnBy {
found := false
for _, colID := range tableRef.ColumnIDs {
if colID == col.ID {
found = true
}
}
if found {
return params.p.dependentViewError(
ctx, "column", col.Name, tableDesc.ParentID, tableRef.ID, "alter type of",
)
}
}

typ, err := tree.ResolveType(ctx, t.ToType, params.p.semaCtx.GetTypeResolver())
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/alter_column_type
Original file line number Diff line number Diff line change
Expand Up @@ -546,3 +546,20 @@ INSERT INTO t30 VALUES (e'a\\01');

statement error pq: could not parse "a\\\\01" as type bytes: bytea encoded value ends with incomplete escape sequence
ALTER TABLE t30 ALTER COLUMN x TYPE BYTES

# Ensure that dependent views prevent column type modification.

statement ok
CREATE VIEW v AS SELECT x FROM t29

statement error cannot alter type of column "x" because view "v" depends on it
ALTER TABLE t29 ALTER COLUMN x TYPE INT2

statement ok
DROP VIEW v

statement ok
CREATE MATERIALIZED VIEW v AS SELECT x FROM t29

statement error cannot alter type of column "x" because view "v" depends on it
ALTER TABLE t29 ALTER COLUMN x TYPE INT2

0 comments on commit bc642d0

Please sign in to comment.