Repository navigation
bug: Migration silently fails — fan_out_id column missing after deploy, breaks chat #236
Description
Summary
The migration system in db/migrations.py silently swallows errors via a bare try/except. After deploying the fan-out feature (#230), the fan_out_id column was not added to schedule_executions on a production instance despite the migration _migrate_execution_fan_out_id being correctly registered. The backend reported /health as healthy but all chat messages and any API endpoint touching schedule_executions with that column failed with OperationalError.
Component
Backend / Database Migrations
Priority
P1
Error
sqlite3.OperationalError: table schedule_executions has no column named fan_out_id
Affects any endpoint that writes or reads fan_out_id from schedule_executions. In practice this crashed chat message delivery.
Location
- File:
src/backend/db/migrations.py - Function:
run_all_migrations(lines 41–92) - Migration:
_migrate_execution_fan_out_id(line 896)
Root Cause
run_all_migrations wraps each migration in try/except Exception as e: print(...). If any migration fails (database locked, race condition with multi-worker uvicorn startup, or any other error), the failure is printed to stdout but execution continues. The backend starts, /health returns {"status": "healthy"}, but the schema is inconsistent.
Additionally:
print()is used instead oflogging, so migration errors may not be visible in structured log pipelines- There is no migration tracking table — no way to verify which migrations completed successfully vs. which were silently skipped
- The backend's health check does not validate schema consistency
# run_all_migrations — current (problematic)
for name, migration_fn in migrations:
try:
migration_fn(cursor, conn)
except Exception as e:
print(f"Migration check ({name}): {e}") # ← silently swallowedReproduction Steps
- Deploy any commit that adds a new migration (e.g.,
3a2a954— fan-out) - Start the backend with multiple uvicorn workers (default prod config)
- OR experience any transient SQLite lock during the migration window at startup
- Backend starts and reports healthy
- Any request that reads/writes the new column raises
OperationalError
Suggested Fix
Two complementary fixes:
1. Use logging + re-raise (or at minimum surface failures clearly):
import logging
logger = logging.getLogger(__name__)
for name, migration_fn in migrations:
try:
migration_fn(cursor, conn)
except Exception as e:
logger.error(f"Migration FAILED ({name}): {e}", exc_info=True)
raise # OR: collect failures and raise after all migrations attempted2. Add a migration tracking table to guarantee idempotency and observability:
CREATE TABLE IF NOT EXISTS schema_migrations (
name TEXT PRIMARY KEY,
applied_at TEXT NOT NULL
);Skip already-applied migrations, record successful ones. This also prevents the multi-worker race condition since only the first worker to insert the row will run the migration.
3. Schema consistency check in health endpoint:
After migrations run, verify expected columns exist. If any are missing, report unhealthy so the problem is immediately visible rather than discovered at request time.
Workaround
Manual column addition applied on affected instance:
sqlite3 ~/trinity/trinity-data/trinity.db \
"ALTER TABLE schedule_executions ADD COLUMN fan_out_id TEXT;"
docker restart trinity-backendEnvironment
- Trinity version:
272995b(includes fan-out from3a2a954) - Introduced in:
3a2a954feat: Fan-out primitive for parallel self-invocation
Related
src/backend/db/migrations.py— migration runnersrc/backend/database.py—init_database()callsrun_all_migrations()- Fan-out feature:
3a2a954/ feat: Fan-out primitive for parallel self-invocation #230
Activity
fix(migrations): raise on failure, track applied migrations, schema h…
fix(migrations): raise on failure, track applied migrations, schema h…