diff --git a/server/plugins/1.on-startup.ts b/server/plugins/1.on-startup.ts index fe14eb4..295cea7 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 { runMigrations } from '../tasks/db/migrate'; export default defineNitroPlugin(async () => { const startKey = randomUUID(); @@ -8,6 +9,7 @@ export default defineNitroPlugin(async () => { startKey, }; + await runMigrations(); await seedDatabase(payload); await configureStorage(); await configureCache(); diff --git a/server/tasks/db/migrate.ts b/server/tasks/db/migrate.ts new file mode 100644 index 0000000..1ace6c8 --- /dev/null +++ b/server/tasks/db/migrate.ts @@ -0,0 +1,24 @@ +import { migrate } from 'drizzle-orm/node-postgres/migrator'; + +export const runMigrations = async () => { + const db = await useDatabase(); + try { + await migrate(db, { + migrationsFolder: 'server/db/migrations', + }); + console.log('Database migrations done'); + } catch (error) { + console.error('Database migrations failed', error); + } +}; + +export default defineTask({ + meta: { + name: 'db:migrate', + description: 'Run database migrations', + }, + async run() { + await runMigrations(); + return { result: undefined }; + }, +});