From d8da7f5ac544f3dd853a221faa5fab4ff788e25b Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Tue, 6 Aug 2019 16:10:09 -0400 Subject: Implement checkfirst for Index.create(), Index.drop() The :meth:`.Index.create` and :meth:`.Index.drop` methods now have a parameter :paramref:`.Index.create.checkfirst`, in the same way as that of :class:`.Table` and :class:`.Sequence`, which when enabled will cause the operation to detect if the index exists (or not) before performing a create or drop operation. Fixes: #527 Change-Id: Idf994bc016359d0ae86cc64ccb20378115cb66d6 --- lib/sqlalchemy/engine/default.py | 9 +++++++++ lib/sqlalchemy/engine/interfaces.py | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) (limited to 'lib/sqlalchemy/engine') diff --git a/lib/sqlalchemy/engine/default.py b/lib/sqlalchemy/engine/default.py index 9d457b800..fb1728eab 100644 --- a/lib/sqlalchemy/engine/default.py +++ b/lib/sqlalchemy/engine/default.py @@ -426,6 +426,15 @@ class DefaultDialect(interfaces.Dialect): ) } + def has_index(self, connection, table_name, index_name, schema=None): + if not self.has_table(connection, table_name, schema=schema): + return False + for idx in self.get_indexes(connection, table_name, schema=schema): + if idx["name"] == index_name: + return True + else: + return False + def validate_identifier(self, ident): if len(ident) > self.max_identifier_length: raise exc.IdentifierError( diff --git a/lib/sqlalchemy/engine/interfaces.py b/lib/sqlalchemy/engine/interfaces.py index 5bd3b1d3e..fddc1712f 100644 --- a/lib/sqlalchemy/engine/interfaces.py +++ b/lib/sqlalchemy/engine/interfaces.py @@ -439,6 +439,24 @@ class Dialect(object): raise NotImplementedError() + def has_index(self, connection, table_name, index_name, schema=None): + """Check the existence of a particular index name in the database. + + Given a :class:`.Connection` object, a string + `table_name` and stiring index name, return True if an index of the + given name on the given table exists, false otherwise. + + The :class:`.DefaultDialect` implements this in terms of the + :meth:`.Dialect.has_table` and :meth:`.Dialect.get_indexes` methods, + however dialects can implement a more performant version. + + + .. versionadded:: 1.4 + + """ + + raise NotImplementedError() + def has_sequence(self, connection, sequence_name, schema=None, **kw): """Check the existence of a particular sequence in the database. -- cgit v1.2.1