Skip to content
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

feat!: Support Django 4.2 #865

Merged
merged 15 commits into from
Apr 25, 2024
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
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):
Copy link

@jaz-la jaz-la Apr 15, 2024

Choose a reason for hiding this comment

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

as commented in this draft:
#860 (comment)

Currently Django 5 is released, since you are already working on it, can this field be override with a system environment, and make it something like:
if django.VERSION[:2] == (4, 2) or os.getenv('USING_DJANGO_4', 'false') == 'true'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry we cant do that as we are not sure how will Django 5 behave with this code.
Please feel free to create a fork and make changes

Copy link

Choose a reason for hiding this comment

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

Can I second @jaz-la's request? If we can appropriately set expectations with the env car, what's the harm? I am currently using a fork that is hard to maintain with Django 5, and this PR includes everything that I have needed for Django 5 so far. Please?

Thanks again for all of your work.

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)
ankiaga marked this conversation as resolved.
Show resolved Hide resolved
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.
ankiaga marked this conversation as resolved.
Show resolved Hide resolved
# 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