Changeset: b3f35f11a223 for MonetDB URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=b3f35f11a223 Modified Files: sql/backends/monet5/sql_cat.c sql/server/rel_schema.c sql/test/rename/Tests/rename01.sql sql/test/rename/Tests/rename01.stable.err sql/test/rename/Tests/rename01.stable.out sql/test/rename/Tests/rename05.sql sql/test/rename/Tests/rename05.stable.err sql/test/rename/Tests/rename05.stable.out Branch: rename-sql Log Message:
Allow to rename SQL objects only when there are no dependencies on them. diffs (truncated from 308 to 300 lines): diff --git a/sql/backends/monet5/sql_cat.c b/sql/backends/monet5/sql_cat.c --- a/sql/backends/monet5/sql_cat.c +++ b/sql/backends/monet5/sql_cat.c @@ -1570,6 +1570,8 @@ SQLrename_schema(Client cntxt, MalBlkPtr throw(SQL, "sql.rename_schema", SQLSTATE(3F000) "ALTER SCHEMA: access denied for %s to schema '%s'", stack_get_string(sql, "current_user"), old_name); if (s->system) throw(SQL, "sql.rename_schema", SQLSTATE(3F000) "ALTER SCHEMA: cannot rename a system schema"); + if (!list_empty(s->tables.set) || !list_empty(s->types.set) || !list_empty(s->funcs.set) || !list_empty(s->seqs.set)) + throw(SQL, "sql.rename_schema", SQLSTATE(2BM37) "ALTER SCHEMA: unable to rename schema '%s' (there are database objects which depend on it)", old_name); if (!new_name || strcmp(new_name, str_nil) == 0) throw(SQL, "sql.rename_schema", SQLSTATE(3F000) "ALTER SCHEMA: invalid new schema name"); if (mvc_bind_schema(sql, new_name)) @@ -1603,6 +1605,8 @@ SQLrename_table(Client cntxt, MalBlkPtr throw(SQL, "sql.rename_table", SQLSTATE(42S02) "ALTER TABLE: no such table '%s' in schema '%s'", old_name, schema_name); if (t->system) throw(SQL, "sql.rename_table", SQLSTATE(42000) "ALTER TABLE: cannot rename a system table"); + if (mvc_check_dependency(sql, t->base.id, TABLE_DEPENDENCY, NULL)) + throw (SQL,"sql.rename_table", SQLSTATE(2BM37) "ALTER TABLE: unable to rename table %s (there are database objects which depend on it)", old_name); if (!new_name || strcmp(new_name, str_nil) == 0) throw(SQL, "sql.rename_table", SQLSTATE(3F000) "ALTER TABLE: invalid new table name"); if (mvc_bind_table(sql, s, new_name)) @@ -1624,6 +1628,7 @@ SQLrename_column(Client cntxt, MalBlkPtr str new_name = *getArgReference_str(stk, pci, 4); sql_schema *s; sql_table *t; + sql_column *col; initcontext(); if (!(s = mvc_bind_schema(sql, schema_name))) @@ -1635,13 +1640,15 @@ SQLrename_column(Client cntxt, MalBlkPtr if (t->system) throw(SQL, "sql.rename_column", SQLSTATE(42000) "ALTER TABLE: cannot rename a column in a system table"); if (isView(t)) - throw(SQL, "sql.rename_column", SQLSTATE(42000) "ALTER TABLE: cannot rename column '%s': '%s' is a view", old_name, t->base.name); - if (!mvc_bind_column(sql, t, old_name)) - throw(SQL, "sql.rename_column", SQLSTATE(42S22) "ALTER TABLE: no such column '%s' in table '%s'", old_name, t->base.name); + throw(SQL, "sql.rename_column", SQLSTATE(42000) "ALTER TABLE: cannot rename column '%s': '%s' is a view", old_name, table_name); + if (!(col = mvc_bind_column(sql, t, old_name))) + throw(SQL, "sql.rename_column", SQLSTATE(42S22) "ALTER TABLE: no such column '%s' in table '%s'", old_name, table_name); + if (mvc_check_dependency(sql, col->base.id, COLUMN_DEPENDENCY, NULL)) + throw(SQL, "sql.rename_column", SQLSTATE(2BM37) "ALTER TABLE: cannot rename column '%s' (there are database objects which depend on it)", old_name); if (!new_name || strcmp(new_name, str_nil) == 0) throw(SQL, "sql.rename_column", SQLSTATE(3F000) "ALTER TABLE: invalid new column name"); if (mvc_bind_column(sql, t, new_name)) - throw(SQL, "sql.rename_column", SQLSTATE(3F000) "ALTER TABLE: there is a column named '%s' in table '%s'", new_name, t->base.name); + throw(SQL, "sql.rename_column", SQLSTATE(3F000) "ALTER TABLE: there is a column named '%s' in table '%s'", new_name, table_name); if (!sql_trans_rename_column(sql->session->tr, t, old_name, new_name)) throw(SQL, "sql.rename_column",SQLSTATE(HY001) MAL_MALLOC_FAIL); diff --git a/sql/server/rel_schema.c b/sql/server/rel_schema.c --- a/sql/server/rel_schema.c +++ b/sql/server/rel_schema.c @@ -2449,6 +2449,8 @@ rel_rename_schema(mvc *sql, char *old_na return sql_error(sql, 02, SQLSTATE(3F000) "ALTER SCHEMA: access denied for %s to schema '%s'", stack_get_string(sql, "current_user"), old_name); if (s->system) return sql_error(sql, 02, SQLSTATE(3F000) "ALTER SCHEMA: cannot rename a system schema"); + if (!list_empty(s->tables.set) || !list_empty(s->types.set) || !list_empty(s->funcs.set) || !list_empty(s->seqs.set)) + return sql_error(sql, 02, SQLSTATE(2BM37) "ALTER SCHEMA: unable to rename schema '%s' (there are database objects which depend on it)", old_name); if (!new_name || strcmp(new_name, str_nil) == 0) return sql_error(sql, 02, SQLSTATE(3F000) "ALTER SCHEMA: invalid new schema name"); if (mvc_bind_schema(sql, new_name)) @@ -2475,17 +2477,19 @@ rel_rename_table(mvc *sql, char* schema_ assert(schema_name && old_name && new_name); if (!(s = mvc_bind_schema(sql, schema_name))) - return sql_error(sql, 02,SQLSTATE(42S02) "ALTER TABLE: no such schema '%s'", schema_name); + return sql_error(sql, 02, SQLSTATE(42S02) "ALTER TABLE: no such schema '%s'", schema_name); if (!mvc_schema_privs(sql, s)) - return sql_error(sql, 02,SQLSTATE(42000) "ALTER TABLE: access denied for %s to schema '%s'", stack_get_string(sql, "current_user"), schema_name); + return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: access denied for %s to schema '%s'", stack_get_string(sql, "current_user"), schema_name); if (!(t = mvc_bind_table(sql, s, old_name))) - return sql_error(sql, 02,SQLSTATE(42S02) "ALTER TABLE: no such table '%s' in schema '%s'", old_name, schema_name); + return sql_error(sql, 02, SQLSTATE(42S02) "ALTER TABLE: no such table '%s' in schema '%s'", old_name, schema_name); if (t->system) - return sql_error(sql, 02,SQLSTATE(42000) "ALTER TABLE: cannot rename a system table"); + return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: cannot rename a system table"); + if (mvc_check_dependency(sql, t->base.id, TABLE_DEPENDENCY, NULL)) + return sql_error(sql, 02, SQLSTATE(2BM37) "ALTER TABLE: unable to rename table %s (there are database objects which depend on it)", old_name); if (!new_name || strcmp(new_name, str_nil) == 0) - return sql_error(sql, 02,SQLSTATE(3F000) "ALTER TABLE: invalid new table name"); + return sql_error(sql, 02, SQLSTATE(3F000) "ALTER TABLE: invalid new table name"); if (mvc_bind_table(sql, s, new_name)) - return sql_error(sql, 02,SQLSTATE(3F000) "ALTER TABLE: there is a table named '%s' in schema '%s'", new_name, schema_name); + return sql_error(sql, 02, SQLSTATE(3F000) "ALTER TABLE: there is a table named '%s' in schema '%s'", new_name, schema_name); rel = rel_create(sql->sa); exps = new_exp_list(sql->sa); @@ -2503,6 +2507,7 @@ rel_rename_column(mvc *sql, char* schema { sql_schema *s; sql_table *t; + sql_column *col; sql_rel *rel; list *exps; @@ -2517,13 +2522,15 @@ rel_rename_column(mvc *sql, char* schema if (t->system) return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: cannot rename a column in a system table"); if (isView(t)) - return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: cannot rename column '%s': '%s' is a view", old_name, t->base.name); - if (!mvc_bind_column(sql, t, old_name)) - return sql_error(sql, 02, SQLSTATE(42S22) "ALTER TABLE: no such column '%s' in table '%s'", old_name, t->base.name); + return sql_error(sql, 02, SQLSTATE(42000) "ALTER TABLE: cannot rename column '%s': '%s' is a view", old_name, table_name); + if (!(col = mvc_bind_column(sql, t, old_name))) + return sql_error(sql, 02, SQLSTATE(42S22) "ALTER TABLE: no such column '%s' in table '%s'", old_name, table_name); + if (mvc_check_dependency(sql, col->base.id, COLUMN_DEPENDENCY, NULL)) + return sql_error(sql, 02, SQLSTATE(2BM37) "ALTER TABLE: cannot rename column '%s' (there are database objects which depend on it)", old_name); if (!new_name || strcmp(new_name, str_nil) == 0) return sql_error(sql, 02, SQLSTATE(3F000) "ALTER TABLE: invalid new column name"); if (mvc_bind_column(sql, t, new_name)) - return sql_error(sql, 02, SQLSTATE(3F000) "ALTER TABLE: there is a column named '%s' in table '%s'", new_name, t->base.name); + return sql_error(sql, 02, SQLSTATE(3F000) "ALTER TABLE: there is a column named '%s' in table '%s'", new_name, table_name); rel = rel_create(sql->sa); exps = new_exp_list(sql->sa); diff --git a/sql/test/rename/Tests/rename01.sql b/sql/test/rename/Tests/rename01.sql --- a/sql/test/rename/Tests/rename01.sql +++ b/sql/test/rename/Tests/rename01.sql @@ -1,22 +1,22 @@ create schema changeme; -create table "changeme"."testme" (oneval int); -insert into "changeme"."testme" values (1), (2), (NULL); select "name", "system" from sys.schemas where "name" = 'changeme'; select "name", "system" from sys.schemas where "name" = 'changed'; -select oneval from "changeme"."testme"; -select oneval from "changed"."testme"; --error - alter schema "changeme" rename to "changed"; - select "name", "system" from sys.schemas where "name" = 'changeme'; select "name", "system" from sys.schemas where "name" = 'changed'; -select oneval from "changeme"."testme"; --error + +create table "changed"."testme" (oneval int); +insert into "changed"."testme" values (1), (2), (NULL); + +alter schema "changeme" rename to "another"; --error, does not exist +alter schema "changed" rename to "another"; --error, dependencies on it + +select oneval from "changeme"."testme"; --error, does not exist select oneval from "changed"."testme"; - -drop table "changeme"."testme"; --error +drop table "changeme"."testme"; --error, does not exist drop table "changed"."testme"; -drop schema "changeme"; --error +drop schema "changeme"; --error, does not exist drop schema "changed"; select "name", "system" from sys.schemas where "name" in ('changeme', 'changed'); diff --git a/sql/test/rename/Tests/rename01.stable.err b/sql/test/rename/Tests/rename01.stable.err --- a/sql/test/rename/Tests/rename01.stable.err +++ b/sql/test/rename/Tests/rename01.stable.err @@ -28,20 +28,24 @@ stderr of test 'rename01` in directory ' # 16:50:35 > "mclient" "-lsql" "-ftest" "-tnone" "-Eutf-8" "-i" "-e" "--host=/var/tmp/mtest-22855" "--port=37559" # 16:50:35 > -MAPI = (monetdb) /var/tmp/mtest-22855/.s.monetdb.37559 -QUERY = select oneval from "changed"."testme"; --error -ERROR = !SELECT: no such schema 'changed' +MAPI = (monetdb) /var/tmp/mtest-23612/.s.monetdb.38749 +QUERY = alter schema "changeme" rename to "another"; --error, does not exist +ERROR = !ALTER SCHEMA: no such schema 'changeme' CODE = 3F000 -MAPI = (monetdb) /var/tmp/mtest-22855/.s.monetdb.37559 -QUERY = select oneval from "changeme"."testme"; --error +MAPI = (monetdb) /var/tmp/mtest-23612/.s.monetdb.38749 +QUERY = alter schema "changed" rename to "another"; --error, dependencies on it +ERROR = !ALTER SCHEMA: unable to rename schema 'changed' (there are database objects which depend on it) +CODE = 2BM37 +MAPI = (monetdb) /var/tmp/mtest-23612/.s.monetdb.38749 +QUERY = select oneval from "changeme"."testme"; --error, does not exist ERROR = !SELECT: no such schema 'changeme' CODE = 3F000 -MAPI = (monetdb) /var/tmp/mtest-22855/.s.monetdb.37559 -QUERY = drop table "changeme"."testme"; --error +MAPI = (monetdb) /var/tmp/mtest-23612/.s.monetdb.38749 +QUERY = drop table "changeme"."testme"; --error, does not exist ERROR = !DROP TABLE: no such schema 'changeme' CODE = 3F000 -MAPI = (monetdb) /var/tmp/mtest-22855/.s.monetdb.37559 -QUERY = drop schema "changeme"; --error +MAPI = (monetdb) /var/tmp/mtest-23612/.s.monetdb.38749 +QUERY = drop schema "changeme"; --error, does not exist ERROR = !DROP SCHEMA: name changeme does not exist CODE = 3F000 diff --git a/sql/test/rename/Tests/rename01.stable.out b/sql/test/rename/Tests/rename01.stable.out --- a/sql/test/rename/Tests/rename01.stable.out +++ b/sql/test/rename/Tests/rename01.stable.out @@ -65,9 +65,6 @@ Ready. # 16:50:35 > #create schema changeme; -#create table "changeme"."testme" (oneval int); -#insert into "changeme"."testme" values (1), (2), (NULL); -[ 3 ] #select "name", "system" from sys.schemas where "name" = 'changeme'; % sys.schemas, sys.schemas # table_name % name, system # name @@ -79,14 +76,6 @@ Ready. % name, system # name % varchar, boolean # type % 0, 5 # length -#select oneval from "changeme"."testme"; -% changeme.testme # table_name -% oneval # name -% int # type -% 1 # length -[ 1 ] -[ 2 ] -[ NULL ] #alter schema "changeme" rename to "changed"; #select "name", "system" from sys.schemas where "name" = 'changeme'; % sys.schemas, sys.schemas # table_name @@ -99,6 +88,9 @@ Ready. % varchar, boolean # type % 7, 5 # length [ "changed", false ] +#create table "changed"."testme" (oneval int); +#insert into "changed"."testme" values (1), (2), (NULL); +[ 3 ] #select oneval from "changed"."testme"; % changed.testme # table_name % oneval # name diff --git a/sql/test/rename/Tests/rename05.sql b/sql/test/rename/Tests/rename05.sql --- a/sql/test/rename/Tests/rename05.sql +++ b/sql/test/rename/Tests/rename05.sql @@ -1,9 +1,9 @@ create schema "nschema"; -create table "nschema"."ntable" (a int); -insert into "nschema"."ntable" values (1); - set schema "nschema"; alter schema "nschema" rename to "nother"; --should be possible to rename current schema, if it's not a system one + +create table "nother"."ntable" (a int); +insert into "nother"."ntable" values (1); select "a" from "ntable"; select "a" from "nother"."ntable"; @@ -15,11 +15,11 @@ begin end; select onefunc(); -alter table "ntable" rename to "ttable"; -insert into "ttable" values (1); -select onefunc(); --error +alter table "ntable" rename to "ttable"; --error because of dependencies +insert into "ntable" values (1); +select onefunc(); drop function "onefunc"; -drop table "ttable"; +drop table "ntable"; set schema "sys"; drop schema "nother"; diff --git a/sql/test/rename/Tests/rename05.stable.err b/sql/test/rename/Tests/rename05.stable.err --- a/sql/test/rename/Tests/rename05.stable.err +++ b/sql/test/rename/Tests/rename05.stable.err @@ -32,10 +32,10 @@ MAPI = (monetdb) /var/tmp/mtest-28754/. QUERY = alter schema "sys" rename to "nsys"; --error ERROR = !ALTER SCHEMA: cannot rename a system schema CODE = 3F000 -MAPI = (monetdb) /var/tmp/mtest-30395/.s.monetdb.30554 -QUERY = select onefunc(); --error -ERROR = !Table missing nother.ntable -CODE = 42S02 +MAPI = (monetdb) /var/tmp/mtest-23272/.s.monetdb.36538 +QUERY = alter table "ntable" rename to "ttable"; --error because of dependencies +ERROR = !ALTER TABLE: unable to rename table ntable (there are database objects which depend on it) +CODE = 2BM37 # 10:55:46 > # 10:55:46 > "Done." diff --git a/sql/test/rename/Tests/rename05.stable.out b/sql/test/rename/Tests/rename05.stable.out --- a/sql/test/rename/Tests/rename05.stable.out +++ b/sql/test/rename/Tests/rename05.stable.out @@ -25,11 +25,11 @@ Ready. # 10:55:46 > #create schema "nschema"; -#create table "nschema"."ntable" (a int); -#insert into "nschema"."ntable" values (1); +#set schema "nschema"; +#alter schema "nschema" rename to "nother"; --should be possible to rename current schema, if it's not a system one +#create table "nother"."ntable" (a int); +#insert into "nother"."ntable" values (1); [ 1 ] -#set schema "nschema"; -#alter schema "nschema" rename to "nother"; --should be possible to rename current schema if it's not a system one #select "a" from "ntable"; % nother.ntable # table_name % a # name @@ -55,6 +55,12 @@ Ready. #alter table "ntable" rename to "ttable"; #insert into "ttable" values (1); [ 1 ] +#select onefunc(); _______________________________________________ checkin-list mailing list checkin-list@monetdb.org https://www.monetdb.org/mailman/listinfo/checkin-list