diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-08-06 16:10:09 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-08-06 23:11:54 -0400 |
commit | d8da7f5ac544f3dd853a221faa5fab4ff788e25b (patch) | |
tree | 0ec2fa102695a107eb799ed12c4ade60e84a8790 /lib/sqlalchemy/sql/ddl.py | |
parent | 6a622c636ca5bc55d96b92652fd43b914a77645c (diff) | |
download | sqlalchemy-d8da7f5ac544f3dd853a221faa5fab4ff788e25b.tar.gz |
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
Diffstat (limited to 'lib/sqlalchemy/sql/ddl.py')
-rw-r--r-- | lib/sqlalchemy/sql/ddl.py | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/lib/sqlalchemy/sql/ddl.py b/lib/sqlalchemy/sql/ddl.py index ff36a68e4..ef6614b61 100644 --- a/lib/sqlalchemy/sql/ddl.py +++ b/lib/sqlalchemy/sql/ddl.py @@ -732,6 +732,17 @@ class SchemaGenerator(DDLBase): self.connection, table.name, schema=effective_schema ) + def _can_create_index(self, index): + effective_schema = self.connection.schema_for_object(index.table) + if effective_schema: + self.dialect.validate_identifier(effective_schema) + return not self.checkfirst or not self.dialect.has_index( + self.connection, + index.table.name, + index.name, + schema=effective_schema, + ) + def _can_create_sequence(self, sequence): effective_schema = self.connection.schema_for_object(sequence) @@ -831,7 +842,7 @@ class SchemaGenerator(DDLBase): if hasattr(table, "indexes"): for index in table.indexes: - self.traverse_single(index) + self.traverse_single(index, create_ok=True) if self.dialect.supports_comments and not self.dialect.inline_comments: if table.comment is not None: @@ -859,7 +870,9 @@ class SchemaGenerator(DDLBase): return self.connection.execute(CreateSequence(sequence)) - def visit_index(self, index): + def visit_index(self, index, create_ok=False): + if not create_ok and not self._can_create_index(index): + return self.connection.execute(CreateIndex(index)) @@ -973,6 +986,17 @@ class SchemaDropper(DDLBase): self.connection, table.name, schema=effective_schema ) + def _can_drop_index(self, index): + effective_schema = self.connection.schema_for_object(index.table) + if effective_schema: + self.dialect.validate_identifier(effective_schema) + return not self.checkfirst or self.dialect.has_index( + self.connection, + index.table.name, + index.name, + schema=effective_schema, + ) + def _can_drop_sequence(self, sequence): effective_schema = self.connection.schema_for_object(sequence) return self.dialect.supports_sequences and ( @@ -985,7 +1009,10 @@ class SchemaDropper(DDLBase): ) ) - def visit_index(self, index): + def visit_index(self, index, drop_ok=False): + if not drop_ok and not self._can_drop_index(index): + return + self.connection.execute(DropIndex(index)) def visit_table(self, table, drop_ok=False, _is_metadata_operation=False): |