Repository navigation
chore(django-google-spanner): batch DDL statements during create_test_db #19067
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,6 +57,35 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): | |
| # sql_create_inline_fk = "CONSTRAINT FK_%(to_table)s_%(to_column)s_%(from_table)s_%(from_column)s FOREIGN KEY (%(from_column_norm)s) REFERENCES %(to_table_norm)s (%(to_column_norm)s)" # noqa | ||
| sql_create_inline_fk = None | ||
|
|
||
| def __enter__(self): | ||
| super().__enter__() | ||
| self._restore_autocommit = False | ||
| if ( | ||
| getattr(self.connection, "_batch_ddl_in_schema_editor", False) | ||
| and not self.collect_sql | ||
| and not self.connection.in_atomic_block | ||
| and self.connection.get_autocommit() | ||
| ): | ||
| self.connection.set_autocommit(False) | ||
| self._restore_autocommit = True | ||
| return self | ||
|
|
||
| def __exit__(self, exc_type, exc_value, traceback): | ||
| clean_exit = False | ||
| try: | ||
| res = super().__exit__(exc_type, exc_value, traceback) | ||
| clean_exit = (exc_type is None) or bool(res) | ||
| return res | ||
| finally: | ||
| if getattr(self, "_restore_autocommit", False): | ||
| try: | ||
| if clean_exit: | ||
| self.connection.commit() | ||
| else: | ||
| self.connection.rollback() | ||
| finally: | ||
| self.connection.set_autocommit(True) | ||
|
Comment on lines
+73
to
+87
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If an exception occurs during def __exit__(self, exc_type, exc_value, traceback):
has_exception = exc_type is not None
try:
return super().__exit__(exc_type, exc_value, traceback)
except Exception:
has_exception = True
raise
finally:
if getattr(self, "_restore_autocommit", False):
try:
if has_exception:
self.connection.rollback()
finally:
self.connection.set_autocommit(True)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. set_autocommit(True) only triggers a flush of the buffered DDL statements in google-cloud-spanner >= 3.72.0. So we should probably also update the minimum version required for Django. |
||
|
|
||
| def create_model(self, model): | ||
| """ | ||
| Create a table and any accompanying indexes or unique constraints for | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # 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 | ||
| from unittest import mock | ||
|
|
||
| from django.db.backends.base.creation import BaseDatabaseCreation | ||
|
|
||
| from django_spanner.creation import DatabaseCreation | ||
| from tests.unit.django_spanner.simple_test import SpannerSimpleTestClass | ||
|
|
||
|
|
||
| class TestCreation(SpannerSimpleTestClass): | ||
| def test_create_test_db_enables_ddl_batching_and_returns_db_name(self): | ||
| creation = DatabaseCreation(self.connection) | ||
| observed_flags = [] | ||
|
|
||
| def fake_super_create_test_db(*args, **kwargs): | ||
| observed_flags.append( | ||
| getattr(self.connection, "_batch_ddl_in_schema_editor", False) | ||
| ) | ||
| return "test_database_id" | ||
|
|
||
| with mock.patch.object( | ||
| BaseDatabaseCreation, | ||
| "create_test_db", | ||
| side_effect=fake_super_create_test_db, | ||
| ): | ||
| result = creation.create_test_db(verbosity=0, autoclobber=True) | ||
|
|
||
| self.assertEqual(result, "test_database_id") | ||
| self.assertEqual(observed_flags, [True]) | ||
| self.assertFalse(self.connection._batch_ddl_in_schema_editor) | ||
|
|
||
| def test_create_test_db_resets_ddl_batching_on_error(self): | ||
| creation = DatabaseCreation(self.connection) | ||
|
|
||
| with mock.patch.object( | ||
| BaseDatabaseCreation, | ||
| "create_test_db", | ||
| side_effect=RuntimeError("boom"), | ||
| ): | ||
| with self.assertRaises(RuntimeError): | ||
| creation.create_test_db(verbosity=0, autoclobber=True) | ||
|
|
||
| self.assertFalse(self.connection._batch_ddl_in_schema_editor) | ||
|
|
||
| def test_create_test_db_marks_skips_when_env_set(self): | ||
| creation = DatabaseCreation(self.connection) | ||
|
|
||
| with ( | ||
| mock.patch.dict(os.environ, {"RUNNING_SPANNER_BACKEND_TESTS": "1"}), | ||
| mock.patch.object(creation, "mark_skips") as mock_mark_skips, | ||
| mock.patch.object( | ||
| BaseDatabaseCreation, | ||
| "create_test_db", | ||
| return_value="test_database_id", | ||
| ), | ||
| ): | ||
| result = creation.create_test_db(verbosity=0, autoclobber=True) | ||
|
|
||
| self.assertEqual(result, "test_database_id") | ||
| mock_mark_skips.assert_called_once_with() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit of a code smell. It introduces a private flag that is currently only used by tests, but that someone who inspects the code could find and set, and then it will also be used in production. Can we try to make a design that makes a clear choice between one of the following:
A test-only solution could look like this:
Or if we want to support this publicly for both tests and applications, then we could add an option to DATABASES:
I would personally lean towards making it a public option, and maybe disable it by default in the first release, and consider making it the default in a follow-up release.