diff --git a/app/lib/db/migrate.ts b/app/lib/db/migrate.ts new file mode 100644 index 0000000..2b3968c --- /dev/null +++ b/app/lib/db/migrate.ts @@ -0,0 +1,19 @@ +import { migrate } from "drizzle-orm/neon-http/migrator"; +import { drizzle } from "drizzle-orm/neon-http"; +import * as schema from "./schema"; + +export async function runMigrations() { + if (!process.env.DATABASE_URL) { + throw new Error("DATABASE_URL environment variable is not set"); + } + + try { + const db = drizzle(process.env.DATABASE_URL, { schema }); + console.log("[DB] Running migrations..."); + await migrate(db, { migrationsFolder: "./drizzle" }); + console.log("[DB] Migrations completed successfully"); + } catch (error) { + console.error("[DB] Migration failed:", error); + throw error; + } +} diff --git a/server/plugins/migrations.ts b/server/plugins/migrations.ts new file mode 100644 index 0000000..e9b2cfc --- /dev/null +++ b/server/plugins/migrations.ts @@ -0,0 +1,15 @@ +import { runMigrations } from "../../app/lib/db/migrate"; + +export default defineNitroPlugin(async () => { + // Run migrations once when the server starts + try { + await runMigrations(); + } catch (error) { + console.error("[Server] Failed to run migrations on startup:", error); + // In production, you may want to throw here to prevent server startup + // For now, we'll just log the error and continue + if (process.env.NODE_ENV === "production") { + throw error; + } + } +});