Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/MIGRATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -550,3 +550,25 @@ environment, resolve the failure per §6 (rolling back) instead.
**Prevention:** run the rehearsal (§5) before pushing, and write idempotent SQL
(`IF [NOT] EXISTS`, `DO` blocks instead of `ADD CONSTRAINT IF NOT EXISTS` for
PostgreSQL < 12).

### A.4 Lexicographical migration sorting order (indexes before tables)

**Incident:** `20261003_001_add_webhook_query_indexes`

**Symptom:** `npx prisma migrate deploy` fails with `ERROR: relation "user_webhook_deliveries" does not exist` (error code P3018, database code 42P01).

**Cause:** Prisma orders migrations alphabetically (lexicographically) by directory name. Because ASCII `'0'` (48) sorts before ASCII `'a'` (97), `20261003_001_add_webhook_query_indexes` was sorted and executed *before* `20261003_add_missing_models` (which creates `user_webhook_deliveries`).

**Recipe**

```bash
# 1. Rename the migration folder so its timestamp sorts after the table creation:
# 20261003_001_add_webhook_query_indexes -> 20261004000000_add_webhook_query_indexes

# 2. If the migration failed on an existing environment, resolve the failed row:
npx prisma migrate resolve --rolled-back 20261003_001_add_webhook_query_indexes
npx prisma migrate deploy
```

**Prevention:** always use standard 14-digit timestamps (`YYYYMMDDHHMMSS_name`) or verify sorting order with `fs.readdirSync('prisma/migrations').sort()` when creating dependent migrations.

1 change: 1 addition & 0 deletions fix-and-deploy-migrations.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ FIXED_MIGRATIONS=(
"20260928180000_add_webhook_query_indexes"
"20260928200000_add_totp_credentials"
"20260929120000_add_collateral_loans"
"20261003_001_add_webhook_query_indexes"
)

for migration in "${FIXED_MIGRATIONS[@]}"; do
Expand Down
Loading