]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
MYSQL: added support for drop check/constraint
authorHannes Hansen <hannes.jakob.hansen@cern.ch>
Thu, 23 May 2019 20:27:21 +0000 (16:27 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 27 May 2019 20:57:37 +0000 (16:57 -0400)
Added support for DROP CHECK constraint which is required by MySQL 8.0.16
to drop a CHECK constraint; MariaDB supports plain DROP CONSTRAINT.  The
logic distinguishes between the two syntaxes by checking the server version
string for MariaDB presence.    Alembic migrations has already worked
around this issue by implementing its own DROP for MySQL / MariaDB CHECK
constraints, however this change implements it straight in Core so that its
available for general use.   Pull request courtesy Hannes Hansen.

Fixes: #4650
Closes: #4659
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/4659
Pull-request-sha: 5b654a55e23c2ca498ca3b1cae4f53859e93e8f7

Change-Id: I967710f890722f11cf1f40406adbb17464d16194

doc/build/changelog/unreleased_13/4650.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/mysql/base.py
test/dialect/mysql/test_compiler.py

diff --git a/doc/build/changelog/unreleased_13/4650.rst b/doc/build/changelog/unreleased_13/4650.rst
new file mode 100644 (file)
index 0000000..9c6623e
--- /dev/null
@@ -0,0 +1,11 @@
+.. change::
+    :tags: mysql, bug
+    :tickets: 4650
+
+    Added support for DROP CHECK constraint which is required by MySQL 8.0.16
+    to drop a CHECK constraint; MariaDB supports plain DROP CONSTRAINT.  The
+    logic distinguishes between the two syntaxes by checking the server version
+    string for MariaDB presence.    Alembic migrations has already worked
+    around this issue by implementing its own DROP for MySQL / MariaDB CHECK
+    constraints, however this change implements it straight in Core so that its
+    available for general use.   Pull request courtesy Hannes Hansen.
index 9cae3c689fe32c5a3b36c7c0cfb401f5245631d4..ad5ab288ce254f9ced7921d737805cdc115de145 100644 (file)
@@ -1703,6 +1703,12 @@ class MySQLDDLCompiler(compiler.DDLCompiler):
         elif isinstance(constraint, sa_schema.UniqueConstraint):
             qual = "INDEX "
             const = self.preparer.format_constraint(constraint)
+        elif isinstance(constraint, sa_schema.CheckConstraint):
+            if self.dialect._is_mariadb:
+                qual = "CONSTRAINT "
+            else:
+                qual = "CHECK "
+            const = self.preparer.format_constraint(constraint)
         else:
             qual = ""
             const = self.preparer.format_constraint(constraint)
@@ -2397,11 +2403,13 @@ class MySQLDialect(default.DefaultDialect):
 
     @property
     def _is_mariadb(self):
-        return "MariaDB" in self.server_version_info
+        return (
+            self.server_version_info and "MariaDB" in self.server_version_info
+        )
 
     @property
     def _is_mysql(self):
-        return "MariaDB" not in self.server_version_info
+        return not self._is_mariadb
 
     @property
     def _is_mariadb_102(self):
index 54767a913b835be051f3cec5c4ed7a39254d5254..60e11ca29fd18ee27749f0ca803931c242d502e5 100644 (file)
@@ -5,6 +5,7 @@ from sqlalchemy import BOOLEAN
 from sqlalchemy import Boolean
 from sqlalchemy import cast
 from sqlalchemy import CHAR
+from sqlalchemy import CheckConstraint
 from sqlalchemy import CLOB
 from sqlalchemy import Column
 from sqlalchemy import DATE
@@ -128,6 +129,35 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             "CREATE INDEX test_idx2 ON testtbl (data(5))",
         )
 
+    def test_drop_constraint_mysql(self):
+        m = MetaData()
+        table_name = "testtbl"
+        constraint_name = "constraint"
+        constraint = CheckConstraint("data IS NOT NULL", name=constraint_name)
+        tbl = Table(table_name, m, Column("data", String(255)), constraint)
+        dialect = mysql.dialect()
+        self.assert_compile(
+            schema.DropConstraint(constraint),
+            "ALTER TABLE %s DROP CHECK `%s`"
+            % (table_name, constraint_name),
+            dialect=dialect
+        )
+
+    def test_drop_constraint_mariadb(self):
+        m = MetaData()
+        table_name = "testtbl"
+        constraint_name = "constraint"
+        constraint = CheckConstraint("data IS NOT NULL", name=constraint_name)
+        tbl = Table(table_name, m, Column("data", String(255)), constraint)
+        dialect = mysql.dialect()
+        dialect.server_version_info = (10, 1, 1, "MariaDB")
+        self.assert_compile(
+            schema.DropConstraint(constraint),
+            "ALTER TABLE %s DROP CONSTRAINT `%s`"
+            % (table_name, constraint_name),
+            dialect=dialect
+        )
+
     def test_create_index_with_length_quoted(self):
         m = MetaData()
         tbl = Table(