Skip to content

Commit 01fb245

Browse files
committed
Fix rename bug
Signed-off-by: Morgan Douglas <[email protected]> . Signed-off-by: Morgan Douglas <[email protected]> . Signed-off-by: Morgan Douglas <[email protected]> . Signed-off-by: Morgan Douglas <[email protected]>
1 parent 2dc04c8 commit 01fb245

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

schemachange/sc_rename_table.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,15 @@ int finalize_alias_table(struct ireq *iq, struct schema_change_type *s,
8383
int finalize_rename_table(struct ireq *iq, struct schema_change_type *s,
8484
tran_type *tran)
8585
{
86-
struct dbtable *db = s->db;
87-
char *newname = strdup(s->newtable);
88-
int rc = 0;
89-
int bdberr = 0;
90-
char *oldname = NULL;
91-
9286
assert(s->kind == SC_RENAMETABLE);
9387

88+
struct dbtable *db = s->db;
89+
if (db->n_rev_constraints > 0) {
90+
sc_client_error(s, "Cannot rename a table referenced by a foreign key");
91+
return -1;
92+
}
93+
94+
char *newname = strdup(s->newtable);
9495
if (!newname) {
9596
sc_errf(s, "strdup error\n");
9697
return -1;
@@ -103,7 +104,8 @@ int finalize_rename_table(struct ireq *iq, struct schema_change_type *s,
103104
s->already_finalized = 1;
104105

105106
/* renamed table schema gets bumped */
106-
rc = table_version_upsert(db, tran, &bdberr);
107+
int bdberr = 0;
108+
int rc = table_version_upsert(db, tran, &bdberr);
107109
if (rc) {
108110
sc_errf(s, "Failed updating table version bdberr %d\n", bdberr);
109111
goto tran_error;
@@ -133,7 +135,7 @@ int finalize_rename_table(struct ireq *iq, struct schema_change_type *s,
133135
}
134136

135137
/* fragile, handle with care */
136-
oldname = db->tablename;
138+
char *oldname = db->tablename;
137139
rc = rename_db(db, newname);
138140
if (rc) {
139141
/* crash the schema change, next master will hopefully have more memory

tests/constraints.test/t17.req

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
drop table if exists foo
2+
drop table if exists bar
3+
drop table if exists baz
4+
create table bar {schema{int i int j} keys{"pk" = i+j}} $$
5+
begin
6+
create table foo {schema{int i int j} keys{dup "key" = i+j} constraints{"key" -> "bar":"pk" on delete cascade }} $$
7+
alter table bar rename to baz$$
8+
commit

tests/constraints.test/t17.req.exp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[drop table if exists foo] rc 0
2+
[drop table if exists bar] rc 0
3+
[drop table if exists baz] rc 0
4+
[create table bar {schema{int i int j} keys{"pk" = i+j}}] rc 0
5+
[begin] rc 0
6+
[create table foo {schema{int i int j} keys{dup "key" = i+j} constraints{"key" -> "bar":"pk" on delete cascade }}] rc 0
7+
[alter table bar rename to baz] rc 0
8+
[commit] failed with rc 240 Cannot rename a table referenced by a foreign key

0 commit comments

Comments
 (0)