Skip to content

Commit

Permalink
feat: explicitely connect to postgres and exit if no connection
Browse files Browse the repository at this point in the history
  • Loading branch information
tippfehlr committed Apr 13, 2024
1 parent 9bdb0dd commit 952424c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { db, migrateToLatest } from './modules/db';
import { db, initDB } from './modules/db';
import { connect } from './modules/bot';
import { writeApi } from './modules/metrics';

Expand All @@ -12,7 +12,7 @@ async function main() {
process.on('SIGINT', close);
process.on('SIGTERM', close);

await migrateToLatest();
await initDB();
await connect();
}

Expand Down
9 changes: 7 additions & 2 deletions src/modules/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { DB, ActivityRoles, Guilds, StatusRoles, Users } from './db.types';
import config from './config';
import { writeIntPoint } from './metrics';

export const pool = new Pool({ connectionString: config.DATABASE_URL, max: 10 });
const pool = new Pool({ connectionString: config.DATABASE_URL, max: 10 });
const dialect = new PostgresDialect({ pool });
export const db = new Kysely<DB>({ dialect });
const migrator = new Migrator({
Expand All @@ -22,7 +22,12 @@ const migrator = new Migrator({
}),
});

export async function migrateToLatest() {
export async function initDB() {
await pool.connect().catch(err => {
log.error('Couldn’t connect to the database: ' + err.code);
process.exit(1);
});

const { error, results } = await migrator.migrateToLatest();

results?.forEach(it => {
Expand Down

0 comments on commit 952424c

Please sign in to comment.