FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat: tweak table dropping mechanism by IlyaFaer · Pull Request #28 · googleapis/python-spanner-sqlalchemy · GitHub

This repository was archived by the owner on May 14, 2026. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -86,6 +86,36 @@ 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 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:
DDL statements separated by semicolons, which will
sequentially drop indexes, foreign keys constraints
and then the table itself.
"""
Comment thread
IlyaFaer marked this conversation as resolved.
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.

Expand Down

Back | FazBrowse Home | New Git URL