feat(api): Auto-Apply von Drizzle-Migrations beim Boot
Some checks are pending
CI / validate (push) Waiting to run
Some checks are pending
CI / validate (push) Waiting to run
Folge-Adoption nach Pageta (mana/docs/playbooks/MIGRATIONS_BOOTSTRAP.md). Versionierte Migrations lagen schon im Repo (src/db/migrations/), aber ohne Auto-Apply-Hook würde ein Volume-Reset die DB schema-leer lassen und Container-Restart liefe dann auf 500er gegen fehlende Tabellen. - src/db/migrate.ts (runMigrations-Helper mit onnotice-Suppress) - src/index.ts top-level-await Hook hinter _RUN_MIGRATIONS - infrastructure/docker-compose.production.yml: _RUN_MIGRATIONS='true' Idempotent über drizzle.__drizzle_migrations. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
56de993fc3
commit
90816de934
3 changed files with 37 additions and 0 deletions
24
apps/api/src/db/migrate.ts
Normal file
24
apps/api/src/db/migrate.ts
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import { drizzle } from 'drizzle-orm/postgres-js';
|
||||
import { migrate } from 'drizzle-orm/postgres-js/migrator';
|
||||
import postgres from 'postgres';
|
||||
|
||||
/**
|
||||
* Drizzle-Migrations beim API-Boot anwenden. Idempotent — Drizzle führt
|
||||
* `drizzle.__drizzle_migrations` als Tracking-Tabelle, applied
|
||||
* Migrations werden geskipped.
|
||||
*
|
||||
* Pattern: mana/docs/playbooks/MIGRATIONS_BOOTSTRAP.md
|
||||
*/
|
||||
export async function runMigrations(databaseUrl: string, migrationsFolder: string): Promise<void> {
|
||||
console.log('[migrate] applying drizzle migrations…');
|
||||
// `onnotice` schluckt Postgres-NOTICEs („schema drizzle already
|
||||
// exists, skipping"), die der Migrator beim 2.+ Boot wirft.
|
||||
const client = postgres(databaseUrl, { max: 1, onnotice: () => {} });
|
||||
try {
|
||||
const db = drizzle(client);
|
||||
await migrate(db, { migrationsFolder, migrationsSchema: 'drizzle' });
|
||||
console.log('[migrate] done');
|
||||
} finally {
|
||||
await client.end({ timeout: 5 });
|
||||
}
|
||||
}
|
||||
|
|
@ -23,6 +23,15 @@ import { pullRequestsRouter as marketplacePullRequestsRouter } from './routes/ma
|
|||
import { discussionsRouter as marketplaceDiscussionsRouter } from './routes/marketplace/discussions.ts';
|
||||
import { moderationRouter as marketplaceModerationRouter } from './routes/marketplace/moderation.ts';
|
||||
import { marketplaceMeRouter } from './routes/marketplace/me.ts';
|
||||
import { runMigrations } from './db/migrate.ts';
|
||||
|
||||
// Drizzle-Migrations beim Boot (vor Routen-Mount). Aktivieren via env-
|
||||
// Flag — Production hat's an, Local-Dev meist aus weil dort drizzle-kit
|
||||
// push direkt benutzt wird. Siehe mana/docs/playbooks/MIGRATIONS_BOOTSTRAP.md
|
||||
if (process.env.WORDECK_RUN_MIGRATIONS === 'true' && process.env.DATABASE_URL) {
|
||||
const folder = process.env.WORDECK_MIGRATIONS_FOLDER ?? './src/db/migrations';
|
||||
await runMigrations(process.env.DATABASE_URL, folder);
|
||||
}
|
||||
|
||||
const app = new Hono();
|
||||
|
||||
|
|
|
|||
|
|
@ -65,6 +65,10 @@ services:
|
|||
# auf 'true' setzen (und sofort wieder rausnehmen).
|
||||
WORDECK_AUTH_DEV_STUB: ${WORDECK_AUTH_DEV_STUB:-false}
|
||||
NODE_ENV: production
|
||||
# Drizzle-Migrations beim Container-Start automatisch
|
||||
# anwenden (idempotent über `drizzle.__drizzle_migrations`).
|
||||
# Siehe mana/docs/playbooks/MIGRATIONS_BOOTSTRAP.md
|
||||
WORDECK_RUN_MIGRATIONS: 'true'
|
||||
ports:
|
||||
- '127.0.0.1:3191:3081'
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue