Skip to content

Commit

Permalink
feat!: Support Django 4.2 (#865)
Browse files Browse the repository at this point in the history
* feat: Changes for Django 4.2

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* Adding allow_transactions_in_auto_commit property

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* Comments incorporated

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* Update README.rst

Co-authored-by: Knut Olav Løite <koloite@gmail.com>

* Update README.rst

Co-authored-by: Knut Olav Løite <koloite@gmail.com>

* Update README.rst

Co-authored-by: Knut Olav Løite <koloite@gmail.com>

* Update django_spanner/base.py

Co-authored-by: Knut Olav Løite <koloite@gmail.com>

* Comments incorporated

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: Knut Olav Løite <koloite@gmail.com>
  • Loading branch information
3 people authored Apr 25, 2024
1 parent 75a368e commit 75cd9bc
Show file tree
Hide file tree
Showing 29 changed files with 1,299 additions and 773 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
branches:
- main
pull_request:
name: django-tests0
name: django4.2-tests0
jobs:
system-tests:
runs-on: ubuntu-latest
Expand All @@ -22,11 +22,11 @@ jobs:
with:
python-version: 3.8
- name: Run Django tests
run: sh django_test_suite.sh
run: sh django_test_suite_4.2.sh
env:
SPANNER_EMULATOR_HOST: localhost:9010
GOOGLE_CLOUD_PROJECT: emulator-test-project
GOOGLE_CLOUD_TESTS_CREATE_SPANNER_INSTANCE: true
RUNNING_SPANNER_BACKEND_TESTS: 1
SPANNER_TEST_INSTANCE: google-cloud-django-backend-tests
DJANGO_TEST_APPS: admin_changelist admin_ordering aggregation choices distinct_on_fields expressions_window fixtures_model_package datetimes custom_methods generic_inline_admin field_defaults datatypes empty m2o_recursive many_to_one_null migration_test_data_persistence admin_docs invalid_models_tests migrate_signals model_forms.test_uuid model_forms.test_modelchoicefield syndication_tests view_tests update test_utils select_related_onetoone sessions_tests
DJANGO_TEST_APPS: admin_changelist admin_ordering aggregation distinct_on_fields expressions_window fixtures_model_package datetimes custom_methods generic_inline_admin field_defaults datatypes empty m2o_recursive many_to_one_null migration_test_data_persistence admin_docs invalid_models_tests migrate_signals model_forms.test_uuid model_forms.test_modelchoicefield syndication_tests view_tests update test_utils select_related_onetoone sessions_tests
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
branches:
- main
pull_request:
name: django-tests1
name: django4.2-tests1
jobs:
system-tests:
runs-on: ubuntu-latest
Expand All @@ -22,7 +22,7 @@ jobs:
with:
python-version: 3.8
- name: Run Django tests
run: sh django_test_suite.sh
run: sh django_test_suite_4.2.sh
env:
SPANNER_EMULATOR_HOST: localhost:9010
GOOGLE_CLOUD_PROJECT: emulator-test-project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
branches:
- main
pull_request:
name: django-tests2
name: django4.2-tests2
jobs:
system-tests:
runs-on: ubuntu-latest
Expand All @@ -22,7 +22,7 @@ jobs:
with:
python-version: 3.8
- name: Run Django tests
run: sh django_test_suite.sh
run: sh django_test_suite_4.2.sh
env:
SPANNER_EMULATOR_HOST: localhost:9010
GOOGLE_CLOUD_PROJECT: emulator-test-project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
branches:
- main
pull_request:
name: django-tests3
name: django4.2-tests3
jobs:
system-tests:
runs-on: ubuntu-latest
Expand All @@ -22,7 +22,7 @@ jobs:
with:
python-version: 3.8
- name: Run Django tests
run: sh django_test_suite.sh
run: sh django_test_suite_4.2.sh
env:
SPANNER_EMULATOR_HOST: localhost:9010
GOOGLE_CLOUD_PROJECT: emulator-test-project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
branches:
- main
pull_request:
name: django-tests4
name: django4.2-tests4
jobs:
system-tests:
runs-on: ubuntu-latest
Expand All @@ -22,7 +22,7 @@ jobs:
with:
python-version: 3.8
- name: Run Django tests
run: sh django_test_suite.sh
run: sh django_test_suite_4.2.sh
env:
SPANNER_EMULATOR_HOST: localhost:9010
GOOGLE_CLOUD_PROJECT: emulator-test-project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
branches:
- main
pull_request:
name: django-tests5
name: django4.2-tests5
jobs:
system-tests:
runs-on: ubuntu-latest
Expand All @@ -22,7 +22,7 @@ jobs:
with:
python-version: 3.8
- name: Run Django tests
run: sh django_test_suite.sh
run: sh django_test_suite_4.2.sh
env:
SPANNER_EMULATOR_HOST: localhost:9010
GOOGLE_CLOUD_PROJECT: emulator-test-project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
branches:
- main
pull_request:
name: django-tests6
name: django4.2-tests6
jobs:
system-tests:
runs-on: ubuntu-latest
Expand All @@ -22,7 +22,7 @@ jobs:
with:
python-version: 3.8
- name: Run Django tests
run: sh django_test_suite.sh
run: sh django_test_suite_4.2.sh
env:
SPANNER_EMULATOR_HOST: localhost:9010
GOOGLE_CLOUD_PROJECT: emulator-test-project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
branches:
- main
pull_request:
name: django-tests7
name: django4.2-tests7
jobs:
system-tests:
runs-on: ubuntu-latest
Expand All @@ -22,7 +22,7 @@ jobs:
with:
python-version: 3.8
- name: Run Django tests
run: sh django_test_suite.sh
run: sh django_test_suite_4.2.sh
env:
SPANNER_EMULATOR_HOST: localhost:9010
GOOGLE_CLOUD_PROJECT: emulator-test-project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
branches:
- main
pull_request:
name: django-tests8
name: django4.2-tests8
jobs:
system-tests:
runs-on: ubuntu-latest
Expand All @@ -22,7 +22,7 @@ jobs:
with:
python-version: 3.8
- name: Run Django tests
run: sh django_test_suite.sh
run: sh django_test_suite_4.2.sh
env:
SPANNER_EMULATOR_HOST: localhost:9010
GOOGLE_CLOUD_PROJECT: emulator-test-project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
branches:
- main
pull_request:
name: django-tests9
name: django4.2-tests9
jobs:
system-tests:
runs-on: ubuntu-latest
Expand All @@ -22,7 +22,7 @@ jobs:
with:
python-version: 3.8
- name: Run Django tests
run: sh django_test_suite.sh
run: sh django_test_suite_4.2.sh
env:
SPANNER_EMULATOR_HOST: localhost:9010
GOOGLE_CLOUD_PROJECT: emulator-test-project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
branches:
- main
pull_request:
name: Run Django Spanner integration tests against emulator 3.7
name: Run Django Spanner integration tests against emulator 3.10
jobs:
system-tests:
runs-on: ubuntu-latest
Expand All @@ -18,14 +18,14 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python 3.7
- name: Set up Python 3.20
uses: actions/setup-python@v5
with:
python-version: 3.7
python-version: "3.10"
- name: Install nox
run: python -m pip install nox
- name: Run nox
run: nox -s unit-3.7
run: nox -s unit-3.10
env:
SPANNER_EMULATOR_HOST: localhost:9010
GOOGLE_CLOUD_PROJECT: emulator-test-project
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/integration-tests-against-emulator-3.8.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ jobs:
python-version: 3.8
- name: Install nox
run: python -m pip install nox
with:
python-version: 3.8
- name: Run nox
run: nox -s unit-3.8
env:
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/integration-tests-against-emulator-3.9.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ jobs:
python-version: 3.9
- name: Install nox
run: python -m pip install nox
with:
python-version: 3.9
- name: Run nox
run: nox -s unit-3.9
env:
Expand Down
18 changes: 18 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,24 @@ configured:
}
}
Transaction support in autocommit mode
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Django version 4.2 and higher by default supports transactions in autocommit mode.
A transaction is automatically started if you define an
[atomic block](https://docs.djangoproject.com/en/4.2/topics/db/transactions/#controlling-transactions-explicitly).

Django version 3.2 and earlier did not support transactions in autocommit mode with Spanner.
You can enable transactions in autocommit mode with Spanner with the
`ALLOW_TRANSACTIONS_IN_AUTO_COMMIT` configuration option.

- To enable transactions in autocommit mode in V3.2, set
the flag "ALLOW_TRANSACTIONS_IN_AUTO_COMMIT" to True in your
settings.py file.
- To disable transactions in autocommit mode in V4.2, set
the flag "ALLOW_TRANSACTIONS_IN_AUTO_COMMIT" to False in your
settings.py file.


Set credentials and project environment variables
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
48 changes: 25 additions & 23 deletions django_spanner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
Field,
)

