feat(zygote): Django test database initialization before fork#88
feat(zygote): Django test database initialization before fork#88NikkeTryHard merged 3 commits intomasterfrom
Conversation
Add _setup_django_test_db() and _teardown_django_test_db() to tach_harness.py. Creates test DB via django.test.utils.setup_databases() in the Zygote before forking workers. Supports --reuse-db via TACH_REUSE_DB env var (keepdb=True). Registers atexit handler for teardown. Refs #84
Set TACH_REUSE_DB and TACH_CREATE_DB env vars before fork so the Zygote can read them during test DB setup. Add reuse_db/create_db fields to Scheduler struct and pass through to TestPayload instead of hardcoded false values. Refs #84
Remove ensure_connection() that connected to the production database. Add _setup_django_test_db() call after init_session() to create a fully migrated test database before forking workers. Connections are closed before fork to prevent FD sharing corruption. Closes #84
|
Caution Review failedThe pull request is closed. 📝 WalkthroughWalkthroughThe PR adds database handling configuration flags (reuse_db, create_db) to the Scheduler and TestPayload, propagates them from CLI through worker construction, and implements Django test database initialization in the Zygote before snapshot creation to ensure workers inherit fully migrated database schemas. Changes
Sequence DiagramsequenceDiagram
actor CLI
participant Main as main.rs
participant Scheduler
participant Zygote as zygote.rs
participant Python as tach_harness.py
participant Django
participant Worker as Worker Process
CLI->>Main: --reuse-db / --create-db flags
Main->>Main: Parse flags to env vars<br/>(TACH_REUSE_DB, TACH_CREATE_DB)
Main->>Scheduler: with_config(reuse_db, create_db)
Scheduler->>Scheduler: Store config flags
Scheduler->>Zygote: Initialize with flags
rect rgba(100, 150, 200, 0.5)
Note over Zygote,Python: Before Snapshot
Zygote->>Python: Call init_session()
Python->>Django: django.setup()
Python->>Python: _setup_django_test_db()
Python->>Django: setup_databases(keepdb)
Django->>Django: Create test schema & run migrations
end
Zygote->>Zygote: Take Zygote Snapshot<br/>(with migrated DB)
Zygote->>Worker: Fork worker process
Worker->>Worker: Inherit DB connection + schema
Worker->>Django: Execute tests on ready database
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Poem
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
_setup_django_test_db()and_teardown_django_test_db()to Python harness--reuse-db/--create-dbCLI flags through env vars and Scheduler to TestPayloaddjango.test.utils.setup_databases()atexithandler for teardown; connections closed before forkCloses #84
What Changed
src/tach_harness.py_setup_django_test_db()+_teardown_django_test_db()+ global_DJANGO_OLD_CONFIGsrc/main.rsTACH_REUSE_DB/TACH_CREATE_DBenv vars before forksrc/execution/scheduler.rsreuse_db/create_dbfields, wire through to TestPayloadsrc/execution/zygote.rsensure_connection()warmup, add_setup_django_test_db()call afterinit_session()Design Decisions
setup_databases()is Python — harness owns the lifecycleTACH_NO_ISOLATIONpattern; Zygote inherits parent env before forkatexitfires when process exits_apply_django_db_isolation()Verification
cargo build✅cargo fmt✅cargo clippy -D warnings✅854/854 unit tests pass✅Summary by CodeRabbit
New Features
Bug Fixes