Skip to content

Commit 1fb4828

Browse files
an3lvuvova
authored andcommitted
MDEV-28343: sys.create_synonym_db fails with ER_VIEW_SELECT_TMPTABLE when schema contains temporary tables
- MDEV-28342 raised the error in case temporary table shadows base table - Now we are allowed to shadow base tables with temporary tables and `sys.create_synonym_db()` can easily check for existance of temporary table and ignore view creation, since it is not supported to create view from temporary table. Reviewed-by: <monty@mariadb.org>, <vicentiu@mariadb.org>
1 parent 91bfc76 commit 1fb4828

File tree

3 files changed

+79
-32
lines changed

3 files changed

+79
-32
lines changed

mysql-test/suite/sysschema/r/pr_create_synonym_db.result

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,37 @@ create table t (b int);
6161
create table b(a int);
6262
create temporary table b (a int);
6363
call sys.create_synonym_db('db','db_copy');
64-
ERROR HY000: Table`db`.`b`shadows base table. View cannot be created! Terminating!
64+
summary
65+
Created 2 views in the `db_copy` database
66+
use db_copy;
67+
show tables;
68+
Tables_in_db_copy
69+
a
70+
t
6571
drop database db;
6672
drop database db_copy;
73+
# MDEV-28343: sys.create_synonym_db fails with ER_VIEW_SELECT_TMPTABLE
74+
# when schema contains temporary tables
75+
#
76+
create database mytestdb;
77+
use mytestdb;
78+
create table t (b int);
79+
create temporary table tmp (a int);
80+
call sys.create_synonym_db('mytestdb','db_syn1');
81+
summary
82+
Created 1 view in the `db_syn1` database
83+
use db_syn1;
84+
show tables;
85+
Tables_in_db_syn1
86+
t
87+
drop database db_syn1;
88+
use mytestdb;
89+
create temporary table t (b int);
90+
call sys.create_synonym_db('mytestdb','db_syn1');
91+
summary
92+
Created 0 views in the `db_syn1` database
93+
use db_syn1;
94+
show tables;
95+
Tables_in_db_syn1
96+
drop database mytestdb;
97+
drop database db_syn1;

mysql-test/suite/sysschema/t/pr_create_synonym_db.test

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,34 @@ create table a(a int);
6262
create table t (b int);
6363
create table b(a int);
6464
create temporary table b (a int);
65-
--error ER_SIGNAL_EXCEPTION
6665
call sys.create_synonym_db('db','db_copy');
6766

67+
use db_copy;
68+
show tables;
69+
6870
drop database db;
6971
drop database db_copy;
72+
--echo # MDEV-28343: sys.create_synonym_db fails with ER_VIEW_SELECT_TMPTABLE
73+
--echo # when schema contains temporary tables
74+
--echo #
75+
76+
create database mytestdb;
77+
use mytestdb;
78+
create table t (b int);
79+
# This temporary table will not be created as an view in synonym db
80+
create temporary table tmp (a int);
81+
call sys.create_synonym_db('mytestdb','db_syn1');
82+
use db_syn1;
83+
show tables;
84+
drop database db_syn1;
85+
86+
use mytestdb;
87+
# This temporary table will shadow the base table and no views will be created
88+
create temporary table t (b int);
89+
call sys.create_synonym_db('mytestdb','db_syn1');
90+
91+
use db_syn1;
92+
show tables;
93+
94+
drop database mytestdb;
95+
drop database db_syn1;

scripts/sys_schema/procedures/create_synonym_db.sql

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ BEGIN
9898
DECLARE v_table VARCHAR(64);
9999
DECLARE v_views_created INT DEFAULT 0;
100100
DECLARE v_table_exists ENUM('', 'BASE TABLE', 'VIEW', 'TEMPORARY') DEFAULT '';
101-
DECLARE v_temp_table TEXT;
101+
102+
DECLARE db_doesnt_exist CONDITION FOR SQLSTATE '42000';
103+
DECLARE db_name_exists CONDITION FOR SQLSTATE 'HY000';
102104

103105
DECLARE c_table_names CURSOR FOR
104106
SELECT TABLE_NAME
@@ -142,37 +144,25 @@ BEGIN
142144
IF v_done THEN
143145
LEAVE c_table_names;
144146
END IF;
145-
146-
-- Check does temporary table shadows the base table. If it is so, terminate.
147+
-- Check the table type, don't support temporary since cannot create the view
147148
CALL sys.table_exists(in_db_name, v_table, v_table_exists);
148-
IF (v_table_exists = 'TEMPORARY') THEN
149-
SET v_temp_table =
150-
CONCAT(
151-
'Table',
152-
sys.quote_identifier(in_db_name),
153-
'.',
154-
sys.quote_identifier(v_table),
155-
'shadows base table. View cannot be created! Terminating!');
156-
SIGNAL SQLSTATE 'HY000'
157-
SET MESSAGE_TEXT = v_temp_table;
158-
LEAVE c_table_names;
149+
IF (v_table_exists <> 'TEMPORARY') THEN
150+
SET @create_view_stmt = CONCAT(
151+
'CREATE SQL SECURITY INVOKER VIEW ',
152+
sys.quote_identifier(in_synonym),
153+
'.',
154+
sys.quote_identifier(v_table),
155+
' AS SELECT * FROM ',
156+
sys.quote_identifier(in_db_name),
157+
'.',
158+
sys.quote_identifier(v_table)
159+
);
160+
PREPARE create_view_stmt FROM @create_view_stmt;
161+
EXECUTE create_view_stmt;
162+
DEALLOCATE PREPARE create_view_stmt;
163+
164+
SET v_views_created = v_views_created + 1;
159165
END IF;
160-
161-
SET @create_view_stmt = CONCAT(
162-
'CREATE SQL SECURITY INVOKER VIEW ',
163-
sys.quote_identifier(in_synonym),
164-
'.',
165-
sys.quote_identifier(v_table),
166-
' AS SELECT * FROM ',
167-
sys.quote_identifier(in_db_name),
168-
'.',
169-
sys.quote_identifier(v_table)
170-
);
171-
PREPARE create_view_stmt FROM @create_view_stmt;
172-
EXECUTE create_view_stmt;
173-
DEALLOCATE PREPARE create_view_stmt;
174-
175-
SET v_views_created = v_views_created + 1;
176166
END LOOP;
177167
CLOSE c_table_names;
178168

0 commit comments

Comments
 (0)