stock-bot/docs/development/backlog/tasks/O01.md

3.2 KiB

O01 — Serialize additive schema migrations safely

Parent epic: E9. Priority/severity: P1 / High. Relative complexity: S. Recommended specialization: PostgreSQL/backend infrastructure.

Scheduling source of truth: manifest entry O01. No task is authorized until the plan is approved. Dependencies and decision references below describe the design; the manifest controls current readiness.

Evidence and root cause

Intended behavior and implementation scope

Preserve the explicit reviewed additive allowlist. On one PostgreSQL transaction client, acquire a transaction-scoped advisory lock BEFORE migration table initialization, migration discovery/check, SQL apply and recording. Do not lock via an arbitrary pooled query or add a migration framework. Integrator allocates additive filenames; applied scripts remain immutable.

Exclusions

No speculative distributed infrastructure or weakened correctness gates.

Likely files and ownership

  • apps/stock/web-api/src/migrations/migration-runner.ts
  • database/postgres/init/
  • test/postgres/ (migration tests)

Write locks: sql-migrations. One agent owns this change. Lock names do not override actual overlapping files.

Dependencies and blockers

No code prerequisite. Global execution approval and write-lock acquisition still required.

No additional economic-policy approval is needed for this narrowly scoped task.

Parallel eligibility: only with disjoint actual files and reserved write locks; read FIRST-WAVE.md for the initial exact ownership boundaries.

Acceptance criteria

  • Two API startups apply each new migration once without duplicate object errors.
  • Upgrade from current schema preserves runs, datasets, execution ledger and checkpoints.
  • Destructive init scripts cannot enter startup migration discovery.

Mandatory tests

  • two_concurrent_startups_same_client_lock, precheck_inside_lock, migration_failure_rollback, destructive_init_excluded.
  • bun run test:postgres on disposable fixtures only; existing migrations/data retained.

Apply relevant quality gates. Report exact commands/results and unavailable checks; named new cases are requirements, not claims of existing passing tests.

Regression risks and rollback

Performance measurements can hide semantic changes; require economic hash equivalence. Keep migrations additive and previous results readable. Do not add a switch that restores a correctness defect. Versioned cutover/migration is required only when persisted contracts or active engines change; never silently fall back to old economics.

Handoff

Report scope changes (if any), fixture expectations, API/schema changes, measurements, remaining limitations and commit/PR references once authorized. Request independent review; only the integration/planning owner may mark the manifest done.