@fuzdev/fuz_app 0.185.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.
- package/dist/actions/CLAUDE.md +14 -2
- package/dist/actions/perform_action.d.ts.map +1 -1
- package/dist/actions/perform_action.js +36 -5
- package/dist/actions/ws_socket_session.d.ts.map +1 -1
- package/dist/actions/ws_socket_session.js +3 -1
- package/dist/auth/CLAUDE.md +17 -6
- package/dist/auth/account_queries.d.ts +60 -44
- package/dist/auth/account_queries.d.ts.map +1 -1
- package/dist/auth/account_queries.js +64 -54
- package/dist/auth/account_routes.d.ts.map +1 -1
- package/dist/auth/account_routes.js +59 -25
- package/dist/auth/admin_actions.d.ts.map +1 -1
- package/dist/auth/admin_actions.js +26 -9
- package/dist/auth/audit_emitter.js +2 -2
- package/dist/auth/audit_log_queries.d.ts +11 -3
- package/dist/auth/audit_log_queries.d.ts.map +1 -1
- package/dist/auth/audit_log_queries.js +4 -4
- package/dist/auth/audit_log_schema.js +1 -1
- package/dist/auth/auth_ddl.d.ts.map +1 -1
- package/dist/auth/auth_ddl.js +4 -0
- package/dist/auth/cleanup.js +1 -1
- package/dist/auth/migrations.d.ts.map +1 -1
- package/dist/auth/migrations.js +35 -0
- package/dist/auth/role_grant_offer_action_specs.d.ts +1 -1
- package/dist/auth/role_grant_offer_action_specs.d.ts.map +1 -1
- package/dist/auth/role_grant_offer_action_specs.js +2 -1
- package/dist/auth/role_grant_offer_actions.d.ts +12 -0
- package/dist/auth/role_grant_offer_actions.d.ts.map +1 -1
- package/dist/auth/role_grant_offer_actions.js +48 -20
- package/dist/auth/role_grant_offer_queries.d.ts +28 -15
- package/dist/auth/role_grant_offer_queries.d.ts.map +1 -1
- package/dist/auth/role_grant_offer_queries.js +35 -16
- package/dist/auth/role_grant_queries.d.ts +118 -20
- package/dist/auth/role_grant_queries.d.ts.map +1 -1
- package/dist/auth/role_grant_queries.js +145 -24
- package/dist/auth/self_service_role_actions.d.ts.map +1 -1
- package/dist/auth/self_service_role_actions.js +15 -0
- package/dist/auth/session_queries.d.ts +24 -0
- package/dist/auth/session_queries.d.ts.map +1 -1
- package/dist/auth/session_queries.js +28 -0
- package/dist/db/CLAUDE.md +3 -3
- package/dist/db/cell_ddl.d.ts +8 -3
- package/dist/db/cell_ddl.d.ts.map +1 -1
- package/dist/db/cell_ddl.js +10 -5
- package/dist/db/migrate.d.ts +46 -22
- package/dist/db/migrate.d.ts.map +1 -1
- package/dist/db/migrate.js +103 -85
- package/dist/http/body_limit.d.ts +4 -4
- package/dist/http/body_limit.js +4 -4
- package/dist/server/app_backend.d.ts.map +1 -1
- package/dist/server/app_backend.js +1 -4
- package/dist/testing/CLAUDE.md +161 -27
- package/dist/testing/app_server.d.ts +5 -7
- package/dist/testing/app_server.d.ts.map +1 -1
- package/dist/testing/app_server.js +5 -7
- package/dist/testing/auth_apps.d.ts +3 -1
- package/dist/testing/auth_apps.d.ts.map +1 -1
- package/dist/testing/auth_apps.js +22 -5
- package/dist/testing/cross_backend/backend_both_live.d.ts +47 -0
- package/dist/testing/cross_backend/backend_both_live.d.ts.map +1 -0
- package/dist/testing/cross_backend/backend_both_live.js +566 -0
- package/dist/testing/cross_backend/backend_config.d.ts +8 -0
- package/dist/testing/cross_backend/backend_config.d.ts.map +1 -1
- package/dist/testing/cross_backend/backend_log.d.ts +71 -0
- package/dist/testing/cross_backend/backend_log.d.ts.map +1 -0
- package/dist/testing/cross_backend/backend_log.js +134 -0
- package/dist/testing/cross_backend/backend_swap.d.ts +41 -0
- package/dist/testing/cross_backend/backend_swap.d.ts.map +1 -0
- package/dist/testing/cross_backend/backend_swap.js +267 -0
- package/dist/testing/cross_backend/body_size_smuggling.js +3 -3
- package/dist/testing/cross_backend/bootstrap_backend.js +4 -4
- package/dist/testing/cross_backend/conformance_case.d.ts +11 -6
- package/dist/testing/cross_backend/conformance_case.d.ts.map +1 -1
- package/dist/testing/cross_backend/conformance_case.js +4 -0
- package/dist/testing/cross_backend/conformance_principal.d.ts +2 -2
- package/dist/testing/cross_backend/conformance_principal.d.ts.map +1 -1
- package/dist/testing/cross_backend/conformance_principal.js +27 -10
- package/dist/testing/cross_backend/default_backend_configs.d.ts +38 -5
- package/dist/testing/cross_backend/default_backend_configs.d.ts.map +1 -1
- package/dist/testing/cross_backend/default_backend_configs.js +50 -15
- package/dist/testing/cross_backend/in_process_setup.d.ts.map +1 -1
- package/dist/testing/cross_backend/in_process_setup.js +52 -30
- package/dist/testing/cross_backend/revocation_drivers.d.ts +14 -2
- package/dist/testing/cross_backend/revocation_drivers.d.ts.map +1 -1
- package/dist/testing/cross_backend/revocation_drivers.js +36 -8
- package/dist/testing/cross_backend/rust_spine_stub_backend_config.d.ts +7 -0
- package/dist/testing/cross_backend/rust_spine_stub_backend_config.d.ts.map +1 -1
- package/dist/testing/cross_backend/rust_spine_stub_backend_config.js +4 -2
- package/dist/testing/cross_backend/setup.d.ts +44 -43
- package/dist/testing/cross_backend/setup.d.ts.map +1 -1
- package/dist/testing/cross_backend/setup.js +64 -62
- package/dist/testing/cross_backend/shared_database_backends.d.ts +175 -0
- package/dist/testing/cross_backend/shared_database_backends.d.ts.map +1 -0
- package/dist/testing/cross_backend/shared_database_backends.js +298 -0
- package/dist/testing/cross_backend/spawn_backend.d.ts +7 -3
- package/dist/testing/cross_backend/spawn_backend.d.ts.map +1 -1
- package/dist/testing/cross_backend/spawn_backend.js +86 -7
- package/dist/testing/cross_backend/spine_suites.d.ts +1 -1
- package/dist/testing/cross_backend/spine_suites.d.ts.map +1 -1
- package/dist/testing/cross_backend/spine_suites.js +5 -2
- package/dist/testing/cross_backend/sse_round_trip.d.ts.map +1 -1
- package/dist/testing/cross_backend/sse_round_trip.js +4 -2
- package/dist/testing/cross_backend/testing_reset_actions.d.ts +7 -6
- package/dist/testing/cross_backend/testing_reset_actions.d.ts.map +1 -1
- package/dist/testing/cross_backend/testing_reset_actions.js +10 -8
- package/dist/testing/cross_backend/testing_server_bun.d.ts.map +1 -1
- package/dist/testing/cross_backend/testing_server_bun.js +7 -31
- package/dist/testing/cross_backend/ws_round_trip.d.ts.map +1 -1
- package/dist/testing/cross_backend/ws_round_trip.js +4 -3
- package/dist/testing/data_exposure.d.ts +4 -1
- package/dist/testing/data_exposure.d.ts.map +1 -1
- package/dist/testing/data_exposure.js +93 -25
- package/dist/testing/db_invariants.d.ts +5 -5
- package/dist/testing/db_invariants.js +5 -5
- package/dist/testing/logged_errors.d.ts +61 -0
- package/dist/testing/logged_errors.d.ts.map +1 -0
- package/dist/testing/logged_errors.js +285 -0
- package/dist/testing/middleware_gate_probes.d.ts.map +1 -1
- package/dist/testing/middleware_gate_probes.js +15 -8
- package/dist/testing/rpc_attack_surface.d.ts.map +1 -1
- package/dist/testing/rpc_attack_surface.js +41 -16
- package/dist/testing/stubs.d.ts +5 -1
- package/dist/testing/stubs.d.ts.map +1 -1
- package/dist/testing/stubs.js +6 -3
- package/dist/testing/test_app.d.ts +40 -11
- package/dist/testing/test_app.d.ts.map +1 -1
- package/dist/testing/test_app.js +66 -9
- package/dist/testing/transports/fetch_transport.d.ts +10 -0
- package/dist/testing/transports/fetch_transport.d.ts.map +1 -1
- package/dist/testing/transports/fetch_transport.js +13 -0
- package/package.json +5 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"self_service_role_actions.d.ts","sourceRoot":"","sources":["../../src/lib/auth/self_service_role_actions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAIH,OAAO,EAAuC,KAAK,SAAS,EAAE,MAAM,4BAA4B,CAAC;AAGjG,OAAO,EAKN,KAAK,YAAY,EACjB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"self_service_role_actions.d.ts","sourceRoot":"","sources":["../../src/lib/auth/self_service_role_actions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAIH,OAAO,EAAuC,KAAK,SAAS,EAAE,MAAM,4BAA4B,CAAC;AAGjG,OAAO,EAKN,KAAK,YAAY,EACjB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,WAAW,CAAC;AAmB3D;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,oCAAoC,GAAI,OAAO,QAAQ,CAAC,MAAM,CAAC,KAAG,IAW9E,CAAC;AAEF,sDAAsD;AACtD,MAAM,WAAW,6BAA6B;IAC7C;;;;;;;;;;;OAWG;IACH,cAAc,CAAC,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;IACvC;;;;OAIG;IACH,KAAK,CAAC,EAAE,YAAY,CAAC;CACrB;AAED;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,gCAAgC,GAC5C,MAAM,yBAAyB,EAC/B,UAAS,6BAAkC,KACzC,KAAK,CAAC,SAAS,CAuKjB,CAAC"}
|
|
@@ -43,6 +43,8 @@ import { jsonrpc_errors } from '../http/jsonrpc_errors.js';
|
|
|
43
43
|
import { builtin_role_specs, is_builtin_role, list_roles_with_grant_path, RoleName } from './role_schema.js';
|
|
44
44
|
import { GRANT_PATH_SELF_SERVICE } from './grant_path_schema.js';
|
|
45
45
|
import { query_create_role_grant } from './role_grant_queries.js';
|
|
46
|
+
import { query_account_lock_live } from './account_queries.js';
|
|
47
|
+
import { ERROR_AUTHENTICATION_REQUIRED } from '../http/error_schemas.js';
|
|
46
48
|
import { query_revoke_role_grant } from './role_grant_offer_queries.js';
|
|
47
49
|
import { emit_supersede_audits } from './role_grant_offer_actions.js';
|
|
48
50
|
import { is_role_grant_active } from './account_schema.js';
|
|
@@ -106,6 +108,19 @@ export const create_self_service_role_actions = (deps, options = {}) => {
|
|
|
106
108
|
const handler = async (input, ctx) => {
|
|
107
109
|
const auth = ctx.auth;
|
|
108
110
|
reject_if_ineligible(input.role);
|
|
111
|
+
// The credential was resolved before the transaction, so a delete or a
|
|
112
|
+
// purge of the account can commit in between: a toggle after it would
|
|
113
|
+
// write a grant (or revoke one) on the tombstone, back on an undelete,
|
|
114
|
+
// or meet a purged actor's foreign key. The account row first — the
|
|
115
|
+
// class order's first row, which a delete's tombstone and a purge's
|
|
116
|
+
// `DELETE` write — refuses it as the dead credential it now is, or
|
|
117
|
+
// makes a removal still to commit wait for the toggle. Both branches,
|
|
118
|
+
// before the no-op answers: a removed account is told it is removed.
|
|
119
|
+
if (!(await query_account_lock_live(ctx, auth.account.id))) {
|
|
120
|
+
throw jsonrpc_errors.unauthenticated('unauthenticated', {
|
|
121
|
+
reason: ERROR_AUTHENTICATION_REQUIRED
|
|
122
|
+
});
|
|
123
|
+
}
|
|
109
124
|
if (input.enabled) {
|
|
110
125
|
// Pre-check for idempotent re-grant. `query_create_role_grant` is itself
|
|
111
126
|
// idempotent (returns the existing role_grant instead of inserting), but
|
|
@@ -101,6 +101,30 @@ export declare const query_session_get_valid: (deps: QueryDeps, token_hash: stri
|
|
|
101
101
|
* @returns `true` when the session exists, belongs to the account, and has not expired
|
|
102
102
|
*/
|
|
103
103
|
export declare const query_session_is_live: (deps: QueryDeps, id: string, account_id: string) => Promise<boolean>;
|
|
104
|
+
/**
|
|
105
|
+
* The password hash of the account a live session belongs to — the hash a
|
|
106
|
+
* password change verifies `current_password` against.
|
|
107
|
+
*
|
|
108
|
+
* The change's credential was resolved before this read, and its account row
|
|
109
|
+
* with it in a separate statement, so a change, a delete or a purge can commit
|
|
110
|
+
* between the two reads or after them. Each revokes the account's sessions in
|
|
111
|
+
* the transaction of its write, so while the session lives, the stored hash is
|
|
112
|
+
* the one the session was resolved under; once it is gone, the credential died
|
|
113
|
+
* mid-request and there is no hash to verify. Bound to the session in one
|
|
114
|
+
* statement, the read answers one or the other, never a later change's hash
|
|
115
|
+
* beside a dead session. The read is no lock: a session that dies after it is
|
|
116
|
+
* caught by the change's transaction, which takes the account row and then
|
|
117
|
+
* reads the session again (`query_session_is_live`). No tombstone filter: a
|
|
118
|
+
* delete revokes the sessions, and the change's transaction refuses a
|
|
119
|
+
* tombstone itself. Twin of the Rust
|
|
120
|
+
* `fuz_auth::query_session_live_password_hash`.
|
|
121
|
+
*
|
|
122
|
+
* @param deps - query dependencies
|
|
123
|
+
* @param id - the session's id, its token hash
|
|
124
|
+
* @param account_id - the account the session must belong to
|
|
125
|
+
* @returns the account's password hash, or `undefined` when the session is no longer live
|
|
126
|
+
*/
|
|
127
|
+
export declare const query_session_live_password_hash: (deps: QueryDeps, id: string, account_id: string) => Promise<string | undefined>;
|
|
104
128
|
/**
|
|
105
129
|
* Revoke (delete) a session by its token hash, with no account scoping.
|
|
106
130
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session_queries.d.ts","sourceRoot":"","sources":["../../src/lib/auth/session_queries.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAKH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAErD,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAMlE;;;;;;;GAOG;AACH,eAAO,MAAM,wBAAwB,QAAuC,CAAC;AAE7E;;;;;;;;GAQG;AACH,eAAO,MAAM,kBAAkB,GAAI,OAAO,MAAM,KAAG,SAElD,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,QAAO,MAEzC,CAAC;AAWF;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,oBAAoB,GAChC,MAAM,SAAS,EACf,YAAY,MAAM,EAClB,YAAY,MAAM,KAChB,OAAO,CAAC,IAAI,CAMd,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,oBAAoB,2DAKoB,CAAC;AAQtD;;;GAGG;AACH,MAAM,WAAW,gBAAiB,SAAQ,WAAW;IACpD;;;;OAIG;IACH,aAAa,EAAE,MAAM,CAAC;CACtB;AAED;;;;;;;;;;GAUG;AACH,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AEF;;;;;;;;;GASG;AACH,eAAO,MAAM,6BAA6B,GACzC,MAAM,SAAS,EACf,cAAW,KACT,OAAO,CAAC,KAAK,CAAC,WAAW,GAAG;IAAE,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC,CASnD,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,eAAO,MAAM,6BAA6B,GAAU,MAAM,SAAS,KAAG,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAO1F,CAAC;AAEF;;GAEG;AACH,MAAM,WAAW,0BAA0B;IAC1C,kDAAkD;IAClD,IAAI,EAAE,SAAS,CAAC;IAChB,2CAA2C;IAC3C,UAAU,EAAE,MAAM,CAAC;IACnB,4DAA4D;IAC5D,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC7B;AAED,6EAA6E;AAC7E,MAAM,WAAW,aAAa;IAC7B,4EAA4E;IAC5E,KAAK,EAAE,MAAM,CAAC;IACd,6EAA6E;IAC7E,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;CACvB;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,qBAAqB,GACjC,SAAS,0BAA0B,KACjC,OAAO,CAAC,aAAa,CAUvB,CAAC"}
|
|
@@ -123,6 +123,34 @@ export const query_session_is_live = async (deps, id, account_id) => {
|
|
|
123
123
|
const row = await deps.db.query_one(`SELECT id FROM auth_session WHERE id = $1 AND account_id = $2 AND expires_at > NOW()`, [id, account_id]);
|
|
124
124
|
return row !== undefined;
|
|
125
125
|
};
|
|
126
|
+
/**
|
|
127
|
+
* The password hash of the account a live session belongs to — the hash a
|
|
128
|
+
* password change verifies `current_password` against.
|
|
129
|
+
*
|
|
130
|
+
* The change's credential was resolved before this read, and its account row
|
|
131
|
+
* with it in a separate statement, so a change, a delete or a purge can commit
|
|
132
|
+
* between the two reads or after them. Each revokes the account's sessions in
|
|
133
|
+
* the transaction of its write, so while the session lives, the stored hash is
|
|
134
|
+
* the one the session was resolved under; once it is gone, the credential died
|
|
135
|
+
* mid-request and there is no hash to verify. Bound to the session in one
|
|
136
|
+
* statement, the read answers one or the other, never a later change's hash
|
|
137
|
+
* beside a dead session. The read is no lock: a session that dies after it is
|
|
138
|
+
* caught by the change's transaction, which takes the account row and then
|
|
139
|
+
* reads the session again (`query_session_is_live`). No tombstone filter: a
|
|
140
|
+
* delete revokes the sessions, and the change's transaction refuses a
|
|
141
|
+
* tombstone itself. Twin of the Rust
|
|
142
|
+
* `fuz_auth::query_session_live_password_hash`.
|
|
143
|
+
*
|
|
144
|
+
* @param deps - query dependencies
|
|
145
|
+
* @param id - the session's id, its token hash
|
|
146
|
+
* @param account_id - the account the session must belong to
|
|
147
|
+
* @returns the account's password hash, or `undefined` when the session is no longer live
|
|
148
|
+
*/
|
|
149
|
+
export const query_session_live_password_hash = async (deps, id, account_id) => {
|
|
150
|
+
const row = await deps.db.query_one(`SELECT a.password_hash FROM auth_session s JOIN account a ON a.id = s.account_id
|
|
151
|
+
WHERE s.id = $1 AND s.account_id = $2 AND s.expires_at > NOW()`, [id, account_id]);
|
|
152
|
+
return row?.password_hash;
|
|
153
|
+
};
|
|
126
154
|
/**
|
|
127
155
|
* Revoke (delete) a session by its token hash, with no account scoping.
|
|
128
156
|
*
|
package/dist/db/CLAUDE.md
CHANGED
|
@@ -120,9 +120,9 @@ The wire schemas + RPC handlers + authz predicates for this layer live in
|
|
|
120
120
|
authorizer's verdict + the `cell_moderate` verb, never in `data`.
|
|
121
121
|
`cell.policy jsonb` (nullable) is a root's contribution policy, opaque to the
|
|
122
122
|
spine — a table CHECK holds it to roots and to JSON objects (`policy @> '{}'`,
|
|
123
|
-
the object test pglet runs too). Nullable
|
|
124
|
-
|
|
125
|
-
/ `refs`; global partial-unique on `path`.
|
|
123
|
+
the object test pglet runs too). Nullable `created_by` FK to `actor` (NULL =
|
|
124
|
+
system origin); `updated_by` and `cell_grant.granted_by` are provenance stamps
|
|
125
|
+
with no FK. GIN on `data` / `refs`; global partial-unique on `path`.
|
|
126
126
|
- **`cell_queries.ts`** — `query_cell_create / get / get_by_path / update /
|
|
127
127
|
delete / set_moderation`, `_list_by_kind / _list_by_creator`, the
|
|
128
128
|
generic `query_cell_list` (filter + SQL-side visibility predicate mirroring
|
package/dist/db/cell_ddl.d.ts
CHANGED
|
@@ -19,10 +19,15 @@
|
|
|
19
19
|
* doesn't block reuse of its path. Path writes are admin-only at the
|
|
20
20
|
* action layer; user-namespaced paths are a future extension.
|
|
21
21
|
*
|
|
22
|
-
* **Ownership columns
|
|
23
|
-
* `
|
|
22
|
+
* **Ownership columns**: `created_by` is a nullable FK to `actor`
|
|
23
|
+
* (`ON DELETE SET NULL`): NULL = system origin (well-known cells,
|
|
24
|
+
* daemon/agent cells), and the owner the edit and manage predicates read.
|
|
24
25
|
* The non-admin authz path treats NULL `created_by` as admin-only via an
|
|
25
|
-
* explicit equality check (`auth/cell_authorize.ts`).
|
|
26
|
+
* explicit equality check (`auth/cell_authorize.ts`). `updated_by` and
|
|
27
|
+
* `cell_grant.granted_by` are provenance stamps with no FK, as `fuz_auth`'s
|
|
28
|
+
* are (`drop_provenance_stamp_foreign_keys`): nothing authorizes on them, so
|
|
29
|
+
* they keep the id they were written with after a purge, and neither a
|
|
30
|
+
* purge's cascade nor a stamp write waits on the other.
|
|
26
31
|
*
|
|
27
32
|
* **Timestamp naming** (`created_at`, `updated_at`) aligns with fuz_app's
|
|
28
33
|
* `_at`-everywhere convention used by `account`, `actor`, `audit_log`,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cell_ddl.d.ts","sourceRoot":"","sources":["../../src/lib/db/cell_ddl.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"cell_ddl.d.ts","sourceRoot":"","sources":["../../src/lib/db/cell_ddl.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AAGH,OAAO,EAGN,KAAK,kBAAkB,EACvB,MAAM,cAAc,CAAC;AAuOtB,6EAA6E;AAC7E,eAAO,MAAM,gBAAgB,4DAA6D,CAAC;AA+B3F;;;GAGG;AACH,eAAO,MAAM,iBAAiB,EAAE,kBAG/B,CAAC"}
|
package/dist/db/cell_ddl.js
CHANGED
|
@@ -19,10 +19,15 @@
|
|
|
19
19
|
* doesn't block reuse of its path. Path writes are admin-only at the
|
|
20
20
|
* action layer; user-namespaced paths are a future extension.
|
|
21
21
|
*
|
|
22
|
-
* **Ownership columns
|
|
23
|
-
* `
|
|
22
|
+
* **Ownership columns**: `created_by` is a nullable FK to `actor`
|
|
23
|
+
* (`ON DELETE SET NULL`): NULL = system origin (well-known cells,
|
|
24
|
+
* daemon/agent cells), and the owner the edit and manage predicates read.
|
|
24
25
|
* The non-admin authz path treats NULL `created_by` as admin-only via an
|
|
25
|
-
* explicit equality check (`auth/cell_authorize.ts`).
|
|
26
|
+
* explicit equality check (`auth/cell_authorize.ts`). `updated_by` and
|
|
27
|
+
* `cell_grant.granted_by` are provenance stamps with no FK, as `fuz_auth`'s
|
|
28
|
+
* are (`drop_provenance_stamp_foreign_keys`): nothing authorizes on them, so
|
|
29
|
+
* they keep the id they were written with after a purge, and neither a
|
|
30
|
+
* purge's cascade nor a stamp write waits on the other.
|
|
26
31
|
*
|
|
27
32
|
* **Timestamp naming** (`created_at`, `updated_at`) aligns with fuz_app's
|
|
28
33
|
* `_at`-everywhere convention used by `account`, `actor`, `audit_log`,
|
|
@@ -107,7 +112,7 @@ CREATE TABLE IF NOT EXISTS cell (
|
|
|
107
112
|
updated_at TIMESTAMPTZ,
|
|
108
113
|
deleted_at TIMESTAMPTZ,
|
|
109
114
|
created_by UUID REFERENCES actor(id) ON DELETE SET NULL,
|
|
110
|
-
updated_by UUID
|
|
115
|
+
updated_by UUID,
|
|
111
116
|
CHECK (policy IS NULL OR (parent_id IS NULL AND policy @> '{}'::jsonb))
|
|
112
117
|
)`;
|
|
113
118
|
/**
|
|
@@ -166,7 +171,7 @@ CREATE TABLE IF NOT EXISTS cell_grant (
|
|
|
166
171
|
actor_id UUID REFERENCES actor(id) ON DELETE CASCADE,
|
|
167
172
|
role TEXT,
|
|
168
173
|
scope_id UUID,
|
|
169
|
-
granted_by UUID
|
|
174
|
+
granted_by UUID,
|
|
170
175
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
171
176
|
CHECK (
|
|
172
177
|
(actor_id IS NOT NULL AND role IS NULL AND scope_id IS NULL) OR
|
package/dist/db/migrate.d.ts
CHANGED
|
@@ -42,18 +42,25 @@
|
|
|
42
42
|
*
|
|
43
43
|
* **Forward-only**: No down-migrations. Schema changes are additive.
|
|
44
44
|
*
|
|
45
|
-
* **Advisory locking**:
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
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
|
|
149
|
-
* by `sequence`, length-checks
|
|
150
|
-
* prefix-verifies, then runs the
|
|
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
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
*
|
|
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
|
|
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
|
-
* -
|
|
194
|
-
*
|
|
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
|
|
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
|
package/dist/db/migrate.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"migrate.d.ts","sourceRoot":"","sources":["../../src/lib/db/migrate.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"migrate.d.ts","sourceRoot":"","sources":["../../src/lib/db/migrate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6DG;AAGH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AAEtD,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAGlC;;;;GAIG;AACH,MAAM,WAAW,SAAS;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9B;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,kBAAkB;IAClC,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,KAAK,CAAC,SAAS,CAAC,CAAC;CAC7B;AAED,2DAA2D;AAC3D,MAAM,WAAW,eAAe;IAC/B,SA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,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"}
|
package/dist/db/migrate.js
CHANGED
|
@@ -42,19 +42,26 @@
|
|
|
42
42
|
*
|
|
43
43
|
* **Forward-only**: No down-migrations. Schema changes are additive.
|
|
44
44
|
*
|
|
45
|
-
* **Advisory locking**:
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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 (
|
|
149
|
-
hash = ((hash << 5) + hash +
|
|
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
|
-
*
|
|
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
|
|
161
|
-
|
|
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
|
|
184
|
-
* by `sequence`, length-checks
|
|
185
|
-
* prefix-verifies, then runs the
|
|
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
|
|
199
|
-
*
|
|
200
|
-
*
|
|
201
|
-
*
|
|
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
|
|
234
|
+
await ensure_schema_version_table(db, options?.log);
|
|
217
235
|
const results = [];
|
|
218
236
|
for (const { namespace, migrations } of namespaces) {
|
|
219
|
-
await
|
|
220
|
-
|
|
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
|
|
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
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
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
|
-
|
|
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
|
-
* -
|
|
289
|
-
*
|
|
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
|
|
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
|
|
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
|
|
330
|
-
|
|
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
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
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
|
};
|
|
@@ -50,10 +50,10 @@
|
|
|
50
50
|
* holds the connection for a drain window and then closes or, the body
|
|
51
51
|
* untouched and arriving within the window, reads it off and serves the
|
|
52
52
|
* next request.
|
|
53
|
-
* - **Bun** closes at once, the body unread, honouring the header. Bun 1.4.
|
|
54
|
-
* is the tested floor
|
|
55
|
-
*
|
|
56
|
-
* later.
|
|
53
|
+
* - **Bun** closes at once, the body unread, honouring the header. Bun 1.4.4
|
|
54
|
+
* is the tested floor (CI's pin); the close needs 1.4.3 — an older Bun kept
|
|
55
|
+
* the connection after the `413` and served the next request (correctly
|
|
56
|
+
* framed) — so run Bun at the floor or later.
|
|
57
57
|
*
|
|
58
58
|
* @module
|
|
59
59
|
*/
|