]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Ensure CHECK constraint name is quoted for MySQL
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 21 Mar 2018 13:21:58 +0000 (09:21 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 21 Mar 2018 13:21:58 +0000 (09:21 -0400)
Change-Id: Ib85de299c5f3c2631c64fe30006879ba274fca15
Fixes: #487
alembic/ddl/mysql.py
docs/build/unreleased/487.rst [new file with mode: 0644]
tests/test_mysql.py

index 32e67ee3fbadfb2291333b121d0f8596114d1719..1f4b345ac9f4d1c3540d4a1539f997b0b049a527 100644 (file)
@@ -342,7 +342,8 @@ def _mysql_drop_constraint(element, compiler, **kw):
         # DROP CONSTRAINT for MySQL/MariaDB, so we implement fully
         # here.
         return "ALTER TABLE %s DROP CONSTRAINT %s" % \
-            (compiler.preparer.format_table(constraint.table), constraint.name)
+            (compiler.preparer.format_table(constraint.table),
+             compiler.preparer.format_constraint(constraint))
     else:
         raise NotImplementedError(
             "No generic 'DROP CONSTRAINT' in MySQL - "
diff --git a/docs/build/unreleased/487.rst b/docs/build/unreleased/487.rst
new file mode 100644 (file)
index 0000000..273edf2
--- /dev/null
@@ -0,0 +1,6 @@
+.. change::
+    :tags: bug, operations, mysql
+    :tickets: 487
+
+    Fixed bug in ``op.drop_constraint()`` for MySQL where
+    quoting rules would not be applied to the constraint name.
index 321488fab026bb59c36efe017d9a519da04ff402..dd872f7f5b43ca7764a869c0b9b631c427ccbc5b 100644 (file)
@@ -174,6 +174,13 @@ class MySQLOpTest(TestBase):
             "ALTER TABLE t1 DROP FOREIGN KEY f1"
         )
 
+    def test_drop_fk_quoted(self):
+        context = op_fixture('mysql')
+        op.drop_constraint("MyFk", "MyTable", "foreignkey")
+        context.assert_(
+            "ALTER TABLE `MyTable` DROP FOREIGN KEY `MyFk`"
+        )
+
     def test_drop_constraint_primary(self):
         context = op_fixture('mysql')
         op.drop_constraint('primary', 't1', type_='primary')
@@ -188,6 +195,13 @@ class MySQLOpTest(TestBase):
             "ALTER TABLE t1 DROP INDEX f1"
         )
 
+    def test_drop_unique_quoted(self):
+        context = op_fixture('mysql')
+        op.drop_constraint("MyUnique", "MyTable", "unique")
+        context.assert_(
+            "ALTER TABLE `MyTable` DROP INDEX `MyUnique`"
+        )
+
     def test_drop_check(self):
         context = op_fixture('mysql')
         op.drop_constraint("f1", "t1", "check")
@@ -195,6 +209,13 @@ class MySQLOpTest(TestBase):
             "ALTER TABLE t1 DROP CONSTRAINT f1"
         )
 
+    def test_drop_check_quoted(self):
+        context = op_fixture('mysql')
+        op.drop_constraint("MyCheck", "MyTable", "check")
+        context.assert_(
+            "ALTER TABLE `MyTable` DROP CONSTRAINT `MyCheck`"
+        )
+
     def test_drop_unknown(self):
         op_fixture('mysql')
         assert_raises_message(