Load Python 2 pickled dates in 0003_drop_pickle migration #679
I don't know rick...
Looks weird to me
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.
<!-- 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>
</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:
| 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>
Could you clarify which part concerns you: the Python 2 fixtures, the migration behavior, or both?
Approved these changes.
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>
</details>
Mogost
nk-batman21
Fixes #618
0003_drop_picklefails 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.
pytest(184 passed, 1 skipped) anddjango-admin test(177 tests, 1 skipped) on Python 3.12 / Django 6.1.django-admin makemigrations --check --dry-run,ruff check, andruff format --checkfor the changed files.