Skip to content

Conversation

@NikkeTryHard
Copy link
Owner

@NikkeTryHard NikkeTryHard commented Feb 10, 2026

Summary

  • Add _setup_django_test_db() and _teardown_django_test_db() to Python harness
  • Wire --reuse-db / --create-db CLI flags through env vars and Scheduler to TestPayload
  • Replace production DB warmup in Zygote with test DB setup via django.test.utils.setup_databases()
  • Register atexit handler for teardown; connections closed before fork

Closes #84

What Changed

File Change
src/tach_harness.py New _setup_django_test_db() + _teardown_django_test_db() + global _DJANGO_OLD_CONFIG
src/main.rs Set TACH_REUSE_DB / TACH_CREATE_DB env vars before fork
src/execution/scheduler.rs Add reuse_db/create_db fields, wire through to TestPayload
src/execution/zygote.rs Remove ensure_connection() warmup, add _setup_django_test_db() call after init_session()

Design Decisions

  • Test DB setup in Python: Django's setup_databases() is Python — harness owns the lifecycle
  • Env vars for config: Matches existing TACH_NO_ISOLATION pattern; Zygote inherits parent env before fork
  • atexit for teardown: Zygote has no Python-level shutdown hook; atexit fires when process exits
  • Connections closed before fork: Prevents FD sharing corruption; workers reopen in _apply_django_db_isolation()

Verification

  • cargo build
  • cargo fmt
  • cargo clippy -D warnings
  • 854/854 unit tests pass

Summary by CodeRabbit

  • New Features

    • Added CLI flags to control Django test database reuse and creation behavior during test runs.
  • Bug Fixes

    • Improved Django test database lifecycle management with optimized setup timing.
    • Enhanced error logging for database isolation operations.

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
@NikkeTryHard NikkeTryHard merged commit 40c1079 into master Feb 10, 2026
1 check passed
@NikkeTryHard NikkeTryHard deleted the fix/issue-84-django-test-db branch February 10, 2026 03:06
@coderabbitai
Copy link

coderabbitai bot commented Feb 10, 2026

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

The 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

Cohort / File(s) Summary
Scheduler Configuration
src/execution/scheduler.rs, src/main.rs
Added reuse_db and create_db boolean fields to Scheduler struct and updated with_config signature to accept these flags. CLI arguments are passed through to Scheduler initialization to enable per-session database configuration.
Zygote Django DB Setup
src/execution/zygote.rs
Replaced Django connection warmup logic with explicit test database setup that runs after pytest configuration but before worker forking. Calls _setup_django_test_db() to initialize test database schema so workers inherit properly migrated databases.
Python Harness Django Lifecycle
src/tach_harness.py
Implemented _setup_django_test_db and _teardown_django_test_db functions to manage Django test database creation/destruction with keepdb semantics. Added Django DB isolation warning improvements, enhanced error logging, and formatting consistency updates across string literals.

Sequence Diagram

sequenceDiagram
    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
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

rust, tests, python, core, execution

Poem

🐰 The Zygote hops and sets up shop,
Django schemas bloom before the fork,
Workers inherit gardens prepared with care,
No more tables missing from the air!
Tests now dance on solid ground,
With databases ready—victory found! 🌱

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/issue-84-django-test-db

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(execution): initialize Django test databases in Zygote before snapshot

1 participant