mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-22 17:26:41 +02:00
The media schema/tables were never created on fresh deploys because mana-media only shipped a `db:push` script and nothing ever ran it in the container. Result: every upload returned 500 the moment a new environment came up (just hit prod again on mana.how). - Add `db:generate` + `db:migrate` scripts and a migrate.ts runner - Generate the initial migration covering media/media_references/ media_thumbnails (matches what was already on local + prod, which were stamped manually so the migrator skips on existing deploys) - Call runMigrations() at startup in src/index.ts so future fresh containers self-bootstrap. Idempotent — drizzle tracks state in drizzle.__drizzle_migrations. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
/**
|
|
* Drizzle migration runner.
|
|
*
|
|
* Run on every container startup (and as `pnpm db:migrate` for local use)
|
|
* so that fresh deployments end up with the `media` schema and tables
|
|
* without any manual SQL. Drizzle's migrator tracks applied migrations
|
|
* in `drizzle.__drizzle_migrations`, so re-runs are no-ops.
|
|
*/
|
|
import { drizzle } from 'drizzle-orm/postgres-js';
|
|
import { migrate } from 'drizzle-orm/postgres-js/migrator';
|
|
import postgres from 'postgres';
|
|
import { fileURLToPath } from 'url';
|
|
import { dirname, resolve } from 'path';
|
|
|
|
export async function runMigrations(databaseUrl: string): Promise<void> {
|
|
// Migrations live next to this file at runtime, regardless of cwd.
|
|
const here = dirname(fileURLToPath(import.meta.url));
|
|
const migrationsFolder = resolve(here, 'migrations');
|
|
|
|
// `max: 1` is required by drizzle's migrator.
|
|
const sql = postgres(databaseUrl, { max: 1 });
|
|
try {
|
|
const db = drizzle(sql);
|
|
await migrate(db, { migrationsFolder });
|
|
} finally {
|
|
await sql.end({ timeout: 5 });
|
|
}
|
|
}
|
|
|
|
// Allow `bun run src/db/migrate.ts` for manual / CI use.
|
|
if (import.meta.main) {
|
|
const databaseUrl = process.env.DATABASE_URL;
|
|
if (!databaseUrl) {
|
|
console.error('DATABASE_URL is required');
|
|
process.exit(1);
|
|
}
|
|
runMigrations(databaseUrl)
|
|
.then(() => {
|
|
console.log('Migrations applied');
|
|
process.exit(0);
|
|
})
|
|
.catch((err) => {
|
|
console.error('Migration failed:', err);
|
|
process.exit(1);
|
|
});
|
|
}
|