Skip to content

Commit 0182385

Browse files
fix(sqlalchemy-spanner): default supports_native_uuid to False for legacy Uuid compatibility
1 parent 681f3de commit 0182385

2 files changed

Lines changed: 15 additions & 14 deletions

File tree

packages/sqlalchemy-spanner/google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -775,10 +775,9 @@ class SpannerTypeCompiler(GenericTypeCompiler):
775775
"""
776776

777777
def visit_uuid(self, type_, **kw):
778-
if not type_.native_uuid or not self.dialect.supports_native_uuid:
779-
return self.visit_CHAR(types.CHAR(36), **kw)
780-
else:
778+
if type_.native_uuid and self.dialect.supports_native_uuid:
781779
return "UUID"
780+
return "STRING(36)"
782781

783782
def visit_INTEGER(self, type_, **kw):
784783
return "INT64"
@@ -862,7 +861,7 @@ class SpannerDialect(DefaultDialect):
862861
supports_identity_columns = True
863862
supports_native_boolean = True
864863
supports_native_decimal = True
865-
supports_native_uuid = True
864+
supports_native_uuid = False
866865
supports_statement_cache = True
867866
# Spanner uses protos for enums. Creating a column like
868867
# Column("an_enum", Enum("A", "B", "C")) will result in a String

packages/sqlalchemy-spanner/tests/mockserver_tests/test_uuid.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,38 +35,40 @@ def test_uuid_type_mapping(self):
3535
def test_uuid_designate_type(self):
3636
"""Test reflecting UUID type string returns types.UUID."""
3737
dialect = SpannerDialect()
38-
assert dialect.supports_native_uuid is True
38+
assert dialect.supports_native_uuid is False
3939
col_type = dialect._designate_type("UUID")
4040
eq_(col_type, types.UUID)
4141

4242
def test_uuid_ddl_compilation_default(self):
43-
"""Test DDL compilation emits UUID type by default when supports_native_uuid
44-
is True.
43+
"""Test DDL compilation: types.Uuid emits STRING(36) by default and
44+
types.UUID emits UUID by default.
4545
"""
4646
dialect = SpannerDialect()
4747
metadata = MetaData()
4848
table = Table(
4949
"test_uuid_table",
5050
metadata,
51-
Column("user_id", types.Uuid, primary_key=True),
51+
Column("legacy_id", types.Uuid, primary_key=True),
52+
Column("native_id", types.UUID),
5253
)
5354
statement = str(CreateTable(table).compile(dialect=dialect)).strip()
54-
assert "user_id UUID NOT NULL" in statement
55+
assert "legacy_id STRING(36) NOT NULL" in statement
56+
assert "native_id UUID" in statement
5557

56-
def test_uuid_ddl_compilation_native_disabled(self):
57-
"""Test DDL compilation falls back to STRING(36) when supports_native_uuid
58-
is False.
58+
def test_uuid_ddl_compilation_native_enabled(self):
59+
"""Test DDL compilation emits UUID type for types.Uuid when supports_native_uuid
60+
is set to True on dialect.
5961
"""
6062
dialect = SpannerDialect()
61-
dialect.supports_native_uuid = False
63+
dialect.supports_native_uuid = True
6264
metadata = MetaData()
6365
table = Table(
6466
"test_uuid_table",
6567
metadata,
6668
Column("user_id", types.Uuid, primary_key=True),
6769
)
6870
statement = str(CreateTable(table).compile(dialect=dialect)).strip()
69-
assert "user_id STRING(36) NOT NULL" in statement
71+
assert "user_id UUID NOT NULL" in statement
7072

7173
def test_uuid_python_conversion_legacy(self):
7274
"""Test that types.Uuid automatically converts uuid.UUID to/from str

0 commit comments

Comments
 (0)