Fail loudly on connection error while running migrations

Closes #77
This commit is contained in:
Bruno Bernardino
2025-07-11 09:14:17 +01:00
parent e0ad428a9f
commit 5d324aac9e
4 changed files with 15 additions and 4 deletions

View File

@@ -1,4 +1,4 @@
import { Client } from 'https://deno.land/x/postgres@v0.19.3/mod.ts';
import { Client } from 'postgres';
import 'std/dotenv/load.ts';
const POSTGRESQL_HOST = Deno.env.get('POSTGRESQL_HOST') || '';
@@ -21,8 +21,14 @@ const tls = POSTGRESQL_CAFILE
export default class Database {
protected db?: Client;
protected throwOnConnectionError?: boolean;
constructor(
{ connectNow = false, throwOnConnectionError = false }: { connectNow?: boolean; throwOnConnectionError?: boolean } =
{},
) {
this.throwOnConnectionError = throwOnConnectionError;
constructor(connectNow = false) {
if (connectNow) {
this.connectToPostgres();
}
@@ -64,6 +70,10 @@ export default class Database {
console.log('Failed to connect to Postgres!');
console.error(error);
if (this.throwOnConnectionError) {
throw error;
}
// This allows tests (and the app) to work even if Postgres is not available
const mockPostgresClient = {
queryObject: () => {