]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
feat: add `drop constraint if exists` to compiler
authorMike Fiedler <miketheman@gmail.com>
Sun, 3 Jul 2022 21:56:07 +0000 (17:56 -0400)
committersqla-tester <sqla-tester@sqlalchemy.org>
Sun, 3 Jul 2022 21:56:07 +0000 (17:56 -0400)
### Description

Add `DROP CONSTRAINT ... IF EXISTS` behavior to the compiler.

Fixes https://github.com/sqlalchemy/sqlalchemy/issues/8141

### Checklist
<!-- go over following points. check them with an `x` if they do apply, (they turn into clickable checkboxes once the PR is submitted, so no need to do everything at once)

-->

This pull request is:

- [ ] A documentation / typographical error fix
- Good to go, no issue or tests are needed
- [ ] A short code fix
- please include the issue number, and create an issue if none exists, which
  must include a complete example of the issue.  one line code fixes without an
  issue and demonstration will not be accepted.
- Please include: `Fixes: #<issue number>` in the commit message
- please include tests.   one line code fixes without tests will not be accepted.
- [x] A new feature implementation
- please include the issue number, and create an issue if none exists, which must
  include a complete example of how the feature would look.
- Please include: `Fixes: #<issue number>` in the commit message
- please include tests.

**Have a nice day!**

Closes: #8161
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/8161
Pull-request-sha: 43276e29fa864fc66900c5a3fa0bf84df5f14271

Change-Id: I18bae3cf013159b6fffde4413fb59ce19ff83c16

doc/build/changelog/unreleased_20/8141.rst [new file with mode: 0644]
lib/sqlalchemy/sql/compiler.py
test/sql/test_constraints.py

diff --git a/doc/build/changelog/unreleased_20/8141.rst b/doc/build/changelog/unreleased_20/8141.rst
new file mode 100644 (file)
index 0000000..8c70c75
--- /dev/null
@@ -0,0 +1,11 @@
+.. change::
+    :tags: usecase, schema
+    :tickets: 8141
+
+    Added parameter :paramref:`_ddl.DropConstraint.if_exists` to the
+    :class:`_ddl.DropConstraint` construct which result in "IF EXISTS" DDL
+    being added to the DROP statement.
+    This phrase is not accepted by all databases and the operation will fail
+    on a database that does not support it as there is no similarly compatible
+    fallback within the scope of a single DDL statement.
+    Pull request courtesy Mike Fiedler.
\ No newline at end of file
index 60ec09771f373e28d37334effae2e8e3d5f05f3f..817fc93159989de8b474db3f9daf8fe85b54b1b3 100644 (file)
@@ -5216,10 +5216,11 @@ class DDLCompiler(Compiled):
                 "Can't emit DROP CONSTRAINT for constraint %r; "
                 "it has no name" % drop.element
             )
-        return "ALTER TABLE %s DROP CONSTRAINT %s%s" % (
+        return "ALTER TABLE %s DROP CONSTRAINT %s%s%s" % (
             self.preparer.format_table(drop.element.table),
+            "IF EXISTS " if drop.if_exists else "",
             formatted_name,
-            drop.cascade and " CASCADE" or "",
+            " CASCADE" if drop.cascade else "",
         )
 
     def get_column_specification(self, column, **kwargs):
index c417255af612612343015164308ec47e3de0230d..462667bedcedb875894b6ff929d3a78218697424 100644 (file)
@@ -1261,6 +1261,16 @@ class ConstraintCompilationTest(fixtures.TestBase, AssertsCompiledSQL):
             "ALTER TABLE tbl DROP CONSTRAINT my_test_constraint CASCADE",
         )
 
+    def test_render_drop_constraint_if_exists(self):
+        t, t2 = self._constraint_create_fixture()
+
+        constraint = CheckConstraint("a = 1", name="a1", table=t)
+
+        self.assert_compile(
+            schema.DropConstraint(constraint, if_exists=True),
+            "ALTER TABLE tbl DROP CONSTRAINT IF EXISTS a1",
+        )
+
     def test_render_add_fk_constraint_stringcol(self):
         t, t2 = self._constraint_create_fixture()