from .expressions import register_expressions
from .functions import register_functions
from .lookups import register_lookups
from .utils import check_django_compatability
Expand All @@ -34,22 +33,24 @@
if django.VERSION[:2] == (3, 2):
USING_DJANGO_3 = True

if USING_DJANGO_3:
from django.db.models.fields import (
SmallAutoField,
BigAutoField,
)
from django.db.models import JSONField
USING_DJANGO_4 = False
if django.VERSION[:2] == (4, 2):
USING_DJANGO_4 = True

from django.db.models.fields import (
SmallAutoField,
BigAutoField,
)
from django.db.models import JSONField

__version__ = pkg_resources.get_distribution("django-google-spanner").version

USE_EMULATOR = os.getenv("SPANNER_EMULATOR_HOST") is not None

# Only active LTS django versions (2.2.*, 3.2.*) are supported by this library right now.
SUPPORTED_DJANGO_VERSIONS = [(2, 2), (3, 2)]
# Only active LTS django versions (3.2.*, 4.2.*) are supported by this library right now.
SUPPORTED_DJANGO_VERSIONS = [(3, 2), (4, 2)]

check_django_compatability(SUPPORTED_DJANGO_VERSIONS)
register_expressions(USING_DJANGO_3)
register_functions()
register_lookups()

