Skip to content

Commit 8368922

Browse files
committed
schemachanger: implement ALTER TABLE .. RENAME in declarative schema changer
This preserves all the validations and error handling from the legacy schema changer. Release note: None
1 parent 7d65e38 commit 8368922

File tree

20 files changed

+522
-6
lines changed

20 files changed

+522
-6
lines changed

pkg/ccl/changefeedccl/alter_changefeed_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,8 @@ func TestAlterChangefeedAlterTableName(t *testing.T) {
12731273

12741274
expectResolvedTimestamp(t, testFeed)
12751275

1276-
waitForSchemaChange(t, sqlDB, `ALTER TABLE movr.users RENAME TO movr.riders`)
1276+
sqlDB.Exec(t, `ALTER TABLE movr.users RENAME TO movr.riders`)
1277+
sqlDB.CheckQueryResultsRetry(t, "SELECT count(*) FROM [SHOW TABLES FROM movr] WHERE table_name = 'riders'", [][]string{{"1"}})
12771278

12781279
var tsLogical string
12791280
sqlDB.QueryRow(t, `SELECT cluster_logical_timestamp()`).Scan(&tsLogical)

pkg/ccl/schemachangerccl/sctestbackupccl/backup_base_generated_test.go

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/sql/drop_function_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,9 @@ USE defaultdb;
244244
dscExpectedErr: `pq: cannot drop view v because other objects depend on it`,
245245
},
246246
{
247-
stmt: "ALTER TABLE t RENAME TO t_new",
248-
expectedErr: `pq: cannot rename relation "t" because function "f" depends on it`,
247+
stmt: "ALTER TABLE t RENAME TO t_new",
248+
expectedErr: `pq: cannot rename relation "t" because function "f" depends on it`,
249+
dscExpectedErr: `pq: cannot rename relation "defaultdb.public.t" because function "f" depends on it`,
249250
},
250251
{
251252
stmt: "ALTER TABLE t SET SCHEMA test_sc",

pkg/sql/logictest/testdata/logic_test/rename_sequence

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,16 @@ DROP SEQUENCE renamed_again_alter_test
2121
statement ok
2222
CREATE SEQUENCE foo
2323

24+
statement error pgcode 42809 "foo" is not a view
25+
ALTER VIEW foo RENAME TO bar
26+
27+
# The legacy schema changer was more strict about using ALTER TABLE to rename
28+
# a sequence. Since Postgres allows that, the declarative schema changer
29+
# supports it.
30+
onlyif config local-legacy-schema-changer local-mixed-25.2 local-mixed-25.3
2431
statement error pgcode 42809 "foo" is not a table
2532
ALTER TABLE foo RENAME TO bar
2633

27-
statement error pgcode 42809 "foo" is not a view
28-
ALTER VIEW foo RENAME TO bar
34+
skipif config local-legacy-schema-changer local-mixed-25.2 local-mixed-25.3
35+
statement ok
36+
ALTER TABLE foo RENAME TO bar

pkg/sql/logictest/testdata/logic_test/rename_view

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,14 @@ SELECT * FROM test.v2
165165
statement ok
166166
CREATE VIEW v3 AS SELECT count(*) FROM test.v AS v JOIN test.v2 AS v2 ON v.k > v2.c1
167167

168+
# onlyif config local-legacy-schema-changer
168169
statement error cannot rename relation "test.public.v" because view "v3" depends on it
169170
ALTER VIEW test.v RENAME TO test.v3
170171

172+
# skipif config local-legacy-schema-changer
173+
# statement error relation "test.public.v3" already exists
174+
# ALTER VIEW test.v RENAME TO test.v3
175+
171176
statement error cannot rename relation "test.public.v2" because view "v3" depends on it
172177
ALTER VIEW test.v2 RENAME TO v4
173178

pkg/sql/logictest/testdata/logic_test/schema_repair

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ FROM
341341
WHERE
342342
name = 'kv'
343343

344-
statement error pgcode XX000 internal error: relation "kv" \(\d+\): column "k" invalid ID \(1\) >= next column ID \(1\)
344+
statement error pgcode XX000 internal error:.* relation "kv" \(\d+\): column "k" invalid ID \(1\) >= next column ID \(1\)
345345
ALTER TABLE kv RENAME TO kv
346346

347347
statement ok

pkg/sql/logictest/testdata/logic_test/udf_star

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ statement error pgcode 2BP01 pq: cannot rename column "a" because function "f_un
204204
ALTER TABLE t_twocol RENAME COLUMN a TO d;
205205

206206
# TODO(#101934): Postgres allows table renaming when only referenced by UDFs.
207+
skipif config local-legacy-schema-changer local-mixed-25.2 local-mixed-25.3
208+
statement error pgcode 2BP01 pq: cannot rename relation "test.public.t_twocol" because function "f_unqualified_twocol" depends on it
209+
ALTER TABLE t_twocol RENAME TO t_twocol_prime;
210+
211+
onlyif config local-legacy-schema-changer local-mixed-25.2 local-mixed-25.3
207212
statement error pgcode 2BP01 pq: cannot rename relation "t_twocol" because function "f_unqualified_twocol" depends on it
208213
ALTER TABLE t_twocol RENAME TO t_twocol_prime;
209214

pkg/sql/schema_changer_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4777,6 +4777,7 @@ func TestBlockedSchemaChange(t *testing.T) {
47774777
codec := s.ApplicationLayer().Codec()
47784778
sqlDB := sqlutils.MakeSQLRunner(db)
47794779

4780+
sqlDB.Exec(t, `SET CLUSTER SETTING sql.defaults.use_declarative_schema_changer = 'off'`)
47804781
sqlDB.Exec(t, `
47814782
SET create_table_with_schema_locked=false;
47824783
SET use_declarative_schema_changer='off';

pkg/sql/schemachanger/scbuild/event_log.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@ func (pb payloadBuilder) droppedSchemaObjects(b buildCtx) (ret []string) {
340340
func (pb payloadBuilder) build(b buildCtx) logpb.EventPayload {
341341
const mutationID = 1
342342
switch e := pb.Element().(type) {
343+
case *scpb.Namespace:
344+
if pb.maybePayload != nil {
345+
return pb.maybePayload
346+
}
343347
case *scpb.Database:
344348
if pb.TargetStatus == scpb.Status_PUBLIC {
345349
return &eventpb.CreateDatabase{

pkg/sql/schemachanger/scbuild/internal/scbuildstmt/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ go_library(
4646
"partition_helpers.go",
4747
"partition_zone_config.go",
4848
"process.go",
49+
"rename_table.go",
4950
"statement_control.go",
5051
"table_zone_config.go",
5152
"truncate.go",

0 commit comments

Comments
 (0)