Skip to content

Commit eeae989

Browse files
committed
BUG24947078: Create or alter view raises an error using non updatable views
This patch fixes the error raised when attempting to create or alter non updatable views. By default the statement for creating or altering views included WITH CHECK OPTION clause, this was removed to allow the usage of non updatable views. The user can still use the with_check_option method to set this clause. Tests were added for regression.
1 parent 75f5a34 commit eeae989

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

lib/mysqlx/statement.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from .errors import ProgrammingError
3030
from .expr import ExprParser
3131
from .compat import STRING_TYPES
32-
from .constants import Algorithms, Securities, CheckOptions
32+
from .constants import Algorithms, Securities
3333
from .dbdoc import DbDoc
3434
from .protobuf import mysqlx_crud_pb2 as MySQLxCrud
3535
from .result import SqlResult, Result, ColumnType
@@ -784,7 +784,7 @@ def __init__(self, view, replace=False):
784784
self._security = Securities.DEFINER
785785
self._definer = None
786786
self._defined_as = None
787-
self._check_option = CheckOptions.CASCADED
787+
self._check_option = None
788788

789789
def columns(self, columns):
790790
"""Sets the column names.
@@ -870,14 +870,16 @@ def execute(self):
870870
columns = " ({0})".format(", ".join(self._columns)) \
871871
if self._columns else ""
872872
view_name = quote_multipart_identifier((self._schema.name, self._name))
873+
check_option = " WITH {0} CHECK OPTION".format(self._check_option) \
874+
if self._check_option else ""
873875
sql = ("CREATE{replace} ALGORITHM = {algorithm}{definer} "
874876
"SQL SECURITY {security} VIEW {view_name}{columns} "
875-
"AS {defined_as} WITH {check_option} CHECK OPTION"
877+
"AS {defined_as}{check_option}"
876878
"".format(replace=replace, algorithm=self._algorithm,
877879
definer=definer, security=self._security,
878880
view_name=view_name, columns=columns,
879881
defined_as=self._defined_as,
880-
check_option=self._check_option))
882+
check_option=check_option))
881883

882884
self._connection.execute_nonquery("sql", sql)
883885
return self._view
@@ -903,13 +905,15 @@ def execute(self):
903905
columns = " ({0})".format(", ".join(self._columns)) \
904906
if self._columns else ""
905907
view_name = quote_multipart_identifier((self._schema.name, self._name))
908+
check_option = " WITH {0} CHECK OPTION".format(self._check_option) \
909+
if self._check_option else ""
906910
sql = ("ALTER ALGORITHM = {algorithm}{definer} "
907911
"SQL SECURITY {security} VIEW {view_name}{columns} "
908-
"AS {defined_as} WITH {check_option} CHECK OPTION"
912+
"AS {defined_as}{check_option}"
909913
"".format(algorithm=self._algorithm, definer=definer,
910914
security=self._security, view_name=view_name,
911915
columns=columns, defined_as=self._defined_as,
912-
check_option=self._check_option))
916+
check_option=check_option))
913917

914918
self._connection.execute_nonquery("sql", sql)
915919
return self._view

tests/test_mysqlx_crud.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ def test_create_view(self):
116116
.defined_as(defined_as) \
117117
.execute()
118118

119+
self.schema.drop_table(view_name)
120+
121+
# using a non-updatable view
122+
defined_as = ("SELECT COLUMN_TYPE, COLUMN_COMMENT FROM "
123+
"INFORMATION_SCHEMA.columns WHERE TABLE_NAME='{0}' "
124+
"AND COLUMN_NAME='id'".format(table_name))
125+
view = self.schema.create_view(view_name, True) \
126+
.defined_as(defined_as) \
127+
.execute()
128+
119129
self.schema.drop_table(table_name)
120130
self.schema.drop_table(view_name)
121131

@@ -144,6 +154,14 @@ def test_alter_view(self):
144154
.execute()
145155
self.assertEqual(5, view.count())
146156

157+
# using a non-updatable view
158+
defined_as = ("SELECT COLUMN_TYPE, COLUMN_COMMENT FROM "
159+
"INFORMATION_SCHEMA.columns WHERE TABLE_NAME='{0}' "
160+
"AND COLUMN_NAME='id'".format(table_name))
161+
view = self.schema.alter_view(view_name) \
162+
.defined_as(defined_as) \
163+
.execute()
164+
147165
self.schema.drop_table(table_name)
148166
self.schema.drop_table(view_name)
149167

0 commit comments

Comments
 (0)