Skip to content

Commit

Permalink
feat(task): run db migration on server startup independently
Browse files Browse the repository at this point in the history
  • Loading branch information
shivam-sharma7 committed Nov 3, 2024
1 parent 6780f2f commit b0b6aba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions server/plugins/1.on-startup.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -8,6 +9,7 @@ export default defineNitroPlugin(async () => {
startKey,
};

await runMigrations();
await seedDatabase(payload);
await configureStorage();
await configureCache();
Expand Down
24 changes: 24 additions & 0 deletions server/tasks/db/migrate.ts
Original file line number Diff line number Diff line change
@@ -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 };
},
});

0 comments on commit b0b6aba

Please sign in to comment.