feat: fix migrations logging
All checks were successful
Build and Push Docker Image / tests (push) Successful in 36s
Build and Push Docker Image / build (push) Successful in 53s

This commit is contained in:
Lars Hampe 2024-10-02 22:03:44 +02:00
parent 8ebb1c95dc
commit 30f79fc6c3
3 changed files with 20 additions and 6 deletions

View File

@ -1,3 +1,4 @@
import path from 'node:path'
import { migrateDatabase } from '@boring.tools/database'
import { logger } from '@boring.tools/logger'
@ -36,5 +37,11 @@ export const startup = async () => {
await migrateDatabase('migrations')
}
if (import.meta.env.NODE_ENV === 'development') {
await migrateDatabase(
path.join(__dirname, '../../../../packages/database/src/migrations'),
)
}
logger.info('API started')
}

View File

@ -1,14 +1,12 @@
import { logger } from '@boring.tools/logger'
import { migrate } from 'drizzle-orm/postgres-js/migrator'
import { client, db } from './'
import { db } from './'
export const migrateDatabase = async (dir: string) => {
try {
await migrate(db, { migrationsFolder: dir })
await client.end()
logger.log('Migrations: Ok')
logger.info('Migrations: Ok')
} catch (error) {
logger.error('Migrations: Failed')
logger.error(error)
logger.error('Migrations: Failed', error)
}
}

View File

@ -4,11 +4,20 @@ import winston from 'winston'
// Create a Winston logger - passing in the Logtail transport
export const logger = winston.createLogger({
format: winston.format.json(),
transports: [],
})
if (import.meta.env.NODE_ENV !== 'test') {
if (import.meta.env.NODE_ENV === 'production') {
// Create a Logtail client
const logtail = new Logtail(process.env.BETTERSTACK_LOG_TOKEN as string)
logger.add(new LogtailTransport(logtail))
}
if (import.meta.env.NODE_ENV === 'development') {
logger.add(
new winston.transports.Console({
format: winston.format.json(),
}),
)
}