Skip to content

Commit

Permalink
Comments incorporated
Browse files Browse the repository at this point in the history
  • Loading branch information
ankiaga committed Apr 16, 2024
1 parent 396882e commit 21c53ab
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 21 deletions.
13 changes: 13 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ configured:
}
}
Transaction support in autocommit mode
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

We have started supporting transactions in autocommit mode by default for V4.2
while they are not supported in prior versions (i.e. V3.2).

- To override supporting transactions in autocommit mode in V3.2 please enable
the flag "ALLOW_TRANSACTIONS_IN_AUTO_COMMIT" with value True in your
settings.py file.
- To override disabling transactions in autocommit mode in V4.2 please disable
the flag "ALLOW_TRANSACTIONS_IN_AUTO_COMMIT" with value False in your
settings.py file.


Set credentials and project environment variables
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
4 changes: 2 additions & 2 deletions django_spanner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@

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), (4, 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_functions()
Expand Down
24 changes: 12 additions & 12 deletions django_spanner/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.db import NotSupportedError
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django_spanner._opentelemetry_tracing import trace_call
from django_spanner import USE_EMULATOR, USING_DJANGO_4
from django_spanner import USE_EMULATOR, USING_DJANGO_3


class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
Expand Down Expand Up @@ -117,24 +117,24 @@ def create_model(self, model):
# Create a unique constraint separately because Spanner doesn't
# allow them inline on a column.
if field.unique and not field.primary_key:
if USING_DJANGO_4:
if USING_DJANGO_3:
self.deferred_sql.append(
self._create_unique_sql(model, [field])
self._create_unique_sql(model, [field.column])
)
else:
self.deferred_sql.append(
self._create_unique_sql(model, [field.column])
self._create_unique_sql(model, [field])
)

# Add any unique_togethers (always deferred, as some fields might be
# created afterwards, like geometry fields with some backends)
for fields in model._meta.unique_together:
if USING_DJANGO_4:
columns = [model._meta.get_field(field) for field in fields]
else:
if USING_DJANGO_3:
columns = [
model._meta.get_field(field).column for field in fields
]
else:
columns = [model._meta.get_field(field) for field in fields]
self.deferred_sql.append(self._create_unique_sql(model, columns))
constraints = [
constraint.constraint_sql(model, self)
Expand Down Expand Up @@ -290,13 +290,13 @@ def add_field(self, model, field):
# Create a unique constraint separately because Spanner doesn't allow
# them inline on a column.
if field.unique and not field.primary_key:
if USING_DJANGO_4:
if USING_DJANGO_3:
self.deferred_sql.append(
self._create_unique_sql(model, [field])
self._create_unique_sql(model, [field.column])
)
else:
self.deferred_sql.append(
self._create_unique_sql(model, [field.column])
self._create_unique_sql(model, [field])
)
# Add any FK constraints later
if (
Expand Down Expand Up @@ -556,15 +556,14 @@ def _unique_sql(
expressions=None,
):
# Inline constraints aren't supported, so create the index separately.
if USING_DJANGO_4:
if USING_DJANGO_3:
sql = self._create_unique_sql(
model,
fields,
name=name,
condition=condition,
include=include,
opclasses=opclasses,
expressions=expressions,
)
else:
sql = self._create_unique_sql(
Expand All @@ -574,6 +573,7 @@ def _unique_sql(
condition=condition,
include=include,
opclasses=opclasses,
expressions=expressions,
)
if sql:
self.deferred_sql.append(sql)
Expand Down
14 changes: 7 additions & 7 deletions tests/unit/django_spanner/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.db.utils import DatabaseError
from django_spanner.compiler import SQLCompiler
from django.db.models.query import QuerySet
from django_spanner import USING_DJANGO_3, USING_DJANGO_4
from django_spanner import USING_DJANGO_3
from tests.unit.django_spanner.simple_test import SpannerSimpleTestClass
from .models import Number

Expand Down Expand Up @@ -48,7 +48,7 @@ def test_get_combinator_sql_all_union_sql_generated(self):
+ "FROM tests_number WHERE tests_number.num >= %s"
],
)
elif USING_DJANGO_4:
else:
self.assertEqual(
sql_compiled,
[
Expand Down Expand Up @@ -80,7 +80,7 @@ def test_get_combinator_sql_distinct_union_sql_generated(self):
+ "tests_number.num >= %s"
],
)
elif USING_DJANGO_4:
else:
self.assertEqual(
sql_compiled,
[
Expand Down Expand Up @@ -112,7 +112,7 @@ def test_get_combinator_sql_difference_all_sql_generated(self):
+ "FROM tests_number WHERE tests_number.num >= %s"
],
)
elif USING_DJANGO_4:
else:
self.assertEqual(
sql_compiled,
[
Expand Down Expand Up @@ -144,7 +144,7 @@ def test_get_combinator_sql_difference_distinct_sql_generated(self):
+ "tests_number.num >= %s"
],
)
elif USING_DJANGO_4:
else:
self.assertEqual(
sql_compiled,
[
Expand Down Expand Up @@ -179,7 +179,7 @@ def test_get_combinator_sql_union_and_difference_query_together(self):
+ "WHERE tests_number.num = %s)"
],
)
elif USING_DJANGO_4:
else:
self.assertEqual(
sql_compiled,
[
Expand Down Expand Up @@ -219,7 +219,7 @@ def test_get_combinator_sql_parentheses_in_compound_not_supported(self):
+ "WHERE tests_number.num = %s)"
],
)
elif USING_DJANGO_4:
else:
self.assertEqual(
sql_compiled,
[
Expand Down

0 comments on commit 21c53ab

Please sign in to comment.