This repository was archived by the owner on Aug 19, 2026. It is now read-only.
fix: prevent Django tests from destroying production D1/DO data - #55
Open
Phat-pham99 wants to merge 1 commit into
Open
Phat-pham99 wants to merge 1 commit into
Phat-pham99 wants to merge 1 commit into
Conversation
Phat-pham99
force-pushed
the
fix/test-db-safety
branch
from
August 19, 2026 13:30
3a15847 to
dc1ddb2
Compare
Cloudflare D1 and Durable Objects backends inherited SQLite's test database creation behavior, which does not create isolated test databases for remote/serverless backends. This caused Django's test runner to execute migrations and flush data directly against production databases. - Override _create_test_db in CFDatabaseCreation to always raise ImproperlyConfigured when running tests against D1/DO, because no TEST setting can create an isolated Cloudflare test database safely - The D1 backend connects via CLOUDFLARE_DATABASE_ID, ignoring NAME, so allowing tests to proceed even with TEST['NAME'] configured would still destroy production data - Include helpful error message with example safe configuration Fixes G4brym#54
Phat-pham99
force-pushed
the
fix/test-db-safety
branch
from
August 19, 2026 13:38
dc1ddb2 to
8baf28b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes a critical data-loss bug where running Django tests against Cloudflare D1 or Durable Objects backends would destroy all production data.
Problem
The D1 and DO backends inherited
SQLiteDatabaseCreation, which computes a test database name but does not create an isolated database for D1/DO. The D1 backend connects viaCLOUDFLARE_DATABASE_ID(or binding), ignoring theNAMEsetting. So when Django's test runner calls_setup_test_db→migrate→flush, every query executes against the production D1 database. Since D1 has transactions disabled, eachflushandtruncateis permanently committed.Solution
Override
_create_test_dbinCFDatabaseCreationto always raiseImproperlyConfigured, with a clear error message explaining why and how to run tests safely. NoTESTsetting can currently create an isolated D1 test database through django-cf, so we block unconditionally.Safe Alternative: Separate Test Settings Module
Create a dedicated settings file for testing:
Then run tests with:
python manage.py test --settings=settings.testOr point
CLOUDFLARE_DATABASE_IDto a dedicated test D1 database in your test settings.Testing
tests/db/test_creation_safety.pywith 3 tests:_create_test_dbis calledTEST['NAME']is configured (because D1 ignores NAME)Impact
This is a breaking change for users who were unknowingly running tests against production D1. However, this is intentional — it prevents data destruction. Users will now need to use a separate settings module for testing.
Fixes #54