Files
speckle-server/packages/server/db/migrations.ts
T
Iain Sproat 1f7620a281 chore(logging): log migration of databases (#3700)
- tidy up Database error handling context data
2024-12-16 13:40:29 +00:00

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
}
}
}