fix(migrations): make migrate:up run clean on a fresh database#42
Closed
MarvyNwaokobia wants to merge 1 commit into
Closed
fix(migrations): make migrate:up run clean on a fresh database#42MarvyNwaokobia wants to merge 1 commit into
MarvyNwaokobia wants to merge 1 commit into
Conversation
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>
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The CI migrate step has never completed on a clean database. Because
node-pg-migrateloads 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:upagainst a throwaway local Postgres and fixing each failure in turn until it completed.Fixes (in migration execution order)
exports./module.exports) under ESM → load failsexport const up/down. 1784 also useddb.query()(never provided to migrations) → routed throughpgm.sql()event_typesjsonbdefault: "[]::jsonb"→ invalid JSONpgm.func("'[]'::jsonb")update_updated_at_column(), created by no migrationpayload/next_retry_atalready created by 1772ifNotExistsloan_events(loan_id), which is non-unique (and later a view)loan_idas a plain columnemail_enabled/sms_enabled/phonealready added by 1773ifNotExistspgm.renameIndex— not a method in node-pg-migrate v8ALTER INDEX ... RENAME TO(matches the pattern already used in the same file)loan_eventsexistence check usedpg_tables(excludes views) → triedCREATE TABLEover the 1788 backward-compat viewto_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:upapplies all 27 migrations on a fresh DB (exit 0), recorded inpgmigrations.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