diff options
| author | RamonWill <ramonwilliams@hotmail.co.uk> | 2020-10-21 08:24:27 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-12-14 10:19:21 -0500 |
| commit | 0d50b0c7c5b0a9fda4c962f09900e45bebeb1a02 (patch) | |
| tree | 72ecf3b97df6e84ebbffa486e07ebde3aaa35e0a /lib/sqlalchemy/dialects | |
| parent | 89ddd0b8976ed695d239898a2a8e4ebf531537f2 (diff) | |
| download | sqlalchemy-0d50b0c7c5b0a9fda4c962f09900e45bebeb1a02.tar.gz | |
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
Diffstat (limited to 'lib/sqlalchemy/dialects')
| -rw-r--r-- | lib/sqlalchemy/dialects/mysql/base.py | 10 | ||||
| -rw-r--r-- | lib/sqlalchemy/dialects/postgresql/base.py | 6 | ||||
| -rw-r--r-- | lib/sqlalchemy/dialects/sqlite/base.py | 8 |
3 files changed, 21 insertions, 3 deletions
diff --git a/lib/sqlalchemy/dialects/mysql/base.py b/lib/sqlalchemy/dialects/mysql/base.py index 27c7b2239..ad029299b 100644 --- a/lib/sqlalchemy/dialects/mysql/base.py +++ b/lib/sqlalchemy/dialects/mysql/base.py @@ -2031,7 +2031,10 @@ class MySQLDDLCompiler(compiler.DDLCompiler): if index_prefix: text += index_prefix + " " - text += "INDEX %s ON %s " % (name, table) + text += "INDEX " + if create.if_not_exists: + text += "IF NOT EXISTS " + text += "%s ON %s " % (name, table) length = index.dialect_options[self.dialect.name]["length"] if length is not None: @@ -2081,8 +2084,11 @@ class MySQLDDLCompiler(compiler.DDLCompiler): def visit_drop_index(self, drop): index = drop.element + text = "\nDROP INDEX " + if drop.if_exists: + text += "IF EXISTS " - return "\nDROP INDEX %s ON %s" % ( + return text + "%s ON %s" % ( self._prepared_index_name(index, include_schema=False), self.preparer.format_table(index.table), ) 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 diff --git a/lib/sqlalchemy/dialects/sqlite/base.py b/lib/sqlalchemy/dialects/sqlite/base.py index fc08b4b5e..1bd249efc 100644 --- a/lib/sqlalchemy/dialects/sqlite/base.py +++ b/lib/sqlalchemy/dialects/sqlite/base.py @@ -1543,7 +1543,13 @@ class SQLiteDDLCompiler(compiler.DDLCompiler): text = "CREATE " if index.unique: text += "UNIQUE " - text += "INDEX %s ON %s (%s)" % ( + + text += "INDEX " + + if create.if_not_exists: + text += "IF NOT EXISTS " + + text += "%s ON %s (%s)" % ( self._prepared_index_name(index, include_schema=True), preparer.format_table(index.table, use_schema=False), ", ".join( |
