1f7620a281
- tidy up Database error handling context data
40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
import { Knex } from 'knex'
|
|
import { DatabaseError } from '@/modules/shared/errors'
|
|
import { ensureError } from '@speckle/shared'
|
|
import { logger } from '@/logging/logging'
|
|
|
|
export const migrateDbToLatest = async (params: { db: Knex; region: string }) => {
|
|
const { db, region } = params
|
|
try {
|
|
const endStopWatch = stopWatch()
|
|
|
|
await db.migrate.latest()
|
|
|
|
const durationMs = endStopWatch().milliseconds
|
|
logger.info(
|
|
{ region, durationMs },
|
|
'Migrated db to latest for region "{region}" in {durationMs}ms.'
|
|
)
|
|
} catch (err: unknown) {
|
|
throw new DatabaseError('Error migrating db to latest for region "{region}".', db, {
|
|
cause: ensureError(err, 'Unknown postgres error'),
|
|
info: { region }
|
|
})
|
|
}
|
|
}
|
|
|
|
const stopWatch = () => {
|
|
const start = process.hrtime.bigint()
|
|
return () => {
|
|
const end = process.hrtime.bigint()
|
|
const elapsedNanoseconds = Number(end - start)
|
|
const elapsedMs = elapsedNanoseconds / 1e6
|
|
const elapsedS = elapsedNanoseconds / 1e9
|
|
return {
|
|
nanoSeconds: elapsedNanoseconds,
|
|
milliseconds: elapsedMs,
|
|
seconds: elapsedS
|
|
}
|
|
}
|
|
}
|