Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

on:
pull_request:
paths:
- 'packages/django-google-spanner/**'
- '.github/workflows/django-spanner-integration-tests-against-emulator-3.10.yml'
- '.github/workflows/django-spanner-integration-tests-against-emulator.yml'
push:
branches:
- main
paths:
- 'packages/django-google-spanner/**'
- '.github/workflows/django-spanner-integration-tests-against-emulator-3.10.yml'
- '.github/workflows/django-spanner-integration-tests-against-emulator.yml'

defaults:
run:
working-directory: packages/django-google-spanner

name: Run Django Spanner integration tests against emulator 3.10
name: Run Django Spanner integration tests against emulator
jobs:
check_changes:
runs-on: ubuntu-latest
Expand All @@ -33,7 +37,7 @@ jobs:
filters: |
django_spanner:
- 'packages/django-google-spanner/**'
- '.github/workflows/django-spanner-integration-tests-against-emulator-3.10.yml'
- '.github/workflows/django-spanner-integration-tests-against-emulator.yml'

system-tests:
needs: check_changes
Expand All @@ -42,7 +46,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.10", "3.12"]
python-version: ["3.12", "3.14"]

services:
emulator:
Expand All @@ -63,7 +67,7 @@ jobs:
- name: Install nox
run: python -m pip install nox
- name: Run nox
run: nox -s "unit-${{ matrix.python-version }}"
run: nox -s "system-${{ matrix.python-version }}"
env:
SPANNER_EMULATOR_HOST: localhost:9010
GOOGLE_CLOUD_PROJECT: emulator-test-project
Expand Down
25 changes: 16 additions & 9 deletions packages/django-google-spanner/django_spanner/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
from django.db.backends.base.features import BaseDatabaseFeatures
from django.db.utils import InterfaceError

from django_spanner import USE_EMULATOR


class DatabaseFeatures(BaseDatabaseFeatures):
can_introspect_big_integer_field = False
Expand Down Expand Up @@ -56,13 +54,8 @@ def introspected_field_types(self):
supports_sequence_reset = False
supports_timezones = False
supports_transactions = True
if USE_EMULATOR:
# Emulator does not support check constraints.
supports_column_check_constraints = False
supports_table_check_constraints = False
else:
supports_column_check_constraints = True
supports_table_check_constraints = True
supports_column_check_constraints = True
supports_table_check_constraints = True
has_native_json_field = True
supports_json_field = True
supports_primitives_in_json_field = True
Expand Down Expand Up @@ -113,6 +106,15 @@ def introspected_field_types(self):
"constraints.tests.UniqueConstraintTests.test_validate_expression_condition",
"constraints.tests.UniqueConstraintTests.test_validate_expression_generated_field_stored",
"constraints.tests.UniqueConstraintTests.test_validate_fields_generated_field_stored_nulls_distinct",
"constraints.tests.UniqueConstraintTests.test_validate_nullable_textfield_with_isnull_true",
# Schema tests
"schema.tests.SchemaTests.test_check_constraint_exact_jsonfield",
"schema.tests.SchemaTests.test_rename_field_with_check_to_truncated_name",
# Model Fields tests
"model_fields.test_generatedfield.StoredGeneratedFieldTests.test_full_clean_with_check_constraint",
# Validation tests
"validation.test_constraints.PerformConstraintChecksTest.test_full_clean_with_check_constraints",
"validation.test_constraints.PerformConstraintChecksTest.test_full_clean_with_check_constraints_on_child_model",
# Custom PK tests
"custom_pk.tests.CustomPKTests.test_auto_field_subclass_create",
# Datetimes tests
Expand Down Expand Up @@ -2331,6 +2333,11 @@ def introspected_field_types(self):
"model_fields.test_jsonfield.TestQuerying.test_deep_negative_lookup_array",
"model_fields.test_jsonfield.TestQuerying.test_deep_negative_lookup_mixed",
"model_fields.test_jsonfield.TestQuerying.test_shallow_list_negative_lookup",
# CheckConstraint.validate() uses Q.check() (SELECT 1 WHERE ... without FROM),
# which Spanner rejects and poisons the atomic block.
"foreign_object.tests.ForeignObjectModelValidationTests.test_validate_constraints_with_foreign_object",
"model_forms.tests.ConstraintValidationTests.test_check_constraint_refs_excluded_field",
"model_forms.tests.ConstraintValidationTests.test_unique_constraint_refs_excluded_field",
)

if django.VERSION >= (6, 0):
Expand Down
11 changes: 8 additions & 3 deletions packages/django-google-spanner/django_spanner/introspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,14 @@ def get_constraints(self, cursor, table_name):
)
for constraint, constraint_type in constraint_types:
already_added = constraint in constraints
if constraint_type == "FOREIGN KEY":
# We don't yet support anything related to FOREIGN KEY.
# See https://github.com/googleapis/python-spanner-django/issues/313.
if constraint_type == "FOREIGN KEY" or (

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We support FOREIGN KEY right? It wasn't supported in Emulator before.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the emulator supports foreign keys, but this pull request intentionally limits the scope to only fixing CHECK constraints. I've updated the TODO to better explain what the current status is, and added a tracking bug for foreign keys.

constraint_type == "CHECK" and constraint.startswith("CK_IS_NOT_NULL_")
):
# TODO(https://github.com/googleapis/google-cloud-python/issues/19145):
# Foreign key introspection is not yet implemented in this
# Django backend (can_introspect_foreign_keys = False), and
# CK_IS_NOT_NULL_* entries are Spanner's synthetic NOT NULL
# constraints that cannot be dropped via DROP CONSTRAINT.
if already_added:
del constraints[constraint]
continue
Expand Down
11 changes: 6 additions & 5 deletions packages/django-google-spanner/django_spanner/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,11 @@ def quote_name(self, name: str) -> str:
backticks, backslashes, or ASCII control characters) are escaped and
wrapped in backticks as a single identifier.

Spanner does not allow spaces or hyphens in table or index names, even
when quoted. Only when running Django's own backend test suite
(``RUNNING_SPANNER_BACKEND_TESTS=1``) are spaces and hyphens replaced
with underscores so Django test models that use such names can run.
Spanner does not allow spaces, hyphens, or '>' in table, index, or
constraint names, even when quoted. Only when running Django's own
backend test suite (``RUNNING_SPANNER_BACKEND_TESTS=1``) are those
characters replaced with underscores so Django test models that use
such names can run.

:type name: str
:param name: The table, index, or column name to quote.
Expand All @@ -154,7 +155,7 @@ def quote_name(self, name: str) -> str:
if not name:
return name
if os.environ.get("RUNNING_SPANNER_BACKEND_TESTS") == "1":
name = name.replace(" ", "_").replace("-", "_")
name = name.replace(" ", "_").replace("-", "_").replace(">", "_")
Comment thread
olavloite marked this conversation as resolved.
Resolved

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why >

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

" ", "-", and ">" are all tokens that Spanner does not allow in identifiers (e.g. table names or constraint names). Normally, if an application tries to use any of those for a table name or constraint name, then we just send them to Spanner and let Spanner return an error. However, these specific replacements are applied only when running the standard Django tests (the ones defined in Django itself). Previously, we only replaced " " and "-", as those were the only ones that were actually used by those tests. Now that we also run the tests for CHECK constraints, we need to replace ">" as well, as the tests contain a constraint that has the name "Generated model check constraint a > 0"

if _QUOTED_IDENTIFIER_RE.match(name):
return name
# TODO(https://github.com/googleapis/google-cloud-python/issues/19072):
Expand Down
10 changes: 0 additions & 10 deletions packages/django-google-spanner/django_spanner/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.models.fields import NOT_PROVIDED

from django_spanner import USE_EMULATOR
from django_spanner._opentelemetry_tracing import trace_call


Expand Down Expand Up @@ -563,15 +562,6 @@ def _alter_column_type_sql(
[],
)

def _check_sql(self, name, check):
# Emulator does not support check constraints yet.
if USE_EMULATOR:
return None
return self.sql_constraint % {
"name": self.quote_name(name),
"constraint": self.sql_check_constraint % {"check": check},
}

def _unique_sql(
self,
model,
Expand Down
34 changes: 20 additions & 14 deletions packages/django-google-spanner/noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,16 @@ def lint_setup_py(session):
session.run("python", "setup.py", "check", "--restructuredtext", "--strict")


def default(session, django_version="5.2"):
def _resolve_django_dependency(session, django_version):
# Django 6.0 dropped support for Python 3.10 and 3.11 (requires Python >= 3.12)
if django_version == "6.0" and session.python in ("3.10", "3.11"):
session.skip(f"Django {django_version} requires Python >= 3.12")
return f"django~={django_version}.0"


def default(session, django_version="5.2"):
# Target specific Django version
django_dep = (
f"django~={django_version}.0" if django_version == "6.0" else "django~=5.2.0"
)
django_dep = _resolve_django_dependency(session, django_version)

# Install all test dependencies, then install this package in-place.
session.install(
Expand Down Expand Up @@ -184,8 +185,10 @@ def mockserver(session, django):
default(session, django_version=django)


def system_test(session):
def system_test(session, django_version="5.2"):
"""Run the system test suite."""
django_dep = _resolve_django_dependency(session, django_version)

constraints_path = str(
CURRENT_DIRECTORY / "testing" / f"constraints-{session.python}.txt"
)
Expand Down Expand Up @@ -215,7 +218,7 @@ def system_test(session):
# Install all test dependencies, then install this package into the
# virtualenv's dist-packages.
session.install(
"django~=5.2",
django_dep,
"mock",
"pytest",
"google-cloud-testutils",
Expand All @@ -231,14 +234,17 @@ def system_test(session):


@nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS)
def system(session):
# TODO(https://github.com/googleapis/google-cloud-python/issues/16099):
# System tests were disabled in the split repo to speed up the build process.
# See: https://github.com/googleapis/python-spanner-django/pull/929.
session.skip("System tests are temporarily disabled.")

print("System tests with django 5.2")
system_test(session)
@nox.parametrize("django", DJANGO_VERSIONS)
def system(session, django):
if not os.environ.get("SPANNER_EMULATOR_HOST", ""):
# TODO(https://github.com/googleapis/google-cloud-python/issues/16099):
# System tests were disabled in the split repo to speed up the build
# process against live Spanner.
# See: https://github.com/googleapis/python-spanner-django/pull/929.
session.skip("System tests against live Spanner are temporarily disabled.")

print(f"System tests with django {django}")
system_test(session, django_version=django)


@nox.session(python=DEFAULT_PYTHON_VERSION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
# https://developers.google.com/open-source/licenses/bsd

import datetime
import unittest

from django.db import connection
from django.db.utils import IntegrityError
from django.test import TransactionTestCase
from django.utils import timezone

from django_spanner import USE_EMULATOR
from tests.system.django_spanner.utils import (
setup_database,
setup_instance,
Expand All @@ -23,7 +21,6 @@
from .models import Event


@unittest.skipIf(USE_EMULATOR, "Check Constraint is not implemented in emulator.")
class TestCheckConstraint(TransactionTestCase):
@classmethod
def setUpClass(cls):
Expand Down Expand Up @@ -61,8 +58,12 @@ def test_insert_invalid_value(self):
now = timezone.now()
now_minus_1_day = now - timezone.timedelta(days=1)
event_invalid = Event(start_date=now, end_date=now_minus_1_day)
with self.assertRaises(IntegrityError):
with self.assertRaisesRegex(IntegrityError, "ConstraintError") as context:
event_invalid.save()
cause = context.exception
while cause.__cause__ is not None:
cause = cause.__cause__
self.assertIn("check_start_date", str(cause))


class TestCheckConstraintValidation(TransactionTestCase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd

import unittest

from django.db import connection, models
from django.test import TransactionTestCase

Expand All @@ -17,6 +19,10 @@
from .models import Detail


@unittest.skip(
"TODO(https://github.com/googleapis/google-cloud-python/issues/19088): "
"Re-enable once JSONField key-transform equality lookup is fixed."
)
class TestJsonField(TransactionTestCase):
@classmethod
def setUpClass(cls):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def test_spanner_specific_feature_flags(self):
self.assertTrue(features.supports_json_field)
self.assertTrue(features.supports_primitives_in_json_field)
self.assertFalse(features.supports_json_field_contains)
self.assertTrue(features.supports_column_check_constraints)
self.assertTrue(features.supports_table_check_constraints)
self.assertEqual(features.bare_select_suffix, " FROM UNNEST([1])")

def test_check_constraint_validate_not_skipped(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,42 @@ def run_sql_in_snapshot(*args, **kwargs):
},
},
)

def test_get_constraints_excludes_synthetic_not_null_check(self):
"""Tests get_constraints filters synthetic CK_IS_NOT_NULL_* CHECK constraints."""
db_introspection = DatabaseIntrospection(self.connection)
cursor = mock.MagicMock()

def run_sql_in_snapshot(sql, *args, **kwargs):
if "CONSTRAINT_NAME, COLUMN_NAME" in sql:
return [
["CK_IS_NOT_NULL_Table_1_age", "age"],
["chk_age_non_negative", "age"],
]
if "CONSTRAINT_NAME, CONSTRAINT_TYPE" in sql:
return [
["CK_IS_NOT_NULL_Table_1_age", "CHECK"],
["chk_age_non_negative", "CHECK"],
]
return []

cursor.run_sql_in_snapshot = run_sql_in_snapshot
constraints = db_introspection.get_constraints(
cursor=cursor, table_name="Table_1"
)

self.assertEqual(
constraints,
{
"chk_age_non_negative": {
"check": True,
"columns": ["age"],
"foreign_key": None,
"index": False,
"orders": [],
"primary_key": False,
"type": None,
"unique": False,
},
},
)
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@ def test_quote_name_running_spanner_backend_tests(self):
self.assertEqual(self.db_operations.quote_name("`already`"), "`already`")
self.assertEqual(self.db_operations.quote_name(""), "")
self.assertEqual(self.db_operations.quote_name(" "), "___")
self.assertEqual(
self.db_operations.quote_name("Generated model check constraint a > 0"),
"Generated_model_check_constraint_a___0",
)
for sample in ("my col-name", "SELECT", "`a b-c`", "a b\nc"):
once = self.db_operations.quote_name(sample)
self.assertEqual(self.db_operations.quote_name(once), once)
Expand All @@ -293,6 +297,10 @@ def test_quote_name_running_spanner_backend_tests(self):
self.assertEqual(
self.db_operations.quote_name("my col-name"), "`my col-name`"
)
self.assertEqual(
self.db_operations.quote_name("a > 0"),
"`a > 0`",
)

with mock.patch.dict("django_spanner.operations.os.environ", {}, clear=True):
self.assertEqual(
Expand Down
Loading
Loading