From 0ee8d96bd00732a538d60bd713af79a26bb7efa9 Mon Sep 17 00:00:00 2001 From: ankiaga Date: Wed, 14 Feb 2024 13:33:10 +0530 Subject: [PATCH 01/10] chore: Add support for named schema --- django_spanner/introspection.py | 50 ++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/django_spanner/introspection.py b/django_spanner/introspection.py index 9202c170d8..3495c8818e 100644 --- a/django_spanner/introspection.py +++ b/django_spanner/introspection.py @@ -38,7 +38,7 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): FROM information_schema.tables AS t WHERE - t.table_catalog = '' and t.table_schema = '' + t.table_catalog = '' and t.table_schema = '{schema_name}' """ else: LIST_TABLE_SQL = """ @@ -47,7 +47,7 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): FROM information_schema.tables AS t WHERE - t.table_catalog = '' and t.table_schema = '' + t.table_catalog = '' and t.table_schema = '{schema_name}' """ def get_field_type(self, data_type, description): @@ -76,7 +76,10 @@ def get_table_list(self, cursor): :rtype: list :returns: A list of table and view names in the current database. """ - results = cursor.run_sql_in_snapshot(self.LIST_TABLE_SQL) + schema_name = self._get_schema_name(cursor) + results = cursor.run_sql_in_snapshot( + self.LIST_TABLE_SQL.format(schema_name=schema_name) + ) tables = [] # The second TableInfo field is 't' for table or 'v' for view. for row in results: @@ -159,8 +162,9 @@ def get_relations(self, cursor, table_name): :rtype: dict :returns: A dictionary representing column relationships to other tables. """ + schema_name = self._get_schema_name(cursor) results = cursor.run_sql_in_snapshot( - ''' + """ SELECT tc.COLUMN_NAME as col, ccu.COLUMN_NAME as ref_col, ccu.TABLE_NAME as ref_table FROM @@ -174,8 +178,11 @@ def get_relations(self, cursor, table_name): ON rc.UNIQUE_CONSTRAINT_NAME = ccu.CONSTRAINT_NAME WHERE - tc.TABLE_NAME="%s"''' - % self.connection.ops.quote_name(table_name) + tc.TABLE_SCHEMA='{schema_name}' AND tc.TABLE_NAME='{view_name}' + """.format( + schema_name=schema_name, + view_name=self.connection.ops.quote_name(table_name), + ) ) return { column: (referred_column, referred_table) @@ -194,6 +201,7 @@ def get_primary_key_column(self, cursor, table_name): :rtype: str :returns: The name of the PK column. """ + schema_name = self._get_schema_name(cursor) results = cursor.run_sql_in_snapshot( """ SELECT @@ -205,9 +213,11 @@ def get_primary_key_column(self, cursor, table_name): AS ccu ON tc.CONSTRAINT_NAME = ccu.CONSTRAINT_NAME WHERE - tc.TABLE_NAME="%s" AND tc.CONSTRAINT_TYPE='PRIMARY KEY' AND tc.TABLE_SCHEMA='' - """ - % self.connection.ops.quote_name(table_name) + tc.TABLE_NAME='{table_name}' AND tc.CONSTRAINT_TYPE='PRIMARY KEY' AND tc.TABLE_SCHEMA='{schema_name}' + """.format( + schema_name=schema_name, + table_name=self.connection.ops.quote_name(table_name), + ) ) return results[0][0] if results else None @@ -225,6 +235,7 @@ def get_constraints(self, cursor, table_name): """ constraints = {} quoted_table_name = self.connection.ops.quote_name(table_name) + schema_name = self._get_schema_name(cursor) # Firstly populate all available constraints and their columns. constraint_columns = cursor.run_sql_in_snapshot( @@ -233,8 +244,8 @@ def get_constraints(self, cursor, table_name): CONSTRAINT_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE - WHERE TABLE_NAME="{table}"'''.format( - table=quoted_table_name + WHERE TABLE_NAME="{table}" AND TABLE_SCHEMA="{schema_name}"'''.format( + table=quoted_table_name, schema_name=schema_name ) ) for constraint, column_name in constraint_columns: @@ -260,8 +271,8 @@ def get_constraints(self, cursor, table_name): FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE - TABLE_NAME="{table}"'''.format( - table=quoted_table_name + TABLE_NAME="{table}" AND TABLE_SCHEMA="{schema_name}"'''.format( + table=quoted_table_name, schema_name=schema_name ) ) for constraint, constraint_type in constraint_types: @@ -305,11 +316,11 @@ def get_constraints(self, cursor, table_name): ON idx_col.INDEX_NAME = idx.INDEX_NAME AND idx_col.TABLE_NAME="{table}" WHERE - idx.TABLE_NAME="{table}" + idx.TABLE_NAME="{table}" AND TABLE_SCHEMA="{schema_name}" ORDER BY idx_col.ORDINAL_POSITION """.format( - table=quoted_table_name + table=quoted_table_name, schema_name=schema_name ) ) for ( @@ -350,6 +361,7 @@ def get_key_columns(self, cursor, table_name): for all key columns in the given table. """ key_columns = [] + schema_name = self._get_schema_name(cursor) cursor.execute( """SELECT tc.COLUMN_NAME as column_name, @@ -366,10 +378,14 @@ def get_key_columns(self, cursor, table_name): ON rc.CONSTRAINT_NAME = ccu.CONSTRAINT_NAME WHERE - tc.TABLE_NAME="{table}" + tc.TABLE_NAME="{table}" AND TABLE_SCHEMA="{schema_name}" """.format( - table=self.connection.ops.quote_name(table_name) + table=self.connection.ops.quote_name(table_name), + schema_name=schema_name, ) ) key_columns.extend(cursor.fetchall()) return key_columns + + def _get_schema_name(self, cursor): + return cursor.connection.current_schema From 485566bbede2a91de6718846cfba00077f442960 Mon Sep 17 00:00:00 2001 From: ankiaga Date: Mon, 11 Mar 2024 10:15:03 +0530 Subject: [PATCH 02/10] Fix --- django_spanner/introspection.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/django_spanner/introspection.py b/django_spanner/introspection.py index 3495c8818e..7e91e72cc7 100644 --- a/django_spanner/introspection.py +++ b/django_spanner/introspection.py @@ -316,7 +316,7 @@ def get_constraints(self, cursor, table_name): ON idx_col.INDEX_NAME = idx.INDEX_NAME AND idx_col.TABLE_NAME="{table}" WHERE - idx.TABLE_NAME="{table}" AND TABLE_SCHEMA="{schema_name}" + idx.TABLE_NAME="{table}" AND idx.TABLE_SCHEMA="{schema_name}" ORDER BY idx_col.ORDINAL_POSITION """.format( @@ -378,7 +378,7 @@ def get_key_columns(self, cursor, table_name): ON rc.CONSTRAINT_NAME = ccu.CONSTRAINT_NAME WHERE - tc.TABLE_NAME="{table}" AND TABLE_SCHEMA="{schema_name}" + tc.TABLE_NAME="{table}" AND tc.TABLE_SCHEMA="{schema_name}" """.format( table=self.connection.ops.quote_name(table_name), schema_name=schema_name, From 9681caf0f5cc387ed62a32e2d7e42daca20950e1 Mon Sep 17 00:00:00 2001 From: ankiaga Date: Mon, 18 Mar 2024 11:39:07 +0530 Subject: [PATCH 03/10] Comments addressed --- django_spanner/introspection.py | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/django_spanner/introspection.py b/django_spanner/introspection.py index 7e91e72cc7..a2eac309c4 100644 --- a/django_spanner/introspection.py +++ b/django_spanner/introspection.py @@ -29,26 +29,14 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): TypeCode.NUMERIC: "DecimalField", TypeCode.JSON: "JSONField", } - if USE_EMULATOR: - # Emulator does not support table_type yet. - # https://github.com/GoogleCloudPlatform/cloud-spanner-emulator/issues/43 - LIST_TABLE_SQL = """ - SELECT - t.table_name, t.table_name - FROM - information_schema.tables AS t - WHERE - t.table_catalog = '' and t.table_schema = '{schema_name}' - """ - else: - LIST_TABLE_SQL = """ - SELECT - t.table_name, t.table_type - FROM - information_schema.tables AS t - WHERE - t.table_catalog = '' and t.table_schema = '{schema_name}' - """ + LIST_TABLE_SQL = """ + SELECT + t.table_name, t.table_type + FROM + information_schema.tables AS t + WHERE + t.table_catalog = '' and t.table_schema = '@schema_name' + """ def get_field_type(self, data_type, description): """A hook for a Spanner database to use the cursor description to @@ -78,7 +66,7 @@ def get_table_list(self, cursor): """ schema_name = self._get_schema_name(cursor) results = cursor.run_sql_in_snapshot( - self.LIST_TABLE_SQL.format(schema_name=schema_name) + self.LIST_TABLE_SQL, params=({"schema_name": schema_name}) ) tables = [] # The second TableInfo field is 't' for table or 'v' for view. From 9de4563316dea53b818efaa2fa880cc2fd173182 Mon Sep 17 00:00:00 2001 From: ankiaga Date: Mon, 18 Mar 2024 12:38:27 +0530 Subject: [PATCH 04/10] Comments addressed --- django_spanner/introspection.py | 60 ++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/django_spanner/introspection.py b/django_spanner/introspection.py index a2eac309c4..5c021becb8 100644 --- a/django_spanner/introspection.py +++ b/django_spanner/introspection.py @@ -35,7 +35,7 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): FROM information_schema.tables AS t WHERE - t.table_catalog = '' and t.table_schema = '@schema_name' + t.table_catalog = '' and t.table_schema = @schema_name """ def get_field_type(self, data_type, description): @@ -66,7 +66,7 @@ def get_table_list(self, cursor): """ schema_name = self._get_schema_name(cursor) results = cursor.run_sql_in_snapshot( - self.LIST_TABLE_SQL, params=({"schema_name": schema_name}) + self.LIST_TABLE_SQL, params={"schema_name": schema_name} ) tables = [] # The second TableInfo field is 't' for table or 'v' for view. @@ -166,11 +166,11 @@ def get_relations(self, cursor, table_name): ON rc.UNIQUE_CONSTRAINT_NAME = ccu.CONSTRAINT_NAME WHERE - tc.TABLE_SCHEMA='{schema_name}' AND tc.TABLE_NAME='{view_name}' - """.format( - schema_name=schema_name, - view_name=self.connection.ops.quote_name(table_name), - ) + tc.TABLE_SCHEMA='@schema_name' AND tc.TABLE_NAME='@view_name' + """, params=[ + {"schema_name": schema_name}, + {"view_name": self.connection.ops.quote_name(table_name)} + ] ) return { column: (referred_column, referred_table) @@ -201,11 +201,11 @@ def get_primary_key_column(self, cursor, table_name): AS ccu ON tc.CONSTRAINT_NAME = ccu.CONSTRAINT_NAME WHERE - tc.TABLE_NAME='{table_name}' AND tc.CONSTRAINT_TYPE='PRIMARY KEY' AND tc.TABLE_SCHEMA='{schema_name}' - """.format( - schema_name=schema_name, - table_name=self.connection.ops.quote_name(table_name), - ) + tc.TABLE_NAME=@table_name AND tc.CONSTRAINT_TYPE='PRIMARY KEY' AND tc.TABLE_SCHEMA=@schema_name + """, params=[ + {"schema_name": schema_name}, + {"table_name": self.connection.ops.quote_name(table_name)} + ] ) return results[0][0] if results else None @@ -232,9 +232,11 @@ def get_constraints(self, cursor, table_name): CONSTRAINT_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE - WHERE TABLE_NAME="{table}" AND TABLE_SCHEMA="{schema_name}"'''.format( - table=quoted_table_name, schema_name=schema_name - ) + WHERE TABLE_NAME=@table AND TABLE_SCHEMA=@schema_name''', + params=[ + {"table": quoted_table_name}, + {"schema_name": schema_name} + ] ) for constraint, column_name in constraint_columns: if constraint not in constraints: @@ -259,9 +261,10 @@ def get_constraints(self, cursor, table_name): FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE - TABLE_NAME="{table}" AND TABLE_SCHEMA="{schema_name}"'''.format( - table=quoted_table_name, schema_name=schema_name - ) + TABLE_NAME=@table AND TABLE_SCHEMA=@schema_name''', params=[ + {"table": quoted_table_name}, + {"schema_name": schema_name} + ] ) for constraint, constraint_type in constraint_types: already_added = constraint in constraints @@ -302,14 +305,15 @@ def get_constraints(self, cursor, table_name): RIGHT JOIN INFORMATION_SCHEMA.INDEX_COLUMNS AS idx_col ON - idx_col.INDEX_NAME = idx.INDEX_NAME AND idx_col.TABLE_NAME="{table}" + idx_col.INDEX_NAME = idx.INDEX_NAME AND idx_col.TABLE_NAME=@table WHERE - idx.TABLE_NAME="{table}" AND idx.TABLE_SCHEMA="{schema_name}" + idx.TABLE_NAME=@table AND idx.TABLE_SCHEMA=@schema_name ORDER BY idx_col.ORDINAL_POSITION - """.format( - table=quoted_table_name, schema_name=schema_name - ) + """, params=[ + {"table": quoted_table_name}, + {"schema_name": schema_name} + ] ) for ( index_name, @@ -366,11 +370,11 @@ def get_key_columns(self, cursor, table_name): ON rc.CONSTRAINT_NAME = ccu.CONSTRAINT_NAME WHERE - tc.TABLE_NAME="{table}" AND tc.TABLE_SCHEMA="{schema_name}" - """.format( - table=self.connection.ops.quote_name(table_name), - schema_name=schema_name, - ) + tc.TABLE_NAME=@table AND tc.TABLE_SCHEMA=@schema_name + """, params=[ + {"table": self.connection.ops.quote_name(table_name)}, + {"schema_name": schema_name} + ] ) key_columns.extend(cursor.fetchall()) return key_columns From 6ca367326c04f7d4fbf7c9afe4f2561e739b40d8 Mon Sep 17 00:00:00 2001 From: ankiaga Date: Mon, 18 Mar 2024 12:48:09 +0530 Subject: [PATCH 05/10] Comments addressed --- django_spanner/introspection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_spanner/introspection.py b/django_spanner/introspection.py index 5c021becb8..72b34d650e 100644 --- a/django_spanner/introspection.py +++ b/django_spanner/introspection.py @@ -166,7 +166,7 @@ def get_relations(self, cursor, table_name): ON rc.UNIQUE_CONSTRAINT_NAME = ccu.CONSTRAINT_NAME WHERE - tc.TABLE_SCHEMA='@schema_name' AND tc.TABLE_NAME='@view_name' + tc.TABLE_SCHEMA=@schema_name AND tc.TABLE_NAME=@view_name """, params=[ {"schema_name": schema_name}, {"view_name": self.connection.ops.quote_name(table_name)} From 1a0d8d90fd5439a3f47b6729f4637b592039030b Mon Sep 17 00:00:00 2001 From: ankiaga Date: Mon, 18 Mar 2024 13:30:26 +0530 Subject: [PATCH 06/10] Comments addressed --- django_spanner/introspection.py | 35 +++++++++------------------------ 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/django_spanner/introspection.py b/django_spanner/introspection.py index 72b34d650e..b60cb2812f 100644 --- a/django_spanner/introspection.py +++ b/django_spanner/introspection.py @@ -167,11 +167,8 @@ def get_relations(self, cursor, table_name): rc.UNIQUE_CONSTRAINT_NAME = ccu.CONSTRAINT_NAME WHERE tc.TABLE_SCHEMA=@schema_name AND tc.TABLE_NAME=@view_name - """, params=[ - {"schema_name": schema_name}, - {"view_name": self.connection.ops.quote_name(table_name)} - ] - ) + """, params={"schema_name": schema_name, "view_name": self.connection.ops.quote_name(table_name)} + ) return { column: (referred_column, referred_table) for (column, referred_column, referred_table) in results @@ -202,11 +199,8 @@ def get_primary_key_column(self, cursor, table_name): ccu ON tc.CONSTRAINT_NAME = ccu.CONSTRAINT_NAME WHERE tc.TABLE_NAME=@table_name AND tc.CONSTRAINT_TYPE='PRIMARY KEY' AND tc.TABLE_SCHEMA=@schema_name - """, params=[ - {"schema_name": schema_name}, - {"table_name": self.connection.ops.quote_name(table_name)} - ] - ) + """, params={"schema_name": schema_name, "table_name": self.connection.ops.quote_name(table_name)} + ) return results[0][0] if results else None def get_constraints(self, cursor, table_name): @@ -233,10 +227,7 @@ def get_constraints(self, cursor, table_name): FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE WHERE TABLE_NAME=@table AND TABLE_SCHEMA=@schema_name''', - params=[ - {"table": quoted_table_name}, - {"schema_name": schema_name} - ] + params={"table": quoted_table_name, "schema_name": schema_name} ) for constraint, column_name in constraint_columns: if constraint not in constraints: @@ -261,10 +252,8 @@ def get_constraints(self, cursor, table_name): FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE - TABLE_NAME=@table AND TABLE_SCHEMA=@schema_name''', params=[ - {"table": quoted_table_name}, - {"schema_name": schema_name} - ] + TABLE_NAME=@table AND TABLE_SCHEMA=@schema_name''', + params={"table": quoted_table_name, "schema_name": schema_name} ) for constraint, constraint_type in constraint_types: already_added = constraint in constraints @@ -310,10 +299,7 @@ def get_constraints(self, cursor, table_name): idx.TABLE_NAME=@table AND idx.TABLE_SCHEMA=@schema_name ORDER BY idx_col.ORDINAL_POSITION - """, params=[ - {"table": quoted_table_name}, - {"schema_name": schema_name} - ] + """, params={"table": quoted_table_name, "schema_name": schema_name} ) for ( index_name, @@ -371,10 +357,7 @@ def get_key_columns(self, cursor, table_name): rc.CONSTRAINT_NAME = ccu.CONSTRAINT_NAME WHERE tc.TABLE_NAME=@table AND tc.TABLE_SCHEMA=@schema_name - """, params=[ - {"table": self.connection.ops.quote_name(table_name)}, - {"schema_name": schema_name} - ] + """, params={"table": self.connection.ops.quote_name(table_name), "schema_name": schema_name} ) key_columns.extend(cursor.fetchall()) return key_columns From 8e868fdfb41ce772a709741d3d9a818d6d907e84 Mon Sep 17 00:00:00 2001 From: ankiaga Date: Thu, 21 Mar 2024 15:31:01 +0530 Subject: [PATCH 07/10] Removing quote method for table name --- django_spanner/introspection.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/django_spanner/introspection.py b/django_spanner/introspection.py index b60cb2812f..027acd64fb 100644 --- a/django_spanner/introspection.py +++ b/django_spanner/introspection.py @@ -167,7 +167,7 @@ def get_relations(self, cursor, table_name): rc.UNIQUE_CONSTRAINT_NAME = ccu.CONSTRAINT_NAME WHERE tc.TABLE_SCHEMA=@schema_name AND tc.TABLE_NAME=@view_name - """, params={"schema_name": schema_name, "view_name": self.connection.ops.quote_name(table_name)} + """, params={"schema_name": schema_name, "view_name": table_name} ) return { column: (referred_column, referred_table) @@ -199,7 +199,7 @@ def get_primary_key_column(self, cursor, table_name): ccu ON tc.CONSTRAINT_NAME = ccu.CONSTRAINT_NAME WHERE tc.TABLE_NAME=@table_name AND tc.CONSTRAINT_TYPE='PRIMARY KEY' AND tc.TABLE_SCHEMA=@schema_name - """, params={"schema_name": schema_name, "table_name": self.connection.ops.quote_name(table_name)} + """, params={"schema_name": schema_name, "table_name": table_name} ) return results[0][0] if results else None @@ -216,7 +216,6 @@ def get_constraints(self, cursor, table_name): :returns: A dictionary with constraints. """ constraints = {} - quoted_table_name = self.connection.ops.quote_name(table_name) schema_name = self._get_schema_name(cursor) # Firstly populate all available constraints and their columns. @@ -227,7 +226,7 @@ def get_constraints(self, cursor, table_name): FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE WHERE TABLE_NAME=@table AND TABLE_SCHEMA=@schema_name''', - params={"table": quoted_table_name, "schema_name": schema_name} + params={"table": table_name, "schema_name": schema_name} ) for constraint, column_name in constraint_columns: if constraint not in constraints: @@ -357,7 +356,7 @@ def get_key_columns(self, cursor, table_name): rc.CONSTRAINT_NAME = ccu.CONSTRAINT_NAME WHERE tc.TABLE_NAME=@table AND tc.TABLE_SCHEMA=@schema_name - """, params={"table": self.connection.ops.quote_name(table_name), "schema_name": schema_name} + """, params={"table": table_name, "schema_name": schema_name} ) key_columns.extend(cursor.fetchall()) return key_columns From 893551a9bc5a63ae85cd0937887011854715073c Mon Sep 17 00:00:00 2001 From: ankiaga Date: Thu, 21 Mar 2024 15:36:33 +0530 Subject: [PATCH 08/10] Fix --- django_spanner/introspection.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/django_spanner/introspection.py b/django_spanner/introspection.py index 027acd64fb..7be5e1e859 100644 --- a/django_spanner/introspection.py +++ b/django_spanner/introspection.py @@ -252,7 +252,7 @@ def get_constraints(self, cursor, table_name): INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME=@table AND TABLE_SCHEMA=@schema_name''', - params={"table": quoted_table_name, "schema_name": schema_name} + params={"table": table_name, "schema_name": schema_name} ) for constraint, constraint_type in constraint_types: already_added = constraint in constraints @@ -298,7 +298,7 @@ def get_constraints(self, cursor, table_name): idx.TABLE_NAME=@table AND idx.TABLE_SCHEMA=@schema_name ORDER BY idx_col.ORDINAL_POSITION - """, params={"table": quoted_table_name, "schema_name": schema_name} + """, params={"table": table_name, "schema_name": schema_name} ) for ( index_name, From 525643d5853fe439d353e215b179adcfd36e521e Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Thu, 21 Mar 2024 10:08:34 +0000 Subject: [PATCH 09/10] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20?= =?UTF-8?q?post-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- django_spanner/introspection.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/django_spanner/introspection.py b/django_spanner/introspection.py index 7be5e1e859..822d9181ca 100644 --- a/django_spanner/introspection.py +++ b/django_spanner/introspection.py @@ -167,8 +167,9 @@ def get_relations(self, cursor, table_name): rc.UNIQUE_CONSTRAINT_NAME = ccu.CONSTRAINT_NAME WHERE tc.TABLE_SCHEMA=@schema_name AND tc.TABLE_NAME=@view_name - """, params={"schema_name": schema_name, "view_name": table_name} - ) + """, + params={"schema_name": schema_name, "view_name": table_name}, + ) return { column: (referred_column, referred_table) for (column, referred_column, referred_table) in results @@ -199,8 +200,9 @@ def get_primary_key_column(self, cursor, table_name): ccu ON tc.CONSTRAINT_NAME = ccu.CONSTRAINT_NAME WHERE tc.TABLE_NAME=@table_name AND tc.CONSTRAINT_TYPE='PRIMARY KEY' AND tc.TABLE_SCHEMA=@schema_name - """, params={"schema_name": schema_name, "table_name": table_name} - ) + """, + params={"schema_name": schema_name, "table_name": table_name}, + ) return results[0][0] if results else None def get_constraints(self, cursor, table_name): @@ -220,13 +222,13 @@ def get_constraints(self, cursor, table_name): # Firstly populate all available constraints and their columns. constraint_columns = cursor.run_sql_in_snapshot( - ''' + """ SELECT CONSTRAINT_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE - WHERE TABLE_NAME=@table AND TABLE_SCHEMA=@schema_name''', - params={"table": table_name, "schema_name": schema_name} + WHERE TABLE_NAME=@table AND TABLE_SCHEMA=@schema_name""", + params={"table": table_name, "schema_name": schema_name}, ) for constraint, column_name in constraint_columns: if constraint not in constraints: @@ -245,14 +247,14 @@ def get_constraints(self, cursor, table_name): # Add the various constraints by type. constraint_types = cursor.run_sql_in_snapshot( - ''' + """ SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE - TABLE_NAME=@table AND TABLE_SCHEMA=@schema_name''', - params={"table": table_name, "schema_name": schema_name} + TABLE_NAME=@table AND TABLE_SCHEMA=@schema_name""", + params={"table": table_name, "schema_name": schema_name}, ) for constraint, constraint_type in constraint_types: already_added = constraint in constraints @@ -298,7 +300,8 @@ def get_constraints(self, cursor, table_name): idx.TABLE_NAME=@table AND idx.TABLE_SCHEMA=@schema_name ORDER BY idx_col.ORDINAL_POSITION - """, params={"table": table_name, "schema_name": schema_name} + """, + params={"table": table_name, "schema_name": schema_name}, ) for ( index_name, @@ -356,7 +359,8 @@ def get_key_columns(self, cursor, table_name): rc.CONSTRAINT_NAME = ccu.CONSTRAINT_NAME WHERE tc.TABLE_NAME=@table AND tc.TABLE_SCHEMA=@schema_name - """, params={"table": table_name, "schema_name": schema_name} + """, + params={"table": table_name, "schema_name": schema_name}, ) key_columns.extend(cursor.fetchall()) return key_columns From 78442844378fe9468b9b145eadfbb5c30b96c8a8 Mon Sep 17 00:00:00 2001 From: Ankit Agarwal <146331865+ankiaga@users.noreply.github.com> Date: Mon, 25 Mar 2024 14:38:52 +0530 Subject: [PATCH 10/10] Update django_spanner/introspection.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Knut Olav Løite --- django_spanner/introspection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_spanner/introspection.py b/django_spanner/introspection.py index 822d9181ca..c752ec303b 100644 --- a/django_spanner/introspection.py +++ b/django_spanner/introspection.py @@ -295,7 +295,7 @@ def get_constraints(self, cursor, table_name): RIGHT JOIN INFORMATION_SCHEMA.INDEX_COLUMNS AS idx_col ON - idx_col.INDEX_NAME = idx.INDEX_NAME AND idx_col.TABLE_NAME=@table + idx_col.INDEX_NAME = idx.INDEX_NAME AND idx_col.TABLE_NAME=@table AND idx_col.TABLE_SCHEMA=idx.TABLE_SCHEMA WHERE idx.TABLE_NAME=@table AND idx.TABLE_SCHEMA=@schema_name ORDER BY