Repository navigation
chore(django-google-spanner): enable check constraints on Spanner Emulator - #19094
Conversation
There was a problem hiding this comment.
Code Review
This pull request enables support for column and table check constraints by default (removing emulator-specific bypasses), filters out synthetic CK_IS_NOT_NULL_* constraints during introspection, and updates identifier quoting to handle the '>' character. It also updates Nox configurations to run system tests against the emulator across multiple Django versions. The review feedback highlights critical issues where CheckConstraint was incorrectly updated to use 'condition' instead of 'check', which will cause runtime TypeErrors. Additionally, it is recommended to safely traverse the exception chain in tests rather than directly chaining 'cause' to prevent potential AttributeErrors.
…lator The Cloud Spanner Django backend previously disabled column and table check constraints when running against the Spanner Emulator, which caused inconsistent DDL and led `alter_field` migrations to emit `"None;"` on the emulator. The Spanner Emulator now supports creating, altering, introspecting, and enforcing `CHECK` constraints, so check-constraint support is now enabled unconditionally across both live Spanner and the emulator. This change also: - Filters Spanner's internal synthetic `NOT NULL` check constraints out of constraint introspection so altering a non-nullable field to remove a check constraint does not attempt to drop an internal constraint. - Updates Django backend test skips and test-only identifier sanitization for newly enabled upstream tests that rely on unsupported Spanner features (column renames, JSON equality, and `SELECT ... WHERE` without a `FROM` clause). - Updates the emulator integration workflow and nox configuration to run the package system tests against the emulator on Python 3.12 and 3.14 (covering Django 5.2 and 6.0), re-enables the check-constraint system test on the emulator, and temporarily skips the failing JSON lookup system test (tracked in #19088).
2ee210d to
e33f4d3
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request enables support for column and table check constraints unconditionally, removing emulator-specific exclusions since the Spanner emulator now supports them. It also updates constraint introspection to filter out synthetic CK_IS_NOT_NULL_ constraints, adjusts identifier quoting to handle the > operator during backend tests, and updates the test suite accordingly. The reviewer suggested expanding the identifier sanitization in quote_name to also replace other comparison operators like <, =, and ! with underscores to make it more robust.
| 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 ( |
There was a problem hiding this comment.
We support FOREIGN KEY right? It wasn't supported in Emulator before.
There was a problem hiding this comment.
There was a problem hiding this comment.
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.
| return name | ||
| if os.environ.get("RUNNING_SPANNER_BACKEND_TESTS") == "1": | ||
| name = name.replace(" ", "_").replace("-", "_") | ||
| name = name.replace(" ", "_").replace("-", "_").replace(">", "_") |
There was a problem hiding this comment.
" ", "-", 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"
The Cloud Spanner Django backend previously disabled column and table check constraints when running against the Spanner Emulator, which caused inconsistent DDL and led
alter_fieldmigrations to emit"None;"on the emulator.The Spanner Emulator now supports creating, altering, introspecting, and enforcing
CHECKconstraints, so check-constraint support is now enabled unconditionally across both live Spanner and the emulator.This change also:
NOT NULLcheck constraints out of constraint introspection so altering a non-nullable field to remove a check constraint does not attempt to drop an internal constraint.SELECT ... WHEREwithout aFROMclause).