From df2cdeb45ee85128e761066de21e436971871f17 Mon Sep 17 00:00:00 2001 From: IlyaFaer Date: Tue, 16 Mar 2021 17:09:46 +0300 Subject: [PATCH 1/2] feat: tweak table dropping mechanism --- .../sqlalchemy_spanner/sqlalchemy_spanner.py | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py b/google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py index 4ecf6e9a..407cbb16 100644 --- a/google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py +++ b/google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py @@ -14,7 +14,7 @@ import re -from sqlalchemy import types +from sqlalchemy import types, ForeignKeyConstraint from sqlalchemy.engine.base import Engine from sqlalchemy.engine.default import DefaultDialect from sqlalchemy.sql.compiler import ( @@ -86,6 +86,35 @@ def visit_empty_set_expr(self, type_): class SpannerDDLCompiler(DDLCompiler): """Spanner DDL statements compiler.""" + def visit_drop_table(self, drop_table): + """ + Cloud Spanner doesn't let to drop table which has indexes + or foreign key constraints. Building several DDLs (separated + by semicolons) to drop indexes and foreign keys of the table + before actually executing DROP TABLE statement. + + Args: + (sqlalchemy.schema.DropTable): DROP TABLE statement object. + + Returns: + str: + DDLs separated by semicolons, which will sequentially + drop indexes, foreign keys constraints and then the + table itself. + """ + constrs = "" + for cons in drop_table.element.constraints: + if isinstance(cons, ForeignKeyConstraint) and cons.name: + constrs += "ALTER TABLE {table} DROP CONSTRAINT {constr};".format( + table=drop_table.element.name, constr=cons.name + ) + + indexes = "" + for index in drop_table.element.indexes: + indexes += "DROP INDEX {};".format(index.name) + + return indexes + constrs + str(drop_table) + def visit_primary_key_constraint(self, constraint): """Build primary key definition. From 438d11137ccab55d688092887a518bda37204261 Mon Sep 17 00:00:00 2001 From: IlyaFaer Date: Wed, 17 Mar 2021 17:54:30 +0300 Subject: [PATCH 2/2] fix docstrings --- .../sqlalchemy_spanner/sqlalchemy_spanner.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py b/google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py index 407cbb16..4275ab6f 100644 --- a/google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py +++ b/google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py @@ -88,19 +88,20 @@ class SpannerDDLCompiler(DDLCompiler): def visit_drop_table(self, drop_table): """ - Cloud Spanner doesn't let to drop table which has indexes - or foreign key constraints. Building several DDLs (separated - by semicolons) to drop indexes and foreign keys of the table - before actually executing DROP TABLE statement. + Cloud Spanner doesn't drop tables which have indexes + or foreign key constraints. This method builds several DDL + statements separated by semicolons to drop the indexes and + foreign keys constraints of the table before the DROP TABLE + statement. Args: (sqlalchemy.schema.DropTable): DROP TABLE statement object. Returns: str: - DDLs separated by semicolons, which will sequentially - drop indexes, foreign keys constraints and then the - table itself. + DDL statements separated by semicolons, which will + sequentially drop indexes, foreign keys constraints + and then the table itself. """ constrs = "" for cons in drop_table.element.constraints: