Skip to content

Commit 533c965

Browse files
committed
Ensure CHECK constraint name is quoted for MySQL
Change-Id: Ib85de299c5f3c2631c64fe30006879ba274fca15 Fixes: #487
1 parent 22294ff commit 533c965

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

alembic/ddl/mysql.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,8 @@ def _mysql_drop_constraint(element, compiler, **kw):
342342
# DROP CONSTRAINT for MySQL/MariaDB, so we implement fully
343343
# here.
344344
return "ALTER TABLE %s DROP CONSTRAINT %s" % \
345-
(compiler.preparer.format_table(constraint.table), constraint.name)
345+
(compiler.preparer.format_table(constraint.table),
346+
compiler.preparer.format_constraint(constraint))
346347
else:
347348
raise NotImplementedError(
348349
"No generic 'DROP CONSTRAINT' in MySQL - "

docs/build/unreleased/487.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.. change::
2+
:tags: bug, operations, mysql
3+
:tickets: 487
4+
5+
Fixed bug in ``op.drop_constraint()`` for MySQL where
6+
quoting rules would not be applied to the constraint name.

tests/test_mysql.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,13 @@ def test_drop_fk(self):
174174
"ALTER TABLE t1 DROP FOREIGN KEY f1"
175175
)
176176

177+
def test_drop_fk_quoted(self):
178+
context = op_fixture('mysql')
179+
op.drop_constraint("MyFk", "MyTable", "foreignkey")
180+
context.assert_(
181+
"ALTER TABLE `MyTable` DROP FOREIGN KEY `MyFk`"
182+
)
183+
177184
def test_drop_constraint_primary(self):
178185
context = op_fixture('mysql')
179186
op.drop_constraint('primary', 't1', type_='primary')
@@ -188,13 +195,27 @@ def test_drop_unique(self):
188195
"ALTER TABLE t1 DROP INDEX f1"
189196
)
190197

198+
def test_drop_unique_quoted(self):
199+
context = op_fixture('mysql')
200+
op.drop_constraint("MyUnique", "MyTable", "unique")
201+
context.assert_(
202+
"ALTER TABLE `MyTable` DROP INDEX `MyUnique`"
203+
)
204+
191205
def test_drop_check(self):
192206
context = op_fixture('mysql')
193207
op.drop_constraint("f1", "t1", "check")
194208
context.assert_(
195209
"ALTER TABLE t1 DROP CONSTRAINT f1"
196210
)
197211

212+
def test_drop_check_quoted(self):
213+
context = op_fixture('mysql')
214+
op.drop_constraint("MyCheck", "MyTable", "check")
215+
context.assert_(
216+
"ALTER TABLE `MyTable` DROP CONSTRAINT `MyCheck`"
217+
)
218+
198219
def test_drop_unknown(self):
199220
op_fixture('mysql')
200221
assert_raises_message(

0 commit comments

Comments
 (0)