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();