summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql/compiler.py
diff options
context:
space:
mode:
authorJesse Bakker <github@jessebakker.com>2022-09-06 16:00:10 -0400
committermike bayer <mike_mp@zzzcomputing.com>2022-10-04 02:40:20 +0000
commitb6eed88ef4ebb3fd7035b4e366bf6653ebb26d15 (patch)
treee98e19d6fa7b236cf2fc7eed3ffc96c805b80667 /lib/sqlalchemy/sql/compiler.py
parentd5905ccd7df000143194975f8f3d72fcef0672e3 (diff)
downloadsqlalchemy-b6eed88ef4ebb3fd7035b4e366bf6653ebb26d15.tar.gz
Make if_exists and if_not_exists flags on ddl statements match compiler
Added ``if_exists`` and ``if_not_exists`` parameters for all "Create" / "Drop" constructs including :class:`.CreateSequence`, :class:`.DropSequence`, :class:`.CreateIndex`, :class:`.DropIndex`, etc. allowing generic "IF EXISTS" / "IF NOT EXISTS" phrases to be rendered within DDL. Pull request courtesy Jesse Bakker. Fixes: #7354 Closes: #8492 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/8492 Pull-request-sha: d107c6ce553bd430111607815f5b3938ffc4770c Change-Id: I367e57b2d9216f5180bcc44e86ca6f3dc794e5ca
Diffstat (limited to 'lib/sqlalchemy/sql/compiler.py')
-rw-r--r--lib/sqlalchemy/sql/compiler.py25
1 files changed, 17 insertions, 8 deletions
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py
index c7e226fcc..dd40bfe34 100644
--- a/lib/sqlalchemy/sql/compiler.py
+++ b/lib/sqlalchemy/sql/compiler.py
@@ -5396,12 +5396,16 @@ class DDLCompiler(Compiled):
return self.sql_compiler.post_process_text(ddl.statement % context)
def visit_create_schema(self, create, **kw):
- schema = self.preparer.format_schema(create.element)
- return "CREATE SCHEMA " + schema
+ text = "CREATE SCHEMA "
+ if create.if_not_exists:
+ text += "IF NOT EXISTS "
+ return text + self.preparer.format_schema(create.element)
def visit_drop_schema(self, drop, **kw):
- schema = self.preparer.format_schema(drop.element)
- text = "DROP SCHEMA " + schema
+ text = "DROP SCHEMA "
+ if drop.if_exists:
+ text += "IF EXISTS "
+ text += self.preparer.format_schema(drop.element)
if drop.cascade:
text += " CASCADE"
return text
@@ -5650,9 +5654,11 @@ class DDLCompiler(Compiled):
return " ".join(text)
def visit_create_sequence(self, create, prefix=None, **kw):
- text = "CREATE SEQUENCE %s" % self.preparer.format_sequence(
- create.element
- )
+ text = "CREATE SEQUENCE "
+ if create.if_not_exists:
+ text += "IF NOT EXISTS "
+ text += self.preparer.format_sequence(create.element)
+
if prefix:
text += prefix
if create.element.start is None:
@@ -5663,7 +5669,10 @@ class DDLCompiler(Compiled):
return text
def visit_drop_sequence(self, drop, **kw):
- return "DROP SEQUENCE %s" % self.preparer.format_sequence(drop.element)
+ text = "DROP SEQUENCE "
+ if drop.if_exists:
+ text += "IF EXISTS "
+ return text + self.preparer.format_sequence(drop.element)
def visit_drop_constraint(self, drop, **kw):
constraint = drop.element