From 0d50b0c7c5b0a9fda4c962f09900e45bebeb1a02 Mon Sep 17 00:00:00 2001 From: RamonWill Date: Wed, 21 Oct 2020 08:24:27 -0400 Subject: Support IF EXISTS/IF NOT EXISTS for DDL constructs Added parameters :paramref:`_ddl.CreateTable.if_not_exists`, :paramref:`_ddl.CreateIndex.if_not_exists`, :paramref:`_ddl.DropTable.if_exists` and :paramref:`_ddl.DropIndex.if_exists` to the :class:`_ddl.CreateTable`, :class:`_ddl.DropTable`, :class:`_ddl.CreateIndex` and :class:`_ddl.DropIndex` constructs which result in "IF NOT EXISTS" / "IF EXISTS" DDL being added to the CREATE/DROP. These phrases are 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 Ramon Williams. Fixes: #2843 Closes: #5663 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/5663 Pull-request-sha: 748b8472345d96efb446e2a444fbe020b313669f Change-Id: I6a2b1f697993ed49c31584f0a31887fb0a868ed3 --- lib/sqlalchemy/dialects/postgresql/base.py | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'lib/sqlalchemy/dialects/postgresql/base.py') diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index bf257ab3f..5292b3901 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -2322,6 +2322,9 @@ class PGDDLCompiler(compiler.DDLCompiler): if concurrently: text += "CONCURRENTLY " + if create.if_not_exists: + text += "IF NOT EXISTS " + text += "%s ON %s " % ( self._prepared_index_name(index, include_schema=False), preparer.format_table(index.table), @@ -2405,6 +2408,9 @@ class PGDDLCompiler(compiler.DDLCompiler): if concurrently: text += "CONCURRENTLY " + if drop.if_exists: + text += "IF EXISTS " + text += self._prepared_index_name(index, include_schema=True) return text -- cgit v1.2.1