Skip to content

bug: Migration silently fails — fan_out_id column missing after deploy, breaks chat #236

Closed

Description

@vybe

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 of logging, 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 swallowed

Reproduction Steps

  1. Deploy any commit that adds a new migration (e.g., 3a2a954 — fan-out)
  2. Start the backend with multiple uvicorn workers (default prod config)
  3. OR experience any transient SQLite lock during the migration window at startup
  4. Backend starts and reports healthy
  5. 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 attempted

2. 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-backend

Environment

  • Trinity version: 272995b (includes fan-out from 3a2a954)
  • Introduced in: 3a2a954 feat: Fan-out primitive for parallel self-invocation

Related

Activity

  1. added
    priority-p1
    type-bug
    status-in-progress
    on Apr 1, 2026
  2. added a commit that references this issue on Apr 1, 2026
    c7950d6
  3. removed
    status-in-progress
    on Apr 17, 2026
  4. added
    complexity-low
    on Jun 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    complexity-lowpriority-p1type-bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions