From cfff57b097d3f2894cd456b5b5973018637740f9 Mon Sep 17 00:00:00 2001 From: Thom Wright Date: Fri, 31 Jan 2020 14:18:52 +0000 Subject: [PATCH] Improve logging Especially for when errors happen during a connection, or when we have an advisory lock --- src/migrate.ts | 5 +++-- src/run-migration.ts | 8 +++----- src/with-connection.ts | 8 +++++++- src/with-lock.ts | 3 +++ 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/migrate.ts b/src/migrate.ts index b874f81..8ffe73e 100644 --- a/src/migrate.ts +++ b/src/migrate.ts @@ -120,8 +120,9 @@ async function fetchAppliedMigrationFromDB( ) { let appliedMigrations = [] if (await doesTableExist(client, migrationTableName)) { - log(`Migrations table with name '${migrationTableName}' exists, -filtering not applied migrations.`) + log( + `Migrations table with name '${migrationTableName}' exists, filtering not applied migrations.`, + ) const {rows} = await client.query( `SELECT * FROM ${migrationTableName} ORDER BY id`, diff --git a/src/run-migration.ts b/src/run-migration.ts index 8dff44f..8b2b01f 100644 --- a/src/run-migration.ts +++ b/src/run-migration.ts @@ -20,8 +20,6 @@ const insertMigration = async ( SQL` ("id", "name", "hash") VALUES (${migration.id},${migration.name},${migration.hash})`, ) - log(`Executing query: ${sql.text}:${sql.values}`) - return client.query(sql) } @@ -55,8 +53,8 @@ export const runMigration = ( } catch { // } - throw new Error(`An error occurred running '${migration.name}'. Rolled back this migration. -No further migrations were run. -Reason: ${err.message}`) + throw new Error( + `An error occurred running '${migration.name}'. Rolled back this migration. No further migrations were run. Reason: ${err.message}`, + ) } } diff --git a/src/with-connection.ts b/src/with-connection.ts index 3a82702..682ef3f 100644 --- a/src/with-connection.ts +++ b/src/with-connection.ts @@ -8,8 +8,9 @@ export function withConnection( return async (client: pg.Client): Promise => { try { try { + log("Connecting to database...") await client.connect() - log("Connected to database") + log("... connected to database") } catch (e) { log(`Error connecting to database: ${e.message}`) throw e @@ -17,10 +18,15 @@ export function withConnection( const result = await f(client) return result + } catch (e) { + log(`Error using connection: ${e.message}`) + throw e } finally { // always try to close the connection try { + log("Closing connection...") await client.end() + log("... connection closed") } catch (e) { log(`Error closing the connection: ${e.message}`) } diff --git a/src/with-lock.ts b/src/with-lock.ts index ef8ef9f..4e519d2 100644 --- a/src/with-lock.ts +++ b/src/with-lock.ts @@ -17,6 +17,9 @@ export function withAdvisoryLock( const result = await f(client) return result + } catch (e) { + log(`Error while using lock: ${e.message}`) + throw e } finally { try { log("Releasing advisory lock...")