Skip to content

Commit c433cda

Browse files
author
Ilya Gurov
authored
fix: ALTER COLUMN NOT NULL directive fails because of inappropriate syntax (googleapis#124)
* fix: ALTER COLUMN NOT NULL directive fails because of inappropriate syntax * add alembic into nox session dependencies * alembic dependency
1 parent 406c34b commit c433cda

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,17 @@
1515
import pkg_resources
1616
import re
1717

18-
from sqlalchemy import types, ForeignKeyConstraint
18+
from alembic.ddl.base import (
19+
ColumnNullable,
20+
ColumnType,
21+
alter_column,
22+
alter_table,
23+
format_type,
24+
)
25+
from sqlalchemy import ForeignKeyConstraint, types, util
1926
from sqlalchemy.engine.base import Engine
2027
from sqlalchemy.engine.default import DefaultDialect
21-
from sqlalchemy import util
28+
from sqlalchemy.ext.compiler import compiles
2229
from sqlalchemy.sql.compiler import (
2330
selectable,
2431
DDLCompiler,
@@ -27,6 +34,7 @@
2734
SQLCompiler,
2835
RESERVED_WORDS,
2936
)
37+
3038
from google.cloud import spanner_dbapi
3139
from google.cloud.sqlalchemy_spanner._opentelemetry_tracing import trace_call
3240

@@ -864,3 +872,28 @@ def do_execute_no_params(self, cursor, statement, context=None):
864872
}
865873
with trace_call("SpannerSqlAlchemy.ExecuteNoParams", trace_attributes):
866874
cursor.execute(statement)
875+
876+
877+
# Alembic ALTER operation override
878+
@compiles(ColumnNullable, "spanner")
879+
def visit_column_nullable(
880+
element: "ColumnNullable", compiler: "SpannerDDLCompiler", **kw
881+
) -> str:
882+
return "%s %s %s %s" % (
883+
alter_table(compiler, element.table_name, element.schema),
884+
alter_column(compiler, element.column_name),
885+
format_type(compiler, element.existing_type),
886+
"" if element.nullable else "NOT NULL",
887+
)
888+
889+
890+
# Alembic ALTER operation override
891+
@compiles(ColumnType, "spanner")
892+
def visit_column_type(
893+
element: "ColumnType", compiler: "SpannerDDLCompiler", **kw
894+
) -> str:
895+
return "%s %s %s" % (
896+
alter_table(compiler, element.table_name, element.schema),
897+
alter_column(compiler, element.column_name),
898+
"%s" % format_type(compiler, element.type_),
899+
)

migration_test_cleanup.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
config.read("setup.cfg")
2828
db_url = config.get("db", "default")
2929

30-
project = re.findall(r'projects(.*?)instances', db_url)
31-
instance_id = re.findall(r'instances(.*?)databases', db_url)
30+
project = re.findall(r"projects(.*?)instances", db_url)
31+
instance_id = re.findall(r"instances(.*?)databases", db_url)
3232

33-
client = spanner.Client(project="".join(project).replace('/', ''))
34-
instance = client.instance(instance_id="".join(instance_id).replace('/', ''))
33+
client = spanner.Client(project="".join(project).replace("/", ""))
34+
instance = client.instance(instance_id="".join(instance_id).replace("/", ""))
3535
database = instance.database("compliance-test")
3636

3737
database.update_ddl(["DROP TABLE account", "DROP TABLE alembic_version"]).result(120)

noxfile.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ class = StreamHandler
6060
sa.Column('id', sa.Integer, primary_key=True),
6161
sa.Column('name', sa.String(50), nullable=False),
6262
sa.Column('description', sa.Unicode(200)),
63+
)
64+
op.alter_column(
65+
'account',
66+
'name',
67+
existing_type=sa.String(50),
68+
nullable=True,
6369
)"""
6470

6571

setup.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919

2020
name = "sqlalchemy-spanner"
2121
description = "SQLAlchemy dialect integrated into Cloud Spanner database"
22-
dependencies = ["sqlalchemy>=1.1.13, <=1.3.23", "google-cloud-spanner>=3.3.0"]
22+
dependencies = [
23+
"sqlalchemy>=1.1.13, <=1.3.23",
24+
"google-cloud-spanner>=3.3.0",
25+
"alembic",
26+
]
2327
extras = {
2428
"tracing": [
2529
"opentelemetry-api >= 1.1.0",

0 commit comments

Comments
 (0)