import { sql } from "drizzle-orm";
//#region src/locking.ts
/**
* Execute fn under an advisory lock.
* - Postgres: acquires pg_advisory_xact_lock(key) within the transaction
* - SQLite: passthrough (single-writer serializes at write-lock grain)
*
* BEHAVIORAL NOTE: PG blocks concurrent callers per-key; SQLite serializes
* at coarser grain (the write lock). Callers needing fine-grained fairness
* must know SQLite has different timing characteristics.
*/
async function withAdvisoryLock(db, key, fn, dialect) {
	if (dialect === "sqlite") return fn();
	await db.execute(sql`SELECT pg_advisory_xact_lock(${key})`);
	return fn();
}
//#endregion
export { withAdvisoryLock };