Expand All @@ -73,23 +74,24 @@ def autofield_init(self, *args, **kwargs):
AutoField.__init__ = autofield_init
AutoField.db_returning = False
AutoField.validators = []
if USING_DJANGO_3:
SmallAutoField.__init__ = autofield_init
BigAutoField.__init__ = autofield_init
SmallAutoField.db_returning = False
BigAutoField.db_returning = False
SmallAutoField.validators = []
BigAutoField.validators = []

def get_prep_value(self, value):
# Json encoding and decoding for spanner is done in python-spanner.
if not isinstance(value, JsonObject) and isinstance(value, dict):
return JsonObject(value)
SmallAutoField.__init__ = autofield_init
BigAutoField.__init__ = autofield_init
SmallAutoField.db_returning = False
BigAutoField.db_returning = False
SmallAutoField.validators = []
BigAutoField.validators = []


def get_prep_value(self, value):
# Json encoding and decoding for spanner is done in python-spanner.
if not isinstance(value, JsonObject) and isinstance(value, dict):
return JsonObject(value)

return value
return value

JSONField.get_prep_value = get_prep_value

JSONField.get_prep_value = get_prep_value

old_datetimewithnanoseconds_eq = getattr(
DatetimeWithNanoseconds, "__eq__", None
Expand Down
25 changes: 17 additions & 8 deletions django_spanner/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .introspection import DatabaseIntrospection
from .operations import DatabaseOperations
from .schema import DatabaseSchemaEditor
from django_spanner import USING_DJANGO_3


class DatabaseWrapper(BaseDatabaseWrapper):
Expand Down Expand Up @@ -123,6 +124,15 @@ def instance(self):
project=os.environ["GOOGLE_CLOUD_PROJECT"]
).instance(self.settings_dict["INSTANCE"])

@property
def allow_transactions_in_auto_commit(self):
if "ALLOW_TRANSACTIONS_IN_AUTO_COMMIT" in self.settings_dict:
return self.settings_dict["ALLOW_TRANSACTIONS_IN_AUTO_COMMIT"]
if USING_DJANGO_3:
return False
else:
return True

@property
def _nodb_connection(self):
raise NotImplementedError(
Expand Down Expand Up @@ -205,15 +215,14 @@ def is_usable(self):

return True

# The usual way to start a transaction is to turn autocommit off.
# Spanner DB API does not properly start a transaction when disabling
# autocommit. To avoid this buggy behavior and to actually enter a new
# transaction, an explicit SELECT 1 is required.
def _start_transaction_under_autocommit(self):
"""
Start a transaction explicitly in autocommit mode.
Staying in autocommit mode works around a bug that breaks
save points when autocommit is disabled by django.
"""
self.connection.cursor().execute("SELECT 1")
if self.allow_transactions_in_auto_commit:
self.connection.cursor().execute("BEGIN")
else:
# This won't start a transaction and was a bug in Spanner Django 3.2 version.
# Set ALLOW_TRANSACTIONS_IN_AUTO_COMMIT = True in your settings.py file to enable
# transactions in autocommit mode for Django 3.2.
self.connection.cursor().execute("SELECT 1")
Loading

0 comments on commit 75cd9bc

Please sign in to comment.