Skip to content

feat: migrate Django 5.2 upgrade and 3.2/4.2 deprecations#16624

Open
sinhasubham wants to merge 3 commits intomainfrom
django_upgrade_5_2
Open

feat: migrate Django 5.2 upgrade and 3.2/4.2 deprecations#16624
sinhasubham wants to merge 3 commits intomainfrom
django_upgrade_5_2

Conversation

@sinhasubham
Copy link
Copy Markdown
Contributor

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes #<issue_number_goes_here> 🦕

@sinhasubham sinhasubham changed the title Django upgrade 5 2 feat: migrate Django 5.2 upgrade and 3.2/4.2 deprecations Apr 13, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request updates the Spanner database backend to support Django 5.2 and Python 3.10+, while removing legacy support for Django 3.2 and 4.2. Key changes include the implementation of a global client cache to prevent initialization crashes, support for JSONArray, GeneratedField, and db_default, and a retry mechanism for flushing tables with foreign key constraints. Feedback highlights several critical issues: the use of a 32-bit mask for primary key generation significantly reduces the key space, and making GOOGLE_CLOUD_PROJECT mandatory is a breaking change for local development. Additionally, the package version was incorrectly decremented, and a minor version bump was recommended for this release. Finally, using WHERE 1=1 was suggested as a more idiomatic alternative to WHERE true for flush operations.

Comment thread packages/django-google-spanner/django_spanner/__init__.py Outdated
Comment thread packages/django-google-spanner/django_spanner/base.py Outdated
Comment thread packages/django-google-spanner/django_spanner/base.py Outdated
Comment thread packages/django-google-spanner/django_spanner/version.py Outdated
Comment thread packages/django-google-spanner/django_spanner/operations.py Outdated
@sinhasubham sinhasubham force-pushed the django_upgrade_5_2 branch 4 times, most recently from 2b71c2a to f6098f1 Compare April 16, 2026 09:17
@sinhasubham sinhasubham marked this pull request as ready for review April 20, 2026 05:57
@sinhasubham sinhasubham requested review from a team as code owners April 20, 2026 05:57

# Global cache for Spanner client to prevent multiple initializations
# which can cause OpenTelemetry 'MeterProvider override' crashes.
_SPANNER_CLIENT_CACHE = None
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I dont understand why do we need this, ideally our Spanner client library should handle the override crash ?

Comment on lines +161 to +164
"project": os.environ.get("GOOGLE_CLOUD_PROJECT")
or self.settings_dict.get("project")
or self.settings_dict.get("PROJECT")
or "test-project",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is being done in multiple places, better to extract in "init" method once and use it everywhere.

Also why do we support self.settings_dict.get("project") and self.settings_dict.get("PROJECT") now ?

Comment on lines +185 to +187
conn_params.pop("instance", None)
conn_params.pop("instance_id", None)
conn_params.pop("client", None)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why this change is required ?
We are not using instance_id or client anywhere. We are only using instance.instance_id or instance._client

Comment on lines +51 to +52
# This method copies the complete code of this overridden method from
# Django core and modify it for Spanner by adding one line
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is duplicate comment

Comment on lines +37 to +39
except AttributeError:
# The test method might not exist in this version of Django.
pass
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do we need this, we have already added an explicit list of skip tests

# https://developers.google.com/open-source/licenses/bsd

__version__ = "4.0.3"
__version__ = "4.1.0"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why are we modifying this version manually ? shouldn't this be updated automatically once we do the release ?

self.assertEqual(
sql_compiled,
"SELECT tests_report.name FROM tests_report ORDER BY "
"SELECT tests_report.name AS name FROM tests_report ORDER BY "
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For my knowledge why this change is required ?


UNIT_TEST_PYTHON_VERSIONS = [
"3.8",
"3.9",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I thought we received 3.9 support as well, as we removed 3.9 integration test suite file

Comment on lines +126 to +127
if session.python == "3.9":
session.skip("Python 3.9 is not supported for Django 5.2 tests")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Lets remove 3.9 from the list then ?

# https://developers.google.com/open-source/licenses/bsd

__version__ = "4.0.3"
__version__ = "4.1.0"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also this should go as a Major version release, since we are deprecating older vesion of Django.

PR title should be feat! to trigger a major version

from django.db import DEFAULT_DB_ALIAS
from django.db.models.fields import (

from django.db import DEFAULT_DB_ALIAS # noqa: E402
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why are you suppressing linting errors?


def gen_rand_int64():
# Credit to https://stackoverflow.com/a/3530326.
# Use 32-bit integer for Emulator compatibility (High-bit issues observed).
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this a valid comment?

method_name,
skip("unsupported by Spanner")(method),
)
try:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Make you sure you clean up the method. Not suppress the error.

"constraint": self.sql_check_constraint % {"check": check},
}

def _unique_sql(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why are we removing it?

@@ -28,7 +28,6 @@
DEFAULT_PYTHON_VERSION = "3.14"

UNIT_TEST_PYTHON_VERSIONS = [
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Remove support for 3.8 and 3.9

else:
return []

def execute_sql_flush(self, sql_list):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this looks so bruteforce. is there any other better approach?

# Spanner does not support expression indexes
# example: CREATE INDEX index_name ON table (LOWER(column_name))
supports_expression_indexes = False
supports_stored_generated_columns = True

# Django tests that aren't supported by Spanner.
skip_tests = (
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We need to group them by cause. Otherwise we will not know if we are doing it correctly or not.

try:
cursor.execute(sql)
progress = True
except Exception as e:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

catch more specific exception ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants