From 626cf0ef70a62f110754ef91ce34940fd4365060 Mon Sep 17 00:00:00 2001 From: Lars Hampe Date: Tue, 1 Oct 2024 21:23:27 +0200 Subject: [PATCH] feat(api): run migrations only on production --- apps/api/src/utils/startup.ts | 6 +++++- packages/database/src/migration.ts | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/apps/api/src/utils/startup.ts b/apps/api/src/utils/startup.ts index 4f8a805..a9fad27 100644 --- a/apps/api/src/utils/startup.ts +++ b/apps/api/src/utils/startup.ts @@ -1,3 +1,4 @@ +import path from 'node:path' import { migrateDatabase } from '@boring.tools/database' declare module 'bun' { @@ -10,6 +11,7 @@ declare module 'bun' { } export const startup = async () => { + console.log(__dirname) if (import.meta.env.NODE_ENV === 'test') { if (!import.meta.env.POSTGRES_URL) { console.error('Env Var POSTGRES_URL is missing!') @@ -30,5 +32,7 @@ export const startup = async () => { } }) - await migrateDatabase() + if (import.meta.env.NODE_ENV === 'production') { + await migrateDatabase(path.join(__dirname, 'migrations')) + } } diff --git a/packages/database/src/migration.ts b/packages/database/src/migration.ts index 8106f17..615cc25 100644 --- a/packages/database/src/migration.ts +++ b/packages/database/src/migration.ts @@ -2,10 +2,10 @@ import path from 'node:path' import { migrate } from 'drizzle-orm/postgres-js/migrator' import { client, db } from './' -export const migrateDatabase = async () => { +export const migrateDatabase = async (dir: string) => { try { console.log(__dirname) - await migrate(db, { migrationsFolder: path.join(__dirname, 'migrations') }) + await migrate(db, { migrationsFolder: dir }) await client.end() console.log('Migrations: Ok') } catch (error) {