From: Mike Fiedler Date: Sun, 3 Jul 2022 21:56:07 +0000 (-0400) Subject: feat: add `drop constraint if exists` to compiler X-Git-Tag: rel_2_0_0b1~156^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=41656f830c41734063a9460d2800c74123837de6;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git feat: add `drop constraint if exists` to compiler ### Description Add `DROP CONSTRAINT ... IF EXISTS` behavior to the compiler. Fixes https://github.com/sqlalchemy/sqlalchemy/issues/8141 ### Checklist 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: #` 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: #` 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 --- diff --git a/doc/build/changelog/unreleased_20/8141.rst b/doc/build/changelog/unreleased_20/8141.rst new file mode 100644 index 0000000000..8c70c754cc --- /dev/null +++ b/doc/build/changelog/unreleased_20/8141.rst @@ -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 diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 60ec09771f..817fc93159 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -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): diff --git a/test/sql/test_constraints.py b/test/sql/test_constraints.py index c417255af6..462667bedc 100644 --- a/test/sql/test_constraints.py +++ b/test/sql/test_constraints.py @@ -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()