← Back to Pull requests

Load Python 2 pickled dates in 0003_drop_pickle migration #679

Merged
nk-batman21 merged 2 files (121 lines) on Oct 31, 2026 09:47 UTC
View on GitHub
nk-batman21 nk-batman21 commented on Oct 28, 2026 22:36 UTC
Author

Fixes #618

0003_drop_pickle fails when a database or Redis value contains a date or datetime saved under Python 2: its binary state cannot be decoded as ASCII.

Keep the default pickle decoding first. On a UnicodeDecodeError, accept a Latin-1 retry only for a plain date, datetime or time value. Ambiguous application strings still raise the original error, leaving the stored pickle intact. Timezone-aware values saved with pytz still require pytz during migration.

Regression tests cover both backends, Python 2 protocols 0 and 2, Python 3 values, and UTF-8 byte strings that must not be reinterpreted.

  • Ran: pytest (184 passed, 1 skipped) and django-admin test (177 tests, 1 skipped) on Python 3.12 / Django 6.1.

  • Ran: django-admin makemigrations --check --dry-run, ruff check, and ruff format --check for the changed files.
  • Diff: 121 lines (+119 / -2) across 2 files Reviewer: Mogost Review duration: 3551m
    All Vouch review confidence checks passed
    Residual risk 0.09 = Change Risk (0.56) × (1 − Review Confidence (0.84)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.56 · 121 lines diff across 2 files
    0.56
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.82 · 5 substantive comment(s)
    0.82
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    84%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.287
    Sensitive Paths sensitive_paths
    0.056
    Real PR Conversation Timeline
    8 items
    camilonova camilonova commented on code on Oct 29, 2026 19:53 UTC
    Code Review
    tests/test_migrations.py:1
    @@ -0,0 +1,68 @@
    +import pickle
    +from base64 import b64encode
    +from datetime import date
    +from datetime import datetime
    +from importlib import import_module
    +from unittest import mock
    +
    +from django.apps import apps
    +from django.db import connection
    +from django.test import TestCase
    +
    +from constance import settings
    +from constance.codecs import dumps
    +from constance.codecs import loads
    +from constance.models import Constance
    +from tests.redis_mockup import Connection
    +
    +drop_pickle = import_module("constance.migrations.0003_drop_pickle")
    +
    +# datetime(2017, 5, 15, 10, 30) and date(2017, 5, 15) as pickled by Python 2, with protocol 2
    +# (django-picklefield, used by the database backend) and protocol 0 (cPickle default, used by the Redis backend).
    +PY2_DATETIME = b"\x80\x02cdatetime\ndatetime\nq\x00U\n\x07\xe1\x05\x0f\n\x1e\x00\x00\x00\x00q\x01\x85q\x02Rq\x03."

    I don't know rick...

    camilonova camilonova commented on Oct 29, 2026 19:54 UTC
    Contributor

    Looks weird to me

    Copilot Copilot commented on code on Oct 30, 2026 17:26 UTC
    Code Review
    constance/migrations/0003_drop_pickle.py:1
    @@ -33,7 +33,8 @@ def migrate_pickled_data(apps, schema_editor) -> None:  # pragma: no cover
     
         for constance in Constance.objects.using(db_alias).exclude(value=None):
             if not is_already_migrated(constance.value):
    -            constance.value = dumps(pickle.loads(b64decode(constance.value.encode())))  # noqa: S301
    +            # encoding="latin1" is required to load datetime, date and time values pickled by Python 2.
    +            constance.value = dumps(pickle.loads(b64decode(constance.value.encode()), encoding="latin1"))  # noqa: S301

    encoding="latin1" also decodes ordinary Python 2 byte strings: a UTF-8 str containing café becomes café and is saved over the original pickle, whereas the old code stopped with a decode error. Handle datetime state separately from application strings, and require an explicit encoding policy or fail safely for ambiguous strings. The same issue affects the Redis conversion at line 60.

    copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on Oct 30, 2026 17:26 UTC
    Reviewer

    <!-- ccr-overview-v2 -->

    🟡 Changes recommended

    The migration can silently corrupt UTF-8 Python 2 byte strings by decoding them as Latin-1 and overwriting stored data; the Redis conversion has the same issue.

    <details open>
    <summary><strong>1 open issue</strong></summary>

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.png" alt="High severity" width="62" height="18" align="texttop"></picture> Latin-1 decoding corrupts UTF-8 strings in pickle and Redis data · New

  • </details>

    <details>
    <summary><strong>What changed in this PR</strong></summary>

    Addresses #618 by allowing the pickle-to-JSON migration to load date and datetime values saved under Python 2.

    Changes:

  • Uses Latin-1 decoding when unpickling database and Redis values.

  • Adds regression tests for legacy pickles, Python 3 pickles, and already-migrated database values.
  • | File | Description |
    | ---- | ----------- |
    | tests/​test_migrations.py | Adds database and Redis migration regression tests. |
    | constance/​migrations/​0003_drop_pickle.py | Enables Latin-1 decoding in both migration paths. |
    </details>

    <sub>Review effort: Balanced</sub>

    nk-batman21 nk-batman21 commented on code on Oct 31, 2026 02:04 UTC
    Code Review
    tests/test_migrations.py:1
    @@ -0,0 +1,68 @@
    +import pickle
    +from base64 import b64encode
    +from datetime import date
    +from datetime import datetime
    +from importlib import import_module
    +from unittest import mock
    +
    +from django.apps import apps
    +from django.db import connection
    +from django.test import TestCase
    +
    +from constance import settings
    +from constance.codecs import dumps
    +from constance.codecs import loads
    +from constance.models import Constance
    +from tests.redis_mockup import Connection
    +
    +drop_pickle = import_module("constance.migrations.0003_drop_pickle")
    +
    +# datetime(2017, 5, 15, 10, 30) and date(2017, 5, 15) as pickled by Python 2, with protocol 2
    +# (django-picklefield, used by the database backend) and protocol 0 (cPickle default, used by the Redis backend).
    +PY2_DATETIME = b"\x80\x02cdatetime\ndatetime\nq\x00U\n\x07\xe1\x05\x0f\n\x1e\x00\x00\x00\x00q\x01\x85q\x02Rq\x03."

    Could you clarify which part concerns you: the Python 2 fixtures, the migration behavior, or both?

    Mogost Mogost approved these changes on Oct 31, 2026 09:39 UTC
    Approved Reviewer

    Approved these changes.

    codecov-commenter codecov-commenter commented on Oct 31, 2026 09:43 UTC
    Contributor

    Codecov Report


    :white_check_mark: All modified and coverable lines are covered by tests.

    | Files with missing lines | Coverage Δ | |
    |---|---|---|
    | constance/migrations/0003\_drop\_pickle.py | 94.11% <100.00%> (ø) | |
    <details><summary> :rocket: New features to boost your workflow: </summary>

  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

  • </details>

    Reviewers
    Assignees
    nk-batman21 nk-batman21
    Labels
    risk: low confidence: 84.1% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 56.4% (MEDIUM) · Higher = larger blast radius
    56.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 84.1% · Lower = rushed or shallow review
    84.1%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 9.0% · Safe to merge
    9.0%