@aria-framework/db-worker 0.27.0 → 0.27.2

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/README.md CHANGED
@@ -437,6 +437,23 @@ Against 0.25.0's code, 6 of its 10 checks fail.
437
437
 
438
438
  ## Changelog
439
439
 
440
+ - **0.27.2** — a FAILED MIGRATION REJECTS `runMigrations` again on the Postgres migration-connection path.
441
+ Its `finally` did `return` after logging "Skipping the BYPASSRLS proof", and a `return` in a finally discards the
442
+ exception in flight: `runMigrations` resolved, the app booted on a half-migrated schema, and the next migrations
443
+ never ran (seen: a renumbered migration failing on every boot; the symptom was a 500 three migrations later). Now
444
+ the migration's own error propagates and the migration connection is still closed. `test/migrationFailureSurfaces.js`
445
+ (fake drivers, no server) fails against 0.27.1.
446
+ - **0.27.1** — the UTC pin is WAITED FOR. 0.27.0 fired `SET TIME ZONE 'UTC'` from the pool's
447
+ `connect` event without awaiting it, so the first query on every new connection was issued
448
+ while the SET was still running: pg 8 prints "Calling client.query() when the client is already
449
+ executing a query is deprecated" (seen on an app at startup), and pg 9 makes it a failed query.
450
+ The SET now runs in pg-pool's awaited `onConnect` hook — installed on the pool's options, so
451
+ `pinPoolUtc` covers a pool the app built too; an `onConnect` already there still runs, after the
452
+ pin. A pg-pool without the hook falls back to the old `connect` path. **Behaviour change:** a SET
453
+ that fails now fails that checkout (the query rejects, the client is discarded, the error is
454
+ logged) instead of handing out an unpinned session. `test/pinAwaited.pg.js` (N concurrent
455
+ queries on N fresh connections: no overlap, every connection UTC) and `test/pinPoolUtc.js`
456
+ (wiring) — against 0.27.0, 4 of 15 and 5 of 6 fail.
440
457
  - **0.27.0** — "Time is UTC" (repo README): a Postgres session and a Date on the wire no longer
441
458
  depend on the server's, the role's, the connection string's or the process's timezone.
442
459
  - **Every connection the driver opens runs `SET TIME ZONE 'UTC'` first** — the app pool and the
package/database.js CHANGED
@@ -456,21 +456,26 @@ function runMigrations(migrationsDir, opts = {}) {
456
456
  // SQL that broke, at the one moment a half-applied chain makes the real cause matter most.
457
457
  // So the proof is skipped when we are already unwinding an error, and any failure of the
458
458
  // proof ITSELF is reported rather than thrown when there is a prior error to preserve.
459
+ //
460
+ // NO `return` HERE (0.27.2). This block is a `finally`, and a `return` in a finally DISCARDS the exception
461
+ // in flight: runMigrations resolved as if it had succeeded, the boot carried on with a half-migrated schema,
462
+ // and the only trace was the warning below. Found when a renumbered migration failed on every boot of a test
463
+ // database and the symptom surfaced three migrations later as a 500 on an unrelated insert.
459
464
  if (migrationError) {
460
465
  _logger.warn('Skipping the BYPASSRLS proof: a migration already failed, and its error is '
461
466
  + 'the one worth keeping. Re-run once that is fixed.');
462
- return;
463
- }
464
- const [who] = await d.all(
465
- 'SELECT current_user AS role, '
466
- + '(SELECT rolbypassrls FROM pg_roles WHERE rolname = current_user) AS bypassrls');
467
- if (who && who.bypassrls) {
468
- throw new Error(
469
- `runMigrations: after closing the migration connection the APPLICATION driver is still `
470
- + `connected as ${who.role}, which holds BYPASSRLS. Row-level security does not apply `
471
- + 'to it, so every partner boundary in this schema is inert. The application must '
472
- + 'connect as a role that owns nothing and has no BYPASSRLS (frozen spec §3); only the '
473
- + 'migration and backup roles may hold it.');
467
+ } else {
468
+ const [who] = await d.all(
469
+ 'SELECT current_user AS role, '
470
+ + '(SELECT rolbypassrls FROM pg_roles WHERE rolname = current_user) AS bypassrls');
471
+ if (who && who.bypassrls) {
472
+ throw new Error(
473
+ `runMigrations: after closing the migration connection the APPLICATION driver is still `
474
+ + `connected as ${who.role}, which holds BYPASSRLS. Row-level security does not apply `
475
+ + 'to it, so every partner boundary in this schema is inert. The application must '
476
+ + 'connect as a role that owns nothing and has no BYPASSRLS (frozen spec §3); only the '
477
+ + 'migration and backup roles may hold it.');
478
+ }
474
479
  }
475
480
  }
476
481
  }
package/driver/pgUtc.js CHANGED
@@ -21,10 +21,23 @@
21
21
  * SET issued after the session starts overrides the server default, the database default, the
22
22
  * role default and the startup packet alike.
23
23
  *
24
- * WHY IT IS IN TIME. pg-pool emits 'connect' synchronously, BEFORE handing the new client to
25
- * whoever asked for it, and a client runs its queries in the order they were queued. The SET is
26
- * therefore the first statement on every connection, ahead of the caller's first query.
27
- * test/timezone.pg.js proves it against a pool whose connection string asks for another zone.
24
+ * WHY IT IS IN TIME — AND WAITED FOR. pg-pool awaits its `onConnect(client)` option after a
25
+ * connection opens and BEFORE the client is handed to whoever asked for it, so the SET has
26
+ * COMPLETED before the caller's first query is issued. Until 0.27.1 the SET was fired from the
27
+ * 'connect' event and not awaited: still first in the client's queue, but the caller's query was
28
+ * then issued while it ran — pg 8 prints "Calling client.query() when the client is already
29
+ * executing a query is deprecated", and pg 9 removes that queue, which makes it a failed query
30
+ * on a fresh connection. The hook is installed on the pool's own options, so it covers a pool
31
+ * the CALLER built too; a hook already there still runs, after the pin.
32
+ * test/timezone.pg.js proves the zone against a pool whose connection string asks for another;
33
+ * test/pinAwaited.pg.js proves that no query overlaps the SET.
34
+ *
35
+ * A pg-pool too old to know `onConnect` ignores it, and the 'connect' listener below then sees
36
+ * a client the hook never pinned and issues the SET the old way: pinned, with the warning.
37
+ *
38
+ * A SET THAT FAILS FAILS THE CHECKOUT: pg-pool ends that client and the caller's query rejects
39
+ * with the error (logged here too). An unpinned session is exactly the silent failure this
40
+ * exists to prevent, so it is no longer handed out.
28
41
  *
29
42
  * LIMIT, stated: only connections opened AFTER the call are pinned. Call it on a pool before
30
43
  * its first query — which is what the driver and the session-store factory both do.
@@ -32,7 +45,9 @@
32
45
 
33
46
  'use strict';
34
47
 
48
+ const PIN_SQL = "SET TIME ZONE 'UTC'";
35
49
  const PINNED = new WeakSet();
50
+ const PINNED_CLIENTS = new WeakSet();
36
51
 
37
52
  /**
38
53
  * @param {object} pool a node-postgres Pool
@@ -43,12 +58,22 @@ function pinPoolUtc(pool, opts = {}) {
43
58
  if (!pool || typeof pool.on !== 'function') throw new Error('pinPoolUtc(pool): a pg Pool is required');
44
59
  if (PINNED.has(pool)) return pool;
45
60
  PINNED.add(pool);
61
+ const say = (err) => {
62
+ // Loud, because an unpinned session is exactly the silent failure this exists to prevent.
63
+ try { (opts.logger || console).error(`postgres: could not pin a session to UTC (${err.message})`); }
64
+ catch (_) { /* a logging failure must not become an unhandled rejection */ }
65
+ };
66
+ if (!pool.options) pool.options = {};
67
+ const prior = pool.options.onConnect;
68
+ pool.options.onConnect = async (client) => {
69
+ try { await client.query(PIN_SQL); } catch (err) { say(err); throw err; }
70
+ PINNED_CLIENTS.add(client);
71
+ if (typeof prior === 'function') await prior(client);
72
+ };
73
+ // Fallback for a pg-pool without the hook; a no-op for every client the hook pinned.
46
74
  pool.on('connect', (client) => {
47
- client.query("SET TIME ZONE 'UTC'").catch((err) => {
48
- // Loud, because an unpinned session is exactly the silent failure this exists to prevent.
49
- try { (opts.logger || console).error(`postgres: could not pin a session to UTC (${err.message})`); }
50
- catch (_) { /* a logging failure must not become an unhandled rejection */ }
51
- });
75
+ if (PINNED_CLIENTS.has(client)) return;
76
+ client.query(PIN_SQL).catch(say);
52
77
  });
53
78
  return pool;
54
79
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aria-framework/db-worker",
3
- "version": "0.27.0",
3
+ "version": "0.27.2",
4
4
  "description": "Aria App Framework — db-worker module. Async RPC bridge over synchronous better-sqlite3: a worker-thread dispatcher (runWorker), a main-thread client with per-call timeouts and crash handling (DbClient), and a SQLite layer with WAL pragmas + a numbered-migration runner (database).",
5
5
  "license": "UNLICENSED",
6
6
  "private": false,
@@ -40,7 +40,7 @@
40
40
  "pg": "^8.23.0"
41
41
  },
42
42
  "scripts": {
43
- "test": "node test/driverContract.js && node test/migrations.js && node test/migrations-notx.js && node test/migrationDialect.js && node test/regressions.js && node test/transactions.js && node test/tenantContext.js && node test/tenancyMigration.js && node test/tenancyAttribution.pglite.js && node test/tenancyNullable.pg.js && node test/tenancyPartnerFk.pg.js && node test/tenancyOnUpdate.pg.js && node test/dialectIntrospection.js && node test/tenantRls.pg.js && node test/nestedTransaction.pg.js && node test/clientError.pg.js && node test/migrationRole.pg.js && node test/smoke.js && node test/tier2.js && node test/timezone.pg.js",
43
+ "test": "node test/driverContract.js && node test/migrations.js && node test/migrations-notx.js && node test/migrationFailureSurfaces.js && node test/migrationDialect.js && node test/regressions.js && node test/transactions.js && node test/tenantContext.js && node test/tenancyMigration.js && node test/tenancyAttribution.pglite.js && node test/tenancyNullable.pg.js && node test/tenancyPartnerFk.pg.js && node test/tenancyOnUpdate.pg.js && node test/dialectIntrospection.js && node test/tenantRls.pg.js && node test/nestedTransaction.pg.js && node test/clientError.pg.js && node test/migrationRole.pg.js && node test/smoke.js && node test/tier2.js && node test/timezone.pg.js && node test/pinPoolUtc.js && node test/pinAwaited.pg.js",
44
44
  "prepublishOnly": "node ../../test/packaging.js db-worker"
45
45
  }
46
46
  }