From 2c1c5ad6383264c0ec97910b56e7a80ea3f19b62 Mon Sep 17 00:00:00 2001 From: MythEclipse Date: Mon, 18 May 2026 08:25:24 +0700 Subject: [PATCH] fix: add Bun database migration runner --- package.json | 2 +- src/db/migrate.ts | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 src/db/migrate.ts diff --git a/package.json b/package.json index 06acf79..57a7ac8 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "dev": "bun --hot src/index.ts", "build": "bun build src/index.ts --target=bun --outfile=dist/index.js", "start": "NODE_ENV=production bun dist/index.js", - "db:migrate": "psql $DATABASE_URL -f schema.sql", + "db:migrate": "bun src/db/migrate.ts", "test": "bun test test/rateLimit.test.ts && bun test test/file.test.ts && bun test test/telegram.test.ts && bun test test/upload.test.ts && bun test test/files.test.ts && bun test test/health.test.ts && bun test test/bot.test.ts && bun test test/bootstrap.test.ts && bun test test/swagger.test.ts", "lint": "bunx biome check src test", "format": "bunx biome format --write src test" diff --git a/src/db/migrate.ts b/src/db/migrate.ts new file mode 100644 index 0000000..5237417 --- /dev/null +++ b/src/db/migrate.ts @@ -0,0 +1,16 @@ +import postgres from 'postgres'; +import { config } from '../env'; +import logger from '../utils/logger'; + +const schemaSql = await Bun.file('schema.sql').text(); +const sql = postgres(config.databaseUrl, { max: 1 }); + +try { + await sql.unsafe(schemaSql); + logger.info('Database migration completed'); +} catch (error: any) { + logger.error('Database migration failed', { error: error.message }); + process.exitCode = 1; +} finally { + await sql.end(); +}