UNPKG

kysely

Version:

Type safe SQL query builder

29 lines (28 loc) 1.11 kB
/// <reference types="./postgres-adapter.d.ts" /> import { sql } from '../../raw-builder/sql.js'; import { DialectAdapterBase } from '../dialect-adapter-base.js'; const LOCK_ID = BigInt('3853314791062309107'); const LOCK_TIMEOUT_MILLISECONDS = 60 * 60 * 1_000; export class PostgresAdapter extends DialectAdapterBase { get supportsTransactionalDdl() { return true; } get supportsReturning() { return true; } async acquireMigrationLock(db, _opt) { // in 1 RTT, acquire a session-level advisory lock with a timeout. // // if ever this runs inside a transaction, niche edge case we support - the // user is responsible to set a different timeout value or disable (`0` value). await sql ` with set_timeout as ( select set_config('lock_timeout', '${sql.lit(LOCK_TIMEOUT_MILLISECONDS)}', true) as config_val ) select pg_advisory_lock(${sql.lit(LOCK_ID)}) from set_timeout`.execute(db); } async releaseMigrationLock(db, _opt) { await sql `select pg_advisory_unlock(${sql.lit(LOCK_ID)})`.execute(db); } }