(
tableId: string,
mutate: (table: TableDefinition, trx: DbTransaction) => Promise<T>,
opts?: { includeArchived?: boolean }
)
| 58 | * `statement_timeout` set in `setTableTxTimeouts`. |
| 59 | */ |
| 60 | export async function withLockedTable<T>( |
| 61 | tableId: string, |
| 62 | mutate: (table: TableDefinition, trx: DbTransaction) => Promise<T>, |
| 63 | opts?: { includeArchived?: boolean } |
| 64 | ): Promise<T> { |
| 65 | return db.transaction(async (trx) => { |
| 66 | await setTableTxTimeouts(trx) |
| 67 | await trx.execute( |
| 68 | sql`SELECT pg_advisory_xact_lock(hashtextextended(${`user_table_schema:${tableId}`}, 0))` |
| 69 | ) |
| 70 | const table = await getTableById(tableId, { tx: trx, includeArchived: opts?.includeArchived }) |
| 71 | if (!table) { |
| 72 | throw new Error('Table not found') |
| 73 | } |
| 74 | return mutate(table, trx) |
| 75 | }) |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Returns `schema` with `columns` sorted by `metadata.columnOrder` (the user- |
no test coverage detected