@fuzdev/fuz_app 0.186.0 → 0.187.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/dist/auth/CLAUDE.md +10 -5
  2. package/dist/auth/account_queries.d.ts +8 -5
  3. package/dist/auth/account_queries.d.ts.map +1 -1
  4. package/dist/auth/account_queries.js +8 -5
  5. package/dist/auth/role_grant_offer_action_specs.d.ts +1 -1
  6. package/dist/auth/role_grant_offer_action_specs.d.ts.map +1 -1
  7. package/dist/auth/role_grant_offer_action_specs.js +2 -1
  8. package/dist/auth/role_grant_offer_actions.d.ts +12 -0
  9. package/dist/auth/role_grant_offer_actions.d.ts.map +1 -1
  10. package/dist/auth/role_grant_offer_actions.js +48 -20
  11. package/dist/auth/role_grant_offer_queries.d.ts +16 -3
  12. package/dist/auth/role_grant_offer_queries.d.ts.map +1 -1
  13. package/dist/auth/role_grant_offer_queries.js +24 -4
  14. package/dist/auth/role_grant_queries.d.ts +102 -11
  15. package/dist/auth/role_grant_queries.d.ts.map +1 -1
  16. package/dist/auth/role_grant_queries.js +129 -15
  17. package/dist/db/migrate.d.ts +46 -22
  18. package/dist/db/migrate.d.ts.map +1 -1
  19. package/dist/db/migrate.js +103 -85
  20. package/dist/server/app_backend.d.ts.map +1 -1
  21. package/dist/server/app_backend.js +1 -4
  22. package/dist/testing/CLAUDE.md +35 -0
  23. package/dist/testing/cross_backend/backend_both_live.d.ts +47 -0
  24. package/dist/testing/cross_backend/backend_both_live.d.ts.map +1 -0
  25. package/dist/testing/cross_backend/backend_both_live.js +566 -0
  26. package/dist/testing/cross_backend/backend_swap.d.ts.map +1 -1
  27. package/dist/testing/cross_backend/backend_swap.js +37 -201
  28. package/dist/testing/cross_backend/shared_database_backends.d.ts +175 -0
  29. package/dist/testing/cross_backend/shared_database_backends.d.ts.map +1 -0
  30. package/dist/testing/cross_backend/shared_database_backends.js +298 -0
  31. package/dist/testing/cross_backend/spine_suites.d.ts +1 -1
  32. package/dist/testing/cross_backend/spine_suites.d.ts.map +1 -1
  33. package/dist/testing/cross_backend/spine_suites.js +1 -0
  34. package/package.json +4 -3
@@ -43,6 +43,96 @@ const role_grant_expr = iso8601_timestamp_expr(ROLE_GRANT_COLUMNS, [
43
43
  'expires_at',
44
44
  'revoked_at'
45
45
  ])('');
46
+ /**
47
+ * The first half of every scope lock's key (`scope_advisory_lock_key`): the
48
+ * ASCII of `fuzs`. Reserved for scope locks — no other lock in the spine takes it.
49
+ *
50
+ * Two-`int4` keys, `pg_advisory_xact_lock(int4, int4)`, sit a key space apart
51
+ * from the single `bigint` key the migration runner takes (`pg_locks.objsubid`
52
+ * 2 against 1), and this half differs from the purge lock's
53
+ * (`ACCOUNT_PURGE_ADVISORY_LOCK_KEY`, `fuz_`), so a scope lock meets neither.
54
+ * Twin of the Rust `fuz_auth::role_grant_offer_queries::SCOPE_ADVISORY_LOCK_CLASS`.
55
+ */
56
+ export const SCOPE_ADVISORY_LOCK_CLASS = 0x6675_7a73;
57
+ /**
58
+ * The advisory lock key of `scope_id`: `SCOPE_ADVISORY_LOCK_CLASS`, then the
59
+ * UUID's four big-endian 32-bit words folded by XOR into one signed `int4`.
60
+ *
61
+ * Every word counts — a UUIDv7 leads with its timestamp, so a prefix would put
62
+ * the scopes made in one millisecond on one key. Two scopes can still share a
63
+ * key; they then take turns, which costs a wait and nothing else. Twin of the
64
+ * Rust `fuz_auth::role_grant_offer_queries::scope_advisory_lock_key`, pinned on
65
+ * both sides by the same vectors.
66
+ *
67
+ * @param scope_id - the scope
68
+ * @returns the `(int4, int4)` pair `pg_advisory_xact_lock` takes
69
+ * @throws Error if `scope_id` is not a UUID's 32 hex digits
70
+ */
71
+ export const scope_advisory_lock_key = (scope_id) => {
72
+ const hex = scope_id.replaceAll('-', '');
73
+ if (!/^[0-9a-f]{32}$/i.test(hex)) {
74
+ throw new Error(`scope lock key: not a UUID: ${scope_id}`);
75
+ }
76
+ let folded = 0;
77
+ for (let i = 0; i < 32; i += 8) {
78
+ folded ^= Number.parseInt(hex.slice(i, i + 8), 16);
79
+ }
80
+ return [SCOPE_ADVISORY_LOCK_CLASS, folded];
81
+ };
82
+ /**
83
+ * Take `scope_id`'s scope lock shared, until the transaction ends.
84
+ *
85
+ * The lock orders a scope's teardown against every write that brings a grant
86
+ * or an offer into the scope: `query_role_grant_revoke_for_scope` takes it
87
+ * exclusive as its first statement, and the scoped writes take it shared —
88
+ * `query_create_role_grant` and `query_role_grant_offer_create` first thing,
89
+ * `query_accept_offer` after its unlocked head read, before any row, and the
90
+ * `role_grant_offer_create` and `role_grant_assign` handlers before the
91
+ * consumer's authorizer runs. A write
92
+ * that reads its scope after the lock sees a teardown that committed first,
93
+ * and one holding the lock makes a teardown wait and then end what it wrote.
94
+ * Writers take turns with a teardown only, not with each other. Re-entrant:
95
+ * a second take in one transaction is free.
96
+ *
97
+ * Take it first, holding no row: the teardown holds it while it waits on offer
98
+ * and grant rows and on whatever its consumer's scope delete cascades into (an
99
+ * invite or a request at the scope), so a transaction that waits on it holding
100
+ * one of those deadlocks. The scope id may come from an unlocked read of a
101
+ * column that never changes, as the accept's head read does. Use it on a
102
+ * consumer path that reads its own scope row and then brings something into the
103
+ * scope (an invite accept, a join decision): the lock, then the read. A
104
+ * transaction never takes two scope locks here; one that must should take them
105
+ * in key order. Twin of the Rust
106
+ * `fuz_auth::role_grant_offer_queries::query_scope_lock_shared`.
107
+ *
108
+ * @param deps - query dependencies (must be the writing transaction)
109
+ * @param scope_id - the scope
110
+ */
111
+ export const query_scope_lock_shared = async (deps, scope_id) => {
112
+ await deps.db.query('SELECT pg_advisory_xact_lock_shared($1, $2)', [
113
+ ...scope_advisory_lock_key(scope_id)
114
+ ]);
115
+ };
116
+ /**
117
+ * Take `scope_id`'s scope lock exclusive, until the transaction ends: every
118
+ * scoped write (`query_scope_lock_shared`) waits until then, and this waits for
119
+ * every one in flight.
120
+ *
121
+ * `query_role_grant_revoke_for_scope` takes it as its first statement. A
122
+ * consumer's teardown that must hold the lock before that — to read or lock
123
+ * its own scope row first — takes it here, holding no row, then calls the
124
+ * cascade (which takes it again, free) — never after taking it shared in the
125
+ * same transaction: two shared holders that both upgrade deadlock. Twin of the
126
+ * Rust `fuz_auth::role_grant_offer_queries::query_scope_lock_exclusive`.
127
+ *
128
+ * @param deps - query dependencies (must be the teardown's transaction)
129
+ * @param scope_id - the scope
130
+ */
131
+ export const query_scope_lock_exclusive = async (deps, scope_id) => {
132
+ await deps.db.query('SELECT pg_advisory_xact_lock($1, $2)', [
133
+ ...scope_advisory_lock_key(scope_id)
134
+ ]);
135
+ };
46
136
  /**
47
137
  * Grant a role_grant to an actor.
48
138
  * Idempotent — if an active (unrevoked and unexpired) role_grant already
@@ -64,12 +154,22 @@ const role_grant_expr = iso8601_timestamp_expr(ROLE_GRANT_COLUMNS, [
64
154
  * `role_grant_scope_kind_paired` CHECK; mismatched pairs raise at the DB
65
155
  * layer rather than producing silent rows.
66
156
  *
157
+ * A scoped grant takes the scope lock shared first (`query_scope_lock_shared`),
158
+ * so a teardown of the scope (`query_role_grant_revoke_for_scope`) in flight
159
+ * is waited for, and one that starts after it waits for this transaction and
160
+ * then revokes the grant. Run it inside the caller's transaction, holding no
161
+ * row unless the transaction already holds this scope's lock (the lock's
162
+ * rule; a re-take is free); a caller that judged the scope live takes the
163
+ * lock before that read.
164
+ *
67
165
  * @param deps - query dependencies
68
166
  * @param input - the role_grant fields
69
167
  * @returns the created or existing active role_grant
70
168
  * @mutates `role_grant` table - revokes an expired unrevoked row at `(actor_id, role, scope_kind, scope_id)`, then inserts a row when no active role_grant matches
71
169
  */
72
170
  export const query_create_role_grant = async (deps, input) => {
171
+ if (input.scope_id != null)
172
+ await query_scope_lock_shared(deps, input.scope_id);
73
173
  // an expired row still holds the partial unique slot (`revoked_at IS NULL`)
74
174
  await deps.db.query(`UPDATE role_grant SET revoked_at = NOW(), revoked_by = NULL, revoked_reason = 'expired'
75
175
  WHERE actor_id = $1
@@ -309,7 +409,7 @@ export const query_actor_has_active_global_role = async (deps, actor_id, role) =
309
409
  * sort returns them: each account's row, its actors' and its grants' before
310
410
  * the next account's. `query_account_lock_live_many` and
311
411
  * `query_account_lock_for_offer_accept` take account rows in the same order,
312
- * so an offer or an accept between two admins and an admin's removal never
412
+ * so an offer, an assign or an accept between two admins and an admin's removal never
313
413
  * wait on each other's rows in opposite orders. Ids never change, so no
314
414
  * concurrent update can move a row the sort has placed.
315
415
  *
@@ -391,16 +491,26 @@ export const query_role_grant_find_account_id_for_role = async (deps, role) => {
391
491
  * orphan role_grants and offers. The cascade is **role-agnostic**: anything
392
492
  * attached to the destroyed scope is cleaned up.
393
493
  *
394
- * Both updates run as separate statements inside the caller's transaction,
395
- * the scope's pending offers locked in id order first, then the `role_grant`
396
- * revoke, then the supersede of the offers that lock returned — offers before
397
- * grants, as `query_revoke_role_grant` takes them. No account row keeps a new
398
- * offer at the scope out (the scope's offers span accounts), so the supersede
399
- * writes only the rows it locked: an offer made at the scope after the lock is
400
- * left pending rather than taken out of order. The two halves are
401
- * independent — orphan pending offers can exist at a scope with no active
402
- * role_grants, so the supersede half always runs even when no role_grant was
403
- * revoked.
494
+ * Its first statement takes the scope lock exclusive
495
+ * (`query_scope_lock_exclusive`): every write that brings a grant or an offer
496
+ * into the scope takes it shared (`query_scope_lock_shared` has the list), so
497
+ * one in flight commits first and is ended here, and one after waits for this
498
+ * transaction. A write whose consumer authorizer reads the scope reads it after
499
+ * its lock, so once this teardown has removed the scope row and committed, the
500
+ * write is refused. Call it before the transaction touches the scope row or
501
+ * any other row a writer's authorizer locks — a teardown holding such a row
502
+ * while it waits here on a writer that waits on that row deadlocks — or take
503
+ * `query_scope_lock_exclusive` first.
504
+ *
505
+ * Then the updates run as separate statements, the scope's pending offers
506
+ * locked in id order first, then the `role_grant` revoke, then the supersede
507
+ * of the offers that lock returned — offers before grants, as
508
+ * `query_revoke_role_grant` takes them. The supersede writes only the rows it
509
+ * locked; an offer written at the scope after that without the scope lock (a
510
+ * raw insert, which no query here makes) is left pending rather than taken
511
+ * out of order. The two halves are independent — orphan pending offers can
512
+ * exist at a scope with no active role_grants, so the supersede half always
513
+ * runs even when no role_grant was revoked.
404
514
  *
405
515
  * @param deps - query dependencies
406
516
  * @param scope_id - the scope whose role_grants and offers to terminate
@@ -409,16 +519,20 @@ export const query_role_grant_find_account_id_for_role = async (deps, role) => {
409
519
  * @returns the revoked role_grants (with `account_id` for fan-out) and superseded offers (with `from_account_id` for fan-out)
410
520
  * @mutates `role_grant` table - sets `revoked_at`/`revoked_by`/`revoked_reason` on every active row at `scope_id`
411
521
  * @mutates `role_grant_offer` table - stamps `superseded_at` on every pending row at `scope_id` its lock took
522
+ * @mutates the scope lock - holds `scope_id`'s advisory lock exclusive until the transaction ends
412
523
  */
413
524
  export const query_role_grant_revoke_for_scope = async (deps, scope_id, revoked_by, reason) => {
525
+ // The scope lock first, holding nothing: every scoped writer holds it
526
+ // shared until its commit, and none waits here holding a row we lock next.
527
+ await query_scope_lock_exclusive(deps, scope_id);
528
+ // The scope's pending offers first, in id order — before the grants, for
529
+ // the reason `query_revoke_role_grant` locks its offers before its grant.
530
+ const at_scope = `scope_id = $1 AND ${offer_pending_predicate('')}`;
531
+ const locked_ids = (await deps.db.query(`SELECT id FROM role_grant_offer WHERE ${at_scope} ORDER BY id FOR UPDATE`, [scope_id])).map((row) => row.id);
414
532
  // Revoke every active role_grant at the scope. CTE returns `actor_id` directly
415
533
  // from the role_grant row (drives `target_actor_id` audit envelopes); a join
416
534
  // against `actor` resolves `account_id` for `target_account_id`
417
535
  // + WS/SSE socket-close fan-out, all in one round-trip.
418
- // The scope's pending offers first, in id order — before the grants, for
419
- // the reason `query_revoke_role_grant` locks its offers before its grant
420
- const at_scope = `scope_id = $1 AND ${offer_pending_predicate('')}`;
421
- const locked_ids = (await deps.db.query(`SELECT id FROM role_grant_offer WHERE ${at_scope} ORDER BY id FOR UPDATE`, [scope_id])).map((row) => row.id);
422
536
  const revoked = await deps.db.query(`WITH updated AS (
423
537
  UPDATE role_grant
424
538
  SET revoked_at = NOW(), revoked_by = $2, revoked_reason = $3
@@ -42,18 +42,25 @@
42
42
  *
43
43
  * **Forward-only**: No down-migrations. Schema changes are additive.
44
44
  *
45
- * **Advisory locking**: Per-namespace `pg_advisory_lock` reduces contention
46
- * in multi-instance deployments — best-effort, not load-bearing. The locks
47
- * are session-scoped, but `Db.query` runs against a pool that may check out
48
- * a different backend per call, so two concurrent boots can both "hold"
49
- * the lock on different sessions. The real serialization comes from chain-
50
- * tx atomicity + the `(namespace, name)` PK on `schema_version`: the
51
- * loser's INSERT hits a PK violation, the chain-tx rolls back, and the
52
- * next boot reads the committed state and proceeds cleanly. Environments
53
- * without `pg_advisory_lock` (pglet) silently fall through.
45
+ * **Advisory locking**: each namespace's chain transaction opens with
46
+ * `pg_advisory_xact_lock` on the namespace's key, so concurrent boots over
47
+ * one database take turns: the second waits for the first's commit, then
48
+ * reads its migrations applied. The lock is transaction-scoped because a
49
+ * pool hands each `Db.query` whichever client is free — a session lock and
50
+ * its unlock could land on different clients, leaving the lock held on an
51
+ * idle one. Inside the transaction there is nothing to unlock and so nothing
52
+ * to leak: the commit or rollback releases it. The Rust spine takes a
53
+ * session `pg_advisory_lock` on the same `bigint` key, and the two forms
54
+ * conflict, so a TS and a Rust boot take turns too. Without the lock the
55
+ * `(namespace, name)` PK keeps a migration from recording twice, but two
56
+ * chains running at once collide on their own DDL and the loser's boot
57
+ * fails. The read after the lock sees the winner's commit only at READ
58
+ * COMMITTED, the default: under REPEATABLE READ the transaction's snapshot
59
+ * predates the lock's wait. pglet and PGlite take the lock as Postgres does.
54
60
  *
55
61
  * @module
56
62
  */
63
+ import type { Logger } from '@fuzdev/fuz_util/log.ts';
57
64
  import type { Db } from './db.js';
58
65
  /**
59
66
  * A single migration: a name + an `up` function applied inside a transaction.
@@ -142,12 +149,30 @@ export declare const create_spine_migration_namespace: (namespace: string, migra
142
149
  * @throws MigrationError with `kind` `namespace-reserved`, naming the first offending namespace
143
150
  */
144
151
  export declare const assert_migration_namespaces: (namespaces: ReadonlyArray<MigrationNamespace>) => void;
152
+ /**
153
+ * Compute a stable advisory lock key from a namespace string — djb2 over the
154
+ * name's UTF-8 bytes, in int32 arithmetic.
155
+ *
156
+ * The Rust twin hashes the same bytes and widens the int32 to the `bigint`
157
+ * `pg_advisory_lock(bigint)` takes; this number reaches Postgres as that same
158
+ * `bigint`, so one namespace locks one key on both spines.
159
+ *
160
+ * @param namespace - the migration namespace's name
161
+ * @returns the int32 key
162
+ */
163
+ export declare const namespace_lock_key: (namespace: string) => number;
164
+ /** Options for `run_migrations`. */
165
+ export interface RunMigrationsOptions {
166
+ /** Receives the debug line for a tolerated `schema_version` DDL race. */
167
+ log?: Logger;
168
+ }
145
169
  /**
146
170
  * Run pending migrations for each namespace.
147
171
  *
148
- * For each namespace: acquires an advisory lock, reads applied rows ordered
149
- * by `sequence`, length-checks (binary-older-than-db short-circuits), name-
150
- * prefix-verifies, then runs the pending tail in a single chain transaction.
172
+ * For each namespace, in one chain transaction: takes the namespace's
173
+ * advisory lock, reads applied rows ordered by `sequence`, length-checks
174
+ * (binary-older-than-db short-circuits), name-prefix-verifies, then runs the
175
+ * pending tail.
151
176
  * Each migration's row is INSERTed with `sequence = max(sequence) + 1` for
152
177
  * the namespace.
153
178
  *
@@ -160,15 +185,14 @@ export declare const assert_migration_namespaces: (namespaces: ReadonlyArray<Mig
160
185
  * invocation. Namespaces are independent: a later namespace's failure does
161
186
  * not roll back an earlier namespace that already committed.
162
187
  *
163
- * **Concurrency**: per-namespace advisory locks reduce contention in
164
- * multi-instance deployments but are best-effort on pool drivers (see
165
- * the module docstring's "Advisory locking" notes). Correctness on concurrent boots
166
- * falls out of chain-tx atomicity + the `(namespace, name)` PK — the
167
- * loser's INSERT triggers PK violation and rollback; subsequent boots
168
- * see the committed state.
188
+ * **Concurrency**: the per-namespace advisory lock serializes concurrent
189
+ * boots, TS or Rust, over one database (module doc, "Advisory locking").
190
+ * The `schema_version` DDL runs before it, and its race with another boot
191
+ * is tolerated.
169
192
  *
170
193
  * @param db - the database instance
171
194
  * @param namespaces - migration namespaces, processed in the order passed
195
+ * @param options - `log` for the tolerated DDL race
172
196
  * @returns one result per namespace where work happened (already-up-to-date
173
197
  * namespaces are omitted)
174
198
  * @mutates schema_version - inserts one row per applied migration
@@ -176,7 +200,7 @@ export declare const assert_migration_namespaces: (namespaces: ReadonlyArray<Mig
176
200
  * the database), `binary-older-than-db`, `name-divergence-at-N`, or
177
201
  * `migration-failed`
178
202
  */
179
- export declare const run_migrations: (db: Db, namespaces: Array<MigrationNamespace>) => Promise<Array<MigrationResult>>;
203
+ export declare const run_migrations: (db: Db, namespaces: Array<MigrationNamespace>, options?: RunMigrationsOptions) => Promise<Array<MigrationResult>>;
180
204
  /**
181
205
  * Insert tracker rows for the named migrations of a namespace **without
182
206
  * executing them**.
@@ -190,15 +214,15 @@ export declare const run_migrations: (db: Db, namespaces: Array<MigrationNamespa
190
214
  * Contract:
191
215
  * - Creates the `schema_version` table if missing — a promotion script can
192
216
  * call `baseline()` against a fresh tracker with no separate DDL step.
193
- * - Acquires the same per-namespace advisory lock as `run_migrations` (with
194
- * the same try/catch fallback for environments lacking `pg_advisory_lock`).
217
+ * - Takes the same per-namespace advisory lock as `run_migrations`, in the
218
+ * transaction that writes the rows.
195
219
  * - Refuses if any tracker rows already exist *for this namespace* — lets
196
220
  * multi-call baseline scripts resume after partial failure (completed
197
221
  * namespaces guard themselves while remaining ones still run).
198
222
  * - Verifies the supplied names are a strict prefix of the namespace's
199
223
  * current migrations array — a name not in the array, or out of order,
200
224
  * errors before any INSERT.
201
- * - Writes sequences `0..N-1` in one transaction.
225
+ * - Writes sequences `0..N-1` in that transaction.
202
226
  *
203
227
  * @param db - the database instance
204
228
  * @param ns - the namespace whose migrations are being baselined
@@ -1 +1 @@
1
- {"version":3,"file":"migrate.d.ts","sourceRoot":"","sources":["../../src/lib/db/migrate.ts"],"names":[],"mappings":"AAAA;;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GAUG;AACH,eAAO,MAAM,kBAAkB,GAAI,WAAW,MAAM,KAAG,MAMtD,CAAC;AAUF,oCAAoC;AACpC,MAAM,WAAW,oBAAoB;IACpC,yEAAyE;IACzE,GAAG,CAAC,EAAE,MAAM,CAAC;CACb;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,eAAO,MAAM,cAAc,GAC1B,IAAI,EAAE,EACN,YAAY,KAAK,CAAC,kBAAkB,CAAC,EACrC,UAAU,oBAAoB,KAC5B,OAAO,CAAC,KAAK,CAAC,eAAe,CAAC,CAkFhC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,eAAO,MAAM,QAAQ,GACpB,IAAI,EAAE,EACN,IAAI,kBAAkB,EACtB,OAAO,aAAa,CAAC,MAAM,CAAC,KAC1B,OAAO,CAAC,IAAI,CA2Dd,CAAC"}
@@ -42,19 +42,26 @@
42
42
  *
43
43
  * **Forward-only**: No down-migrations. Schema changes are additive.
44
44
  *
45
- * **Advisory locking**: Per-namespace `pg_advisory_lock` reduces contention
46
- * in multi-instance deployments — best-effort, not load-bearing. The locks
47
- * are session-scoped, but `Db.query` runs against a pool that may check out
48
- * a different backend per call, so two concurrent boots can both "hold"
49
- * the lock on different sessions. The real serialization comes from chain-
50
- * tx atomicity + the `(namespace, name)` PK on `schema_version`: the
51
- * loser's INSERT hits a PK violation, the chain-tx rolls back, and the
52
- * next boot reads the committed state and proceeds cleanly. Environments
53
- * without `pg_advisory_lock` (pglet) silently fall through.
45
+ * **Advisory locking**: each namespace's chain transaction opens with
46
+ * `pg_advisory_xact_lock` on the namespace's key, so concurrent boots over
47
+ * one database take turns: the second waits for the first's commit, then
48
+ * reads its migrations applied. The lock is transaction-scoped because a
49
+ * pool hands each `Db.query` whichever client is free — a session lock and
50
+ * its unlock could land on different clients, leaving the lock held on an
51
+ * idle one. Inside the transaction there is nothing to unlock and so nothing
52
+ * to leak: the commit or rollback releases it. The Rust spine takes a
53
+ * session `pg_advisory_lock` on the same `bigint` key, and the two forms
54
+ * conflict, so a TS and a Rust boot take turns too. Without the lock the
55
+ * `(namespace, name)` PK keeps a migration from recording twice, but two
56
+ * chains running at once collide on their own DDL and the loser's boot
57
+ * fails. The read after the lock sees the winner's commit only at READ
58
+ * COMMITTED, the default: under REPEATABLE READ the transaction's snapshot
59
+ * predates the lock's wait. pglet and PGlite take the lock as Postgres does.
54
60
  *
55
61
  * @module
56
62
  */
57
63
  import { to_error_message } from '@fuzdev/fuz_util/error.ts';
64
+ import { pg_error_code } from './pg_error.js';
58
65
  /**
59
66
  * Tagged error thrown by `run_migrations` and `baseline`.
60
67
  *
@@ -139,50 +146,62 @@ CREATE TABLE IF NOT EXISTS schema_version (
139
146
  UNIQUE (namespace, sequence)
140
147
  )`;
141
148
  /**
142
- * Compute a stable int32 advisory lock key from a namespace string.
149
+ * The `schema_version` DDL's outcomes when a concurrent boot created the table
150
+ * first: `CREATE TABLE IF NOT EXISTS` checks for the table before taking any
151
+ * lock, so two boots can both pass the check, and the loser then trips the
152
+ * catalog's unique index (`23505`) or finds the relation (`42P07`) or a
153
+ * constraint (`42710`) already there. The Rust runner tolerates the same
154
+ * three.
155
+ */
156
+ const SCHEMA_VERSION_DDL_RACE_CODES = ['23505', '42P07', '42710'];
157
+ /**
158
+ * Apply the `schema_version` DDL, tolerating the race with a concurrent boot
159
+ * that created the table first (`SCHEMA_VERSION_DDL_RACE_CODES`). It runs
160
+ * before any namespace's lock, so concurrent boots run it at once.
161
+ */
162
+ const ensure_schema_version_table = async (db, log) => {
163
+ try {
164
+ await db.query(SCHEMA_VERSION_DDL);
165
+ }
166
+ catch (err) {
167
+ const code = pg_error_code(err);
168
+ if (code === null || !SCHEMA_VERSION_DDL_RACE_CODES.includes(code))
169
+ throw err;
170
+ log?.debug(`schema_version DDL raced (${code}); another boot won`);
171
+ }
172
+ };
173
+ /**
174
+ * Compute a stable advisory lock key from a namespace string — djb2 over the
175
+ * name's UTF-8 bytes, in int32 arithmetic.
176
+ *
177
+ * The Rust twin hashes the same bytes and widens the int32 to the `bigint`
178
+ * `pg_advisory_lock(bigint)` takes; this number reaches Postgres as that same
179
+ * `bigint`, so one namespace locks one key on both spines.
143
180
  *
144
- * Uses djb2 hash, masked to int32 range for `pg_advisory_lock`.
181
+ * @param namespace - the migration namespace's name
182
+ * @returns the int32 key
145
183
  */
146
- const namespace_lock_key = (namespace) => {
184
+ export const namespace_lock_key = (namespace) => {
147
185
  let hash = 5381;
148
- for (let i = 0; i < namespace.length; i++) {
149
- hash = ((hash << 5) + hash + namespace.charCodeAt(i)) | 0;
186
+ for (const byte of new TextEncoder().encode(namespace)) {
187
+ hash = ((hash << 5) + hash + byte) | 0;
150
188
  }
151
189
  return hash;
152
190
  };
153
191
  /**
154
- * Run `fn` with the namespace's advisory lock held.
155
- *
156
- * Acquire / release are best-effort and silently fall through in
157
- * environments without `pg_advisory_lock` (pglet). The
158
- * `finally` ensures release on any throw from `fn`.
192
+ * Take the namespace's advisory lock in the open transaction `tx` — held
193
+ * until it commits or rolls back (module doc, "Advisory locking").
159
194
  */
160
- const with_namespace_lock = async (db, namespace, fn) => {
161
- const lock_key = namespace_lock_key(namespace);
162
- try {
163
- await db.query('SELECT pg_advisory_lock($1)', [lock_key]);
164
- }
165
- catch {
166
- // Advisory lock not supported — proceed without serialization
167
- }
168
- try {
169
- return await fn();
170
- }
171
- finally {
172
- try {
173
- await db.query('SELECT pg_advisory_unlock($1)', [lock_key]);
174
- }
175
- catch {
176
- // Advisory lock not supported — nothing to release
177
- }
178
- }
195
+ const lock_namespace = async (tx, namespace) => {
196
+ await tx.query('SELECT pg_advisory_xact_lock($1)', [namespace_lock_key(namespace)]);
179
197
  };
180
198
  /**
181
199
  * Run pending migrations for each namespace.
182
200
  *
183
- * For each namespace: acquires an advisory lock, reads applied rows ordered
184
- * by `sequence`, length-checks (binary-older-than-db short-circuits), name-
185
- * prefix-verifies, then runs the pending tail in a single chain transaction.
201
+ * For each namespace, in one chain transaction: takes the namespace's
202
+ * advisory lock, reads applied rows ordered by `sequence`, length-checks
203
+ * (binary-older-than-db short-circuits), name-prefix-verifies, then runs the
204
+ * pending tail.
186
205
  * Each migration's row is INSERTed with `sequence = max(sequence) + 1` for
187
206
  * the namespace.
188
207
  *
@@ -195,15 +214,14 @@ const with_namespace_lock = async (db, namespace, fn) => {
195
214
  * invocation. Namespaces are independent: a later namespace's failure does
196
215
  * not roll back an earlier namespace that already committed.
197
216
  *
198
- * **Concurrency**: per-namespace advisory locks reduce contention in
199
- * multi-instance deployments but are best-effort on pool drivers (see
200
- * the module docstring's "Advisory locking" notes). Correctness on concurrent boots
201
- * falls out of chain-tx atomicity + the `(namespace, name)` PK — the
202
- * loser's INSERT triggers PK violation and rollback; subsequent boots
203
- * see the committed state.
217
+ * **Concurrency**: the per-namespace advisory lock serializes concurrent
218
+ * boots, TS or Rust, over one database (module doc, "Advisory locking").
219
+ * The `schema_version` DDL runs before it, and its race with another boot
220
+ * is tolerated.
204
221
  *
205
222
  * @param db - the database instance
206
223
  * @param namespaces - migration namespaces, processed in the order passed
224
+ * @param options - `log` for the tolerated DDL race
207
225
  * @returns one result per namespace where work happened (already-up-to-date
208
226
  * namespaces are omitted)
209
227
  * @mutates schema_version - inserts one row per applied migration
@@ -211,13 +229,14 @@ const with_namespace_lock = async (db, namespace, fn) => {
211
229
  * the database), `binary-older-than-db`, `name-divergence-at-N`, or
212
230
  * `migration-failed`
213
231
  */
214
- export const run_migrations = async (db, namespaces) => {
232
+ export const run_migrations = async (db, namespaces, options) => {
215
233
  assert_migration_namespaces(namespaces);
216
- await db.query(SCHEMA_VERSION_DDL);
234
+ await ensure_schema_version_table(db, options?.log);
217
235
  const results = [];
218
236
  for (const { namespace, migrations } of namespaces) {
219
- await with_namespace_lock(db, namespace, async () => {
220
- const applied = await db.query(`SELECT name, sequence FROM schema_version
237
+ const applied_names = await db.transaction(async (tx) => {
238
+ await lock_namespace(tx, namespace);
239
+ const applied = await tx.query(`SELECT name, sequence FROM schema_version
221
240
  WHERE namespace = $1
222
241
  ORDER BY sequence ASC`, [namespace]);
223
242
  // Step 3: length check (short-circuits before name verify)
@@ -244,31 +263,31 @@ export const run_migrations = async (db, namespaces) => {
244
263
  }
245
264
  // up-to-date case
246
265
  if (applied.length === migrations.length)
247
- return;
248
- // Step 5: run pending tail in a single chain-tx
266
+ return null;
267
+ // Step 5: run the pending tail in this chain-tx
249
268
  let next_sequence = applied.length > 0 ? applied[applied.length - 1].sequence + 1 : 0;
250
- const applied_names = [];
251
- await db.transaction(async (tx) => {
252
- for (let i = applied.length; i < migrations.length; i++) {
253
- const m = migrations[i];
254
- try {
255
- await m.up(tx);
256
- await tx.query(`INSERT INTO schema_version (namespace, name, sequence)
257
- VALUES ($1, $2, $3)`, [namespace, m.name, next_sequence]);
258
- applied_names.push(m.name);
259
- next_sequence++;
260
- }
261
- catch (err) {
262
- if (err instanceof MigrationError)
263
- throw err;
264
- throw new MigrationError('migration-failed', `Migration ${namespace}["${m.name}"] failed: ` +
265
- `${to_error_message(err)}. ` +
266
- `Hint: fix the migration body and retry; the chain is left at the prior committed version.`, { namespace, at_index: i, cause: err });
267
- }
269
+ const names = [];
270
+ for (let i = applied.length; i < migrations.length; i++) {
271
+ const m = migrations[i];
272
+ try {
273
+ await m.up(tx);
274
+ await tx.query(`INSERT INTO schema_version (namespace, name, sequence)
275
+ VALUES ($1, $2, $3)`, [namespace, m.name, next_sequence]);
276
+ names.push(m.name);
277
+ next_sequence++;
268
278
  }
269
- });
270
- results.push({ namespace, applied_names });
279
+ catch (err) {
280
+ if (err instanceof MigrationError)
281
+ throw err;
282
+ throw new MigrationError('migration-failed', `Migration ${namespace}["${m.name}"] failed: ` +
283
+ `${to_error_message(err)}. ` +
284
+ `Hint: fix the migration body and retry; the chain is left at the prior committed version.`, { namespace, at_index: i, cause: err });
285
+ }
286
+ }
287
+ return names;
271
288
  });
289
+ if (applied_names !== null)
290
+ results.push({ namespace, applied_names });
272
291
  }
273
292
  return results;
274
293
  };
@@ -285,15 +304,15 @@ export const run_migrations = async (db, namespaces) => {
285
304
  * Contract:
286
305
  * - Creates the `schema_version` table if missing — a promotion script can
287
306
  * call `baseline()` against a fresh tracker with no separate DDL step.
288
- * - Acquires the same per-namespace advisory lock as `run_migrations` (with
289
- * the same try/catch fallback for environments lacking `pg_advisory_lock`).
307
+ * - Takes the same per-namespace advisory lock as `run_migrations`, in the
308
+ * transaction that writes the rows.
290
309
  * - Refuses if any tracker rows already exist *for this namespace* — lets
291
310
  * multi-call baseline scripts resume after partial failure (completed
292
311
  * namespaces guard themselves while remaining ones still run).
293
312
  * - Verifies the supplied names are a strict prefix of the namespace's
294
313
  * current migrations array — a name not in the array, or out of order,
295
314
  * errors before any INSERT.
296
- * - Writes sequences `0..N-1` in one transaction.
315
+ * - Writes sequences `0..N-1` in that transaction.
297
316
  *
298
317
  * @param db - the database instance
299
318
  * @param ns - the namespace whose migrations are being baselined
@@ -306,7 +325,7 @@ export const run_migrations = async (db, namespaces) => {
306
325
  */
307
326
  export const baseline = async (db, ns, names) => {
308
327
  assert_migration_namespaces([ns]);
309
- await db.query(SCHEMA_VERSION_DDL);
328
+ await ensure_schema_version_table(db, undefined);
310
329
  const code_names = ns.migrations.map((m) => m.name);
311
330
  // Validate every supplied name exists in code (catches drift between cutover
312
331
  // script and deployed build before any INSERT).
@@ -326,19 +345,18 @@ export const baseline = async (db, ns, names) => {
326
345
  `the code's array order.`, { namespace: ns.namespace, at_index: i });
327
346
  }
328
347
  }
329
- await with_namespace_lock(db, ns.namespace, async () => {
330
- const existing = await db.query('SELECT name FROM schema_version WHERE namespace = $1 LIMIT 1', [ns.namespace]);
348
+ await db.transaction(async (tx) => {
349
+ await lock_namespace(tx, ns.namespace);
350
+ const existing = await tx.query('SELECT name FROM schema_version WHERE namespace = $1 LIMIT 1', [ns.namespace]);
331
351
  if (existing.length > 0) {
332
352
  throw new MigrationError('baseline-namespace-already-populated', `baseline: namespace "${ns.namespace}" already has tracker rows. ` +
333
353
  `Hint: per-namespace guard for partial-failure resume — completed namespaces ` +
334
354
  `self-skip; if you need to re-baseline, manually ` +
335
355
  `\`DELETE FROM schema_version WHERE namespace = '${ns.namespace}'\` first.`, { namespace: ns.namespace });
336
356
  }
337
- await db.transaction(async (tx) => {
338
- for (let i = 0; i < names.length; i++) {
339
- await tx.query(`INSERT INTO schema_version (namespace, name, sequence)
340
- VALUES ($1, $2, $3)`, [ns.namespace, names[i], i]);
341
- }
342
- });
357
+ for (let i = 0; i < names.length; i++) {
358
+ await tx.query(`INSERT INTO schema_version (namespace, name, sequence)
359
+ VALUES ($1, $2, $3)`, [ns.namespace, names[i], i]);
360
+ }
343
361
  });
344
362
  };
@@ -1 +1 @@
1
- {"version":3,"file":"app_backend.d.ts","sourceRoot":"","sources":["../../src/lib/server/app_backend.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AAEjD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAwB,KAAK,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACnF,OAAO,EAA0B,KAAK,cAAc,EAAE,MAAM,iCAAiC,CAAC;AAE9F,OAAO,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAC5D,OAAO,EAAkB,KAAK,kBAAkB,EAAE,KAAK,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAMjG;;;;;GAKG;AACH,MAAM,WAAW,UAAU;IAC1B,IAAI,EAAE,OAAO,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,oIAAoI;IACpI,QAAQ,CAAC,iBAAiB,EAAE,aAAa,CAAC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,EAAE,cAAc,CAAC;IACnC;;;;;;;;;;;;;OAaG;IACH,oBAAoB,CAAC,EAAE,aAAa,CAAC,kBAAkB,CAAC,CAAC;IACzD;;;;;;;;OAQG;IACH,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAChC;;;;;;;OAOG;IACH,4BAA4B,CAAC,EAAE,MAAM,CAAC;CACtC;AAED;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,kBAAkB,GAAU,SAAS,uBAAuB,KAAG,OAAO,CAAC,UAAU,CA0D7F,CAAC"}
@@ -63,10 +63,7 @@ export const create_app_backend = async (options) => {
63
63
  // on the success path. Cleanup errors are logged and swallowed so the
64
64
  // caller sees the original failure, not a teardown-shaped one.
65
65
  try {
66
- const migration_results = await run_migrations(db, [
67
- AUTH_MIGRATION_NS,
68
- ...(options.migration_namespaces ?? [])
69
- ]);
66
+ const migration_results = await run_migrations(db, [AUTH_MIGRATION_NS, ...(options.migration_namespaces ?? [])], { log });
70
67
  const audit = audit_factory({
71
68
  db: db.with_checkout_timeout(options.db_audit_checkout_timeout_ms ?? DEFAULT_DB_AUDIT_CHECKOUT_TIMEOUT_MS),
72
69
  log