fix(install): silence clean-install postgres log noise (#484)

Two latent install-time issues that emitted scary postgres ERROR lines
on every fresh start but didn't actually break anything. MrGabri flagged
them after #494 had already cleared the FK-ordering crash.

1. Migration 035 builds three `CREATE INDEX` statements against
   `backup_runs(created_at, …)`, but 029 creates the table with
   `started_at` and no `created_at`. The wrapping try/catch silently
   swallowed the resulting `column "created_at" does not exist` ERROR,
   so the migration "succeeded" without ever creating the indexes.
   Switched 035 to reference `started_at` (same chronological semantics)
   and added migration 105 to create the same indexes idempotently for
   deployments whose 035 already ran and silently failed.

2. `run-migrations-safe.js` snapshots `appliedFilenames` *before*
   `detectExistingSchema()` runs. When `detectExistingSchema()` inserts a
   row for e.g. `004_add_categories_and_cms.js` (because its tables exist
   from a partially-completed prior install), the subsequent migration
   loop still doesn't know about that insert, attempts the legacy
   migration anyway, and its transaction-internal
   `insert into migrations` conflicts with the row already there.
   Re-query the applied set after detectExistingSchema so the loop sees
   the corrected snapshot.

No behavioural change for healthy installs. New installs no longer log
the `column "created_at" does not exist` or `duplicate key value
violates unique constraint "migrations_filename_unique"` ERRORs.
This commit is contained in:
Paul Nothaft
2026-05-16 23:56:05 +02:00
parent 3a490844e1
commit 86b33d4dda
3 changed files with 83 additions and 12 deletions
+12 -4
View File
@@ -126,18 +126,26 @@ async function runMigrations() {
const hasActivityLogsTable = await db.schema.hasTable('activity_logs');
// Get applied migrations
const appliedMigrations = await db('migrations').select('filename');
const appliedFilenames = appliedMigrations.map(m => m.filename);
let appliedMigrations = await db('migrations').select('filename');
let appliedFilenames = appliedMigrations.map(m => m.filename);
// Check if this is a new deployment
// It's new if no essential tables exist OR no migrations have been applied
const hasEssentialTables = hasEventsTable && hasPhotosTable && hasAdminTable && hasActivityLogsTable;
const isDatabaseEmpty = !hasEventsTable && !hasPhotosTable && !hasAdminTable && !hasActivityLogsTable;
const isNewDeployment = isDatabaseEmpty || (appliedFilenames.length === 0 && !hasEssentialTables);
// Only detect existing schema for truly existing deployments
if (!isNewDeployment) {
await detectExistingSchema();
// detectExistingSchema may have inserted rows into the migrations
// table (e.g. for 004_add_categories_and_cms.js when photo_categories
// already exists). Re-query so the iteration below sees the up-to-date
// applied set — otherwise the loop attempts those migrations again,
// their tx-internal `insert into migrations` conflicts, and postgres
// logs a "duplicate key" ERROR on every fresh-after-partial install.
appliedMigrations = await db('migrations').select('filename');
appliedFilenames = appliedMigrations.map(m => m.filename);
}
// Get migration files from appropriate directories