From 7795c8c43f765053457cd4fd30aee39ab8441fc8 Mon Sep 17 00:00:00 2001 From: IlyaFaer Date: Tue, 27 Sep 2022 11:33:41 +0400 Subject: [PATCH 1/9] feat: mark foreign keys as supported --- django_spanner/features.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/django_spanner/features.py b/django_spanner/features.py index bcd6621c4f..e918164979 100644 --- a/django_spanner/features.py +++ b/django_spanner/features.py @@ -23,7 +23,7 @@ class DatabaseFeatures(BaseDatabaseFeatures): has_case_insensitive_like = False # https://cloud.google.com/spanner/quotas#query_limits max_query_params = 900 - supports_foreign_keys = False + supports_foreign_keys = True can_create_inline_fk = False supports_ignore_conflicts = False supports_partial_indexes = False @@ -1343,7 +1343,6 @@ class DatabaseFeatures(BaseDatabaseFeatures): "many_to_one.tests.ManyToOneTests.test_add_after_prefetch", # noqa "many_to_one.tests.ManyToOneTests.test_add_then_remove_after_prefetch", # noqa "many_to_one.tests.ManyToOneTests.test_cached_foreign_key_with_to_field_not_cleared_by_save", # noqa - "many_to_one.tests.ManyToOneTests.test_multiple_foreignkeys", # noqa "many_to_one.tests.ManyToOneTests.test_reverse_foreign_key_instance_to_field_caching", # noqa "many_to_one.tests.ManyToOneTests.test_set_after_prefetch", # noqa "many_to_one_null.tests.ManyToOneNullTests.test_add_efficiency", # noqa @@ -1509,7 +1508,6 @@ class DatabaseFeatures(BaseDatabaseFeatures): "ordering.tests.OrderingTests.test_stop_slicing", # noqa "ordering.tests.OrderingTests.test_stop_start_slicing", # noqa "queries.test_bulk_update.BulkUpdateNoteTests.test_batch_size", # noqa - "queries.test_bulk_update.BulkUpdateNoteTests.test_foreign_keys_do_not_lookup", # noqa "queries.test_bulk_update.BulkUpdateNoteTests.test_functions", # noqa "queries.test_bulk_update.BulkUpdateNoteTests.test_set_field_to_null", # noqa "queries.test_bulk_update.BulkUpdateNoteTests.test_set_mixed_fields_to_null", # noqa From 20fb91975f7960684afaa3f41aa37f6a266abb81 Mon Sep 17 00:00:00 2001 From: IlyaFaer Date: Fri, 30 Sep 2022 10:43:20 +0400 Subject: [PATCH 2/9] feat: add foreign keys template --- django_spanner/schema.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/django_spanner/schema.py b/django_spanner/schema.py index a1dcf05a71..6ac741eeea 100644 --- a/django_spanner/schema.py +++ b/django_spanner/schema.py @@ -21,7 +21,10 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): "CREATE TABLE %(table)s (%(definition)s) PRIMARY KEY(%(primary_key)s)" ) sql_delete_table = "DROP TABLE %(table)s" - sql_create_fk = None + sql_create_fk = sql_create_fk = ( + "ALTER TABLE %(table)s ADD CONSTRAINT %(name)s FOREIGN KEY (%(column)s) " + "REFERENCES %(to_table)s (%(to_column)s)" + ) # Spanner doesn't support partial indexes. This string omits the # %(condition)s placeholder so that partial indexes are ignored. sql_create_index = ( From 16211a51d431e7bdc9628eafbc592dae71ac45bf Mon Sep 17 00:00:00 2001 From: IlyaFaer Date: Fri, 14 Oct 2022 11:34:30 +0400 Subject: [PATCH 3/9] feat: support foreign keys --- django_spanner/schema.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/django_spanner/schema.py b/django_spanner/schema.py index 6ac741eeea..386c1e14fe 100644 --- a/django_spanner/schema.py +++ b/django_spanner/schema.py @@ -3,8 +3,9 @@ # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file or at # https://developers.google.com/open-source/licenses/bsd - +import os import uuid + from django.db import NotSupportedError from django.db.backends.base.schema import BaseDatabaseSchemaEditor from django_spanner._opentelemetry_tracing import trace_call @@ -21,10 +22,13 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): "CREATE TABLE %(table)s (%(definition)s) PRIMARY KEY(%(primary_key)s)" ) sql_delete_table = "DROP TABLE %(table)s" - sql_create_fk = sql_create_fk = ( - "ALTER TABLE %(table)s ADD CONSTRAINT %(name)s FOREIGN KEY (%(column)s) " - "REFERENCES %(to_table)s (%(to_column)s)" - ) + if os.environ.get("TEST_DBNAME"): + sql_create_fk = sql_create_fk = ( + "ALTER TABLE %(table)s ADD CONSTRAINT %(name)s FOREIGN KEY (%(column)s) " + "REFERENCES %(to_table)s (%(to_column)s)" + ) + else: + sql_create_fk = None # Spanner doesn't support partial indexes. This string omits the # %(condition)s placeholder so that partial indexes are ignored. sql_create_index = ( From 2e80b541592566532bf5e813ae406e203166341f Mon Sep 17 00:00:00 2001 From: IlyaFaer Date: Fri, 14 Oct 2022 11:37:45 +0400 Subject: [PATCH 4/9] fix typo --- django_spanner/schema.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_spanner/schema.py b/django_spanner/schema.py index 386c1e14fe..4929263e9d 100644 --- a/django_spanner/schema.py +++ b/django_spanner/schema.py @@ -23,7 +23,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): ) sql_delete_table = "DROP TABLE %(table)s" if os.environ.get("TEST_DBNAME"): - sql_create_fk = sql_create_fk = ( + sql_create_fk = ( "ALTER TABLE %(table)s ADD CONSTRAINT %(name)s FOREIGN KEY (%(column)s) " "REFERENCES %(to_table)s (%(to_column)s)" ) From 296a84af1daf181b47d0a2f7feaf49b6955c1386 Mon Sep 17 00:00:00 2001 From: IlyaFaer Date: Fri, 14 Oct 2022 11:45:12 +0400 Subject: [PATCH 5/9] Use empty line for fk template. --- django_spanner/schema.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_spanner/schema.py b/django_spanner/schema.py index 4929263e9d..f610a40a64 100644 --- a/django_spanner/schema.py +++ b/django_spanner/schema.py @@ -28,7 +28,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): "REFERENCES %(to_table)s (%(to_column)s)" ) else: - sql_create_fk = None + sql_create_fk = "" # Spanner doesn't support partial indexes. This string omits the # %(condition)s placeholder so that partial indexes are ignored. sql_create_index = ( From 265463183e8cb3ea97a3b8442902485a1b52e01e Mon Sep 17 00:00:00 2001 From: IlyaFaer Date: Fri, 14 Oct 2022 12:51:06 +0400 Subject: [PATCH 6/9] fix typo --- django_spanner/schema.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/django_spanner/schema.py b/django_spanner/schema.py index f610a40a64..c001420dfd 100644 --- a/django_spanner/schema.py +++ b/django_spanner/schema.py @@ -23,12 +23,12 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): ) sql_delete_table = "DROP TABLE %(table)s" if os.environ.get("TEST_DBNAME"): + sql_create_fk = "" + else: sql_create_fk = ( "ALTER TABLE %(table)s ADD CONSTRAINT %(name)s FOREIGN KEY (%(column)s) " "REFERENCES %(to_table)s (%(to_column)s)" ) - else: - sql_create_fk = "" # Spanner doesn't support partial indexes. This string omits the # %(condition)s placeholder so that partial indexes are ignored. sql_create_index = ( From d0b49a5a2bd8fa95bbb57adfa69a5c8eb9aef44a Mon Sep 17 00:00:00 2001 From: IlyaFaer Date: Fri, 14 Oct 2022 12:55:15 +0400 Subject: [PATCH 7/9] fix --- django_spanner/features.py | 5 ++++- django_spanner/schema.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/django_spanner/features.py b/django_spanner/features.py index e918164979..1506003ce7 100644 --- a/django_spanner/features.py +++ b/django_spanner/features.py @@ -23,7 +23,10 @@ class DatabaseFeatures(BaseDatabaseFeatures): has_case_insensitive_like = False # https://cloud.google.com/spanner/quotas#query_limits max_query_params = 900 - supports_foreign_keys = True + if os.environ.get("TEST_DBNAME"): + supports_foreign_keys = False + else: + supports_foreign_keys = True can_create_inline_fk = False supports_ignore_conflicts = False supports_partial_indexes = False diff --git a/django_spanner/schema.py b/django_spanner/schema.py index c001420dfd..06d6e21ab1 100644 --- a/django_spanner/schema.py +++ b/django_spanner/schema.py @@ -23,7 +23,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): ) sql_delete_table = "DROP TABLE %(table)s" if os.environ.get("TEST_DBNAME"): - sql_create_fk = "" + sql_create_fk = None else: sql_create_fk = ( "ALTER TABLE %(table)s ADD CONSTRAINT %(name)s FOREIGN KEY (%(column)s) " From 3405b7ea72ee946621a75b52fb4d06944c4922ba Mon Sep 17 00:00:00 2001 From: IlyaFaer Date: Fri, 14 Oct 2022 13:00:11 +0400 Subject: [PATCH 8/9] fix --- django_spanner/schema.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_spanner/schema.py b/django_spanner/schema.py index 06d6e21ab1..44081e90aa 100644 --- a/django_spanner/schema.py +++ b/django_spanner/schema.py @@ -22,7 +22,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): "CREATE TABLE %(table)s (%(definition)s) PRIMARY KEY(%(primary_key)s)" ) sql_delete_table = "DROP TABLE %(table)s" - if os.environ.get("TEST_DBNAME"): + if os.environ.get("RUNNING_SPANNER_BACKEND_TESTS"): sql_create_fk = None else: sql_create_fk = ( From fb04b531ef598e6070289f11ab9aebd72ce4bc30 Mon Sep 17 00:00:00 2001 From: IlyaFaer Date: Fri, 14 Oct 2022 13:04:22 +0400 Subject: [PATCH 9/9] fix --- django_spanner/features.py | 2 +- django_spanner/schema.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/django_spanner/features.py b/django_spanner/features.py index 1506003ce7..9252c8284e 100644 --- a/django_spanner/features.py +++ b/django_spanner/features.py @@ -23,7 +23,7 @@ class DatabaseFeatures(BaseDatabaseFeatures): has_case_insensitive_like = False # https://cloud.google.com/spanner/quotas#query_limits max_query_params = 900 - if os.environ.get("TEST_DBNAME"): + if os.environ.get("RUNNING_SPANNER_BACKEND_TESTS") == "1": supports_foreign_keys = False else: supports_foreign_keys = True diff --git a/django_spanner/schema.py b/django_spanner/schema.py index 44081e90aa..eb82ab689d 100644 --- a/django_spanner/schema.py +++ b/django_spanner/schema.py @@ -22,7 +22,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): "CREATE TABLE %(table)s (%(definition)s) PRIMARY KEY(%(primary_key)s)" ) sql_delete_table = "DROP TABLE %(table)s" - if os.environ.get("RUNNING_SPANNER_BACKEND_TESTS"): + if os.environ.get("RUNNING_SPANNER_BACKEND_TESTS") == "1": sql_create_fk = None else: sql_create_fk = (