Skip to content
This repository was archived by the owner on Mar 13, 2026. It is now read-only.

Commit 217841b

Browse files
olavloiteankiaga
andauthored
chore: support named schemas (#380)
* chore: support named schemas Modify information_schema queries so these are able to handle databases that contain multiple schemas. * Reformatting --------- Co-authored-by: Ankit Agarwal <146331865+ankiaga@users.noreply.github.com> Co-authored-by: ankiaga <ankiaga@google.com>
1 parent 62cccc3 commit 217841b

1 file changed

Lines changed: 30 additions & 17 deletions

File tree

google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ def get_multi_columns(
846846
col.spanner_type, col.is_nullable, col.generation_expression
847847
FROM information_schema.columns as col
848848
JOIN information_schema.tables AS t
849-
ON col.table_name = t.table_name
849+
USING (TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME)
850850
WHERE
851851
{table_filter_query}
852852
{table_type_query}
@@ -979,9 +979,14 @@ def get_multi_indexes(
979979
ARRAY_AGG(ic.column_ordering)
980980
FROM information_schema.indexes as i
981981
JOIN information_schema.index_columns AS ic
982-
ON ic.index_name = i.index_name AND ic.table_name = i.table_name
982+
ON ic.index_name = i.index_name
983+
AND ic.table_catalog = i.table_catalog
984+
AND ic.table_schema = i.table_schema
985+
AND ic.table_name = i.table_name
983986
JOIN information_schema.tables AS t
984-
ON i.table_name = t.table_name
987+
ON i.table_catalog = t.table_catalog
988+
AND i.table_schema = t.table_schema
989+
AND i.table_name = t.table_name
985990
WHERE
986991
{table_filter_query}
987992
{table_type_query}
@@ -1076,9 +1081,11 @@ def get_multi_pk_constraint(
10761081
SELECT tc.table_schema, tc.table_name, ccu.COLUMN_NAME
10771082
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS tc
10781083
JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS ccu
1079-
ON ccu.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
1084+
USING (TABLE_CATALOG, TABLE_SCHEMA, CONSTRAINT_NAME)
10801085
JOIN information_schema.tables AS t
1081-
ON tc.table_name = t.table_name
1086+
ON tc.TABLE_CATALOG = t.TABLE_CATALOG
1087+
AND tc.TABLE_SCHEMA = t.TABLE_SCHEMA
1088+
AND tc.TABLE_NAME = t.TABLE_NAME
10821089
WHERE {table_filter_query} {table_type_query}
10831090
{schema_filter_query} tc.CONSTRAINT_TYPE = "PRIMARY KEY"
10841091
""".format(
@@ -1196,13 +1203,19 @@ def get_multi_foreign_keys(
11961203
)
11971204
FROM information_schema.table_constraints AS tc
11981205
JOIN information_schema.constraint_column_usage AS ccu
1199-
ON ccu.constraint_name = tc.constraint_name
1206+
USING (table_catalog, table_schema, constraint_name)
12001207
JOIN information_schema.constraint_table_usage AS ctu
1201-
ON ctu.constraint_name = tc.constraint_name
1208+
ON ctu.table_catalog = tc.table_catalog
1209+
and ctu.table_schema = tc.table_schema
1210+
and ctu.constraint_name = tc.constraint_name
12021211
JOIN information_schema.key_column_usage AS kcu
1203-
ON kcu.constraint_name = tc.constraint_name
1212+
ON kcu.table_catalog = tc.table_catalog
1213+
and kcu.table_schema = tc.table_schema
1214+
and kcu.constraint_name = tc.constraint_name
12041215
JOIN information_schema.tables AS t
1205-
ON tc.table_name = t.table_name
1216+
ON t.table_catalog = tc.table_catalog
1217+
and t.table_schema = tc.table_schema
1218+
and t.table_name = tc.table_name
12061219
WHERE
12071220
{table_filter_query}
12081221
{table_type_query}
@@ -1323,15 +1336,14 @@ def get_unique_constraints(self, connection, table_name, schema=None, **kw):
13231336
SELECT ccu.CONSTRAINT_NAME, ccu.COLUMN_NAME
13241337
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS tc
13251338
JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS ccu
1326-
ON ccu.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
1327-
LEFT JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS rc
1328-
on tc.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
1339+
USING (TABLE_CATALOG, TABLE_SCHEMA, CONSTRAINT_NAME)
13291340
WHERE
13301341
tc.TABLE_NAME="{table_name}"
1342+
AND tc.TABLE_SCHEMA="{table_schema}"
13311343
AND tc.CONSTRAINT_TYPE = "UNIQUE"
1332-
AND rc.CONSTRAINT_NAME IS NOT NULL
1344+
AND tc.CONSTRAINT_NAME IS NOT NULL
13331345
""".format(
1334-
table_name=table_name
1346+
table_schema=schema or "", table_name=table_name
13351347
)
13361348

13371349
cols = []
@@ -1363,10 +1375,10 @@ def has_table(self, connection, table_name, schema=None, **kw):
13631375
"""
13641376
SELECT true
13651377
FROM INFORMATION_SCHEMA.TABLES
1366-
WHERE TABLE_NAME="{table_name}"
1378+
WHERE TABLE_SCHEMA="{table_schema}" AND TABLE_NAME="{table_name}"
13671379
LIMIT 1
13681380
""".format(
1369-
table_name=table_name
1381+
table_schema=schema or "", table_name=table_name
13701382
)
13711383
)
13721384

@@ -1390,9 +1402,10 @@ def has_sequence(self, connection, sequence_name, schema=None, **kw):
13901402
SELECT true
13911403
FROM INFORMATION_SCHEMA.SEQUENCES
13921404
WHERE NAME="{sequence_name}"
1405+
AND SCHEMA="{schema}"
13931406
LIMIT 1
13941407
""".format(
1395-
sequence_name=sequence_name
1408+
sequence_name=sequence_name, schema=schema or ""
13961409
)
13971410
)
13981411

0 commit comments

Comments
 (0)