From 74f51d795e5e2e14f6aeb8874029078a35a35d22 Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Mon, 9 Sep 2024 06:55:33 +0530 Subject: [PATCH] Run migration on server startup independently --- server/db/migration.ts | 15 +++++++++++++++ server/plugins/1.on-startup.ts | 2 ++ 2 files changed, 17 insertions(+) create mode 100644 server/db/migration.ts diff --git a/server/db/migration.ts b/server/db/migration.ts new file mode 100644 index 00000000..bb3d8d4f --- /dev/null +++ b/server/db/migration.ts @@ -0,0 +1,15 @@ +import { migrate } from 'drizzle-orm/node-postgres/migrator'; +import { useDatabase } from '../utils/db'; + +export const dbMigrations = async () => { + const db = await useDatabase(); + + try { + await migrate(db, { + migrationsFolder: 'server/db/migrations', + }); + console.log('Database migrations done'); + } catch (err) { + console.error('Database migrations failed', err); + } +}; diff --git a/server/plugins/1.on-startup.ts b/server/plugins/1.on-startup.ts index 505866c2..059eaad2 100644 --- a/server/plugins/1.on-startup.ts +++ b/server/plugins/1.on-startup.ts @@ -1,5 +1,6 @@ import { randomUUID } from 'uncrypto'; import { seedDatabase, type SeedPayload } from '../utils/tasks/seed-database'; +import { dbMigrations } from '../db/migration'; export default defineNitroPlugin(async () => { const startKey = randomUUID(); @@ -8,6 +9,7 @@ export default defineNitroPlugin(async () => { startKey, }; + await dbMigrations(); await seedDatabase(payload); await configureStorage(); await configureCache();