Skip to content

Commit

Permalink
Run migration on server startup independently
Browse files Browse the repository at this point in the history
  • Loading branch information
shivam-sharma7 committed Sep 16, 2024
1 parent 592b1fe commit 74f51d7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
15 changes: 15 additions & 0 deletions server/db/migration.ts
Original file line number Diff line number Diff line change
@@ -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);
}
};
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 { dbMigrations } from '../db/migration';

export default defineNitroPlugin(async () => {
const startKey = randomUUID();
Expand All @@ -8,6 +9,7 @@ export default defineNitroPlugin(async () => {
startKey,
};

await dbMigrations();
await seedDatabase(payload);
await configureStorage();
await configureCache();
Expand Down

0 comments on commit 74f51d7

Please sign in to comment.