Skip to content

fix(migrations): make migrate:up run clean on a fresh database#42

Closed
MarvyNwaokobia wants to merge 1 commit into
LabsCrypt:mainfrom
MarvyNwaokobia:fix/migrations-clean-run
Closed

fix(migrations): make migrate:up run clean on a fresh database#42
MarvyNwaokobia wants to merge 1 commit into
LabsCrypt:mainfrom
MarvyNwaokobia:fix/migrations-clean-run

Conversation

@MarvyNwaokobia

Copy link
Copy Markdown

Problem

The CI migrate step has never completed on a clean database. Because node-pg-migrate loads all migration files before running any, CI only ever surfaced the first failure — a CommonJS migration file under our "type": "module" package (exports is not defined) — and that masked a whole chain of further runtime errors behind it. PRs that touch nothing migration-related (e.g. #37, #38) are blocked on this.

This was verified by running npm run migrate:up against a throwaway local Postgres and fixing each failure in turn until it completed.

Fixes (in migration execution order)

Migration Problem Fix
1776 / 1778 / 1784 CommonJS (exports./module.exports) under ESM → load fails Convert to export const up/down. 1784 also used db.query() (never provided to migrations) → routed through pgm.sql()
1772 webhook-subscriptions event_types jsonb default: "[]::jsonb" → invalid JSON pgm.func("'[]'::jsonb")
1778 transaction-submissions trigger calls update_updated_at_column(), created by no migration define the function before the trigger
1781 webhook-retry-logic re-adds payload / next_retry_at already created by 1772 guard with ifNotExists
1784 add-loan-disputes FK → loan_events(loan_id), which is non-unique (and later a view) drop the FK; keep loan_id as a plain column
1787 user-notification-preferences re-adds email_enabled/sms_enabled/phone already added by 1773 guard with ifNotExists
1788 unified-contract-events pgm.renameIndex — not a method in node-pg-migrate v8 raw ALTER INDEX ... RENAME TO (matches the pattern already used in the same file)
1789 ensure-core-tables loan_events existence check used pg_tables (excludes views) → tried CREATE TABLE over the 1788 backward-compat view use to_regclass('public.loan_events')

No SQL/schema behaviour is changed beyond making each statement succeed; the CommonJS→ESM conversions are syntax-only.

Verification

  • npm run migrate:up applies all 27 migrations on a fresh DB (exit 0), recorded in pgmigrations.
  • Idempotent: a second run reports No migrations to run!.
  • prettier --check . clean; eslint . has 0 errors (pre-existing warnings unchanged).

Note

This makes the migrate step reachable end-to-end for the first time, which also unblocks the Test step that runs after it — that step is now exercised on a real schema and may surface its own (separate) issues.

🤖 Generated with Claude Code

node-pg-migrate loads every migration file before running any, so CI only
ever surfaced the first failure (a CommonJS file under an ESM package) and
masked a chain of further runtime errors behind it. migrate:up has never
completed on a clean DB. Fixes, in execution order:

- 1776/1778/1784: convert CommonJS (exports./module.exports) to ESM so the
  files load under "type": "module". 1784 also called db.query(), which
  node-pg-migrate never provides to migrations — routed through pgm.sql().
- 1772: event_types jsonb default "[]::jsonb" rendered as invalid JSON;
  use pgm.func("'[]'::jsonb").
- 1778: trigger referenced update_updated_at_column(), created by no
  migration — define it before the trigger.
- 1781: payload / next_retry_at are already created by 1772 — guard the
  re-adds with ifNotExists.
- 1784: drop FK to loan_events(loan_id); loan_events is an append-only event
  table (and later a view) with no unique loan_id to reference.
- 1787: email_enabled/sms_enabled/phone already added by 1773 — guard with
  ifNotExists.
- 1788: replace pgm.renameIndex (not a method in node-pg-migrate v8) with
  raw ALTER INDEX ... RENAME TO.
- 1789: loan_events existence check used pg_tables (excludes views) and tried
  to CREATE TABLE over the 1788 backward-compat view; use to_regclass.

Verified locally against a fresh Postgres: migrate:up applies all 27
migrations (exit 0) and is a no-op on re-run. Unblocks the migrate step for
all branches.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@MarvyNwaokobia

Copy link
Copy Markdown
Author

Superseded — the migration fixes are folded into #38 so that PR is self-contained and goes green on its own. Closing this in favor of #38.

@MarvyNwaokobia MarvyNwaokobia deleted the fix/migrations-clean-run branch June 23, 2026 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant