Skip to content

Commit

Permalink
Improve logging
Browse files Browse the repository at this point in the history
Especially for when errors happen during a connection,
or when we have an advisory lock
  • Loading branch information
ThomWright committed Jan 31, 2020
1 parent 6fd11cb commit cfff57b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
8 changes: 3 additions & 5 deletions src/run-migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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}`,
)
}
}
8 changes: 7 additions & 1 deletion src/with-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,25 @@ export function withConnection<T>(
return async (client: pg.Client): Promise<T> => {
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
}

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}`)
}
Expand Down
3 changes: 3 additions & 0 deletions src/with-lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export function withAdvisoryLock<T>(

const result = await f(client)
return result
} catch (e) {
log(`Error while using lock: ${e.message}`)
throw e
} finally {
try {
log("Releasing advisory lock...")
Expand Down

0 comments on commit cfff57b

Please sign in to comment.