Fix: replace broken migrate import with raw SQL migration
Previous commit removed the drizzle-orm/migrator import but left the function call, causing a crash. Now uses raw SQL execution directly. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -15,18 +15,37 @@ import type { AppEnv } from './types.js';
|
|||||||
|
|
||||||
const db = createDb(process.env.DATABASE_URL!);
|
const db = createDb(process.env.DATABASE_URL!);
|
||||||
|
|
||||||
// Auto-migrate on startup in production
|
// Auto-migrate on startup — execute raw SQL from migration files
|
||||||
if (process.env.NODE_ENV === 'production') {
|
{
|
||||||
console.log('[Boot] Running migrations...');
|
const fs = await import('fs');
|
||||||
|
const pathMod = await import('path');
|
||||||
|
const { sql: dsql } = await import('drizzle-orm');
|
||||||
|
const folder = pathMod.resolve(process.cwd(), 'packages/db/src/migrations');
|
||||||
try {
|
try {
|
||||||
// Resolve relative to the working directory (which is /app in Docker)
|
const journalPath = pathMod.join(folder, 'meta', '_journal.json');
|
||||||
const path = await import('path');
|
if (fs.existsSync(journalPath)) {
|
||||||
const folder = path.resolve(process.cwd(), 'packages/db/src/migrations');
|
const journal = JSON.parse(fs.readFileSync(journalPath, 'utf8'));
|
||||||
console.log(`[Boot] Migrations folder: ${folder}`);
|
for (const entry of journal.entries) {
|
||||||
await migrate(db, { migrationsFolder: folder });
|
const sqlFile = pathMod.join(folder, `${entry.tag}.sql`);
|
||||||
console.log('[Boot] Migrations applied.');
|
if (!fs.existsSync(sqlFile)) continue;
|
||||||
} catch (err) {
|
const rawSql = fs.readFileSync(sqlFile, 'utf8');
|
||||||
console.error('[Boot] Migration failed:', err);
|
const stmts = rawSql.split('--> statement-breakpoint').map((s: string) => s.trim()).filter(Boolean);
|
||||||
|
for (const stmt of stmts) {
|
||||||
|
try {
|
||||||
|
await db.execute(dsql.raw(stmt));
|
||||||
|
} catch (e: any) {
|
||||||
|
if (!e.message?.includes('already exists') && !e.message?.includes('duplicate')) {
|
||||||
|
console.error(`[Migrate] ${entry.tag}:`, e.message?.slice(0, 200));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
console.log(`[Boot] Migrations applied (${journal.entries.length} files).`);
|
||||||
|
} else {
|
||||||
|
console.log('[Boot] No migration journal found at', journalPath);
|
||||||
|
}
|
||||||
|
} catch (err: any) {
|
||||||
|
console.error('[Boot] Migration error:', err.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user