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

@@ -36,6 +36,7 @@
"$fresh/": "https://deno.land/x/fresh@1.7.3/",
"std/": "https://deno.land/std@0.224.0/",
"$std/": "https://deno.land/std@0.224.0/",
"postgres": "https://deno.land/x/postgres@v0.19.3/mod.ts",
"preact": "https://esm.sh/preact@10.23.2",
"preact/": "https://esm.sh/preact@10.23.2/",
"@preact/signals": "https://esm.sh/*@preact/signals@1.3.0",

View File

@@ -1,6 +1,6 @@
services:
website:
image: ghcr.io/bewcloud/bewcloud:v2.2.2
image: ghcr.io/bewcloud/bewcloud:v2.2.3
restart: always
ports:
- 127.0.0.1:8000:8000

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: () => {

View File

@@ -6,7 +6,7 @@ const migrationsDirectoryPath = `${Deno.cwd()}/db-migrations`;
const migrationsDirectory = Deno.readDir(migrationsDirectoryPath);
const db = new Database();
const db = new Database({ throwOnConnectionError: true });
interface Migration {
id: string;