@aria-framework/db-worker 0.20.0 → 0.20.1

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 (2) hide show
  1. package/database.js +41 -6
  2. package/package.json +1 -1
package/database.js CHANGED
@@ -579,14 +579,49 @@ async function ensureMigrationsTable(given) {
579
579
  // bypasses ENABLE ROW LEVEL SECURITY.
580
580
  const d = given || driver();
581
581
  const t = d.dialect;
582
+
583
+ // ── EXISTS IS CHECKED BEFORE CREATE, and on Postgres that is not an optimisation ────────────
584
+ //
585
+ // `CREATE TABLE IF NOT EXISTS` does NOT make this privilege-free: Postgres resolves the schema
586
+ // and checks CREATE on it BEFORE it checks whether the table is already there. So an application
587
+ // role that correctly has no CREATE — the whole point of a separate owner role — died right
588
+ // here, at initDriver(), with `permission denied for schema public`, before it could run or even
589
+ // read a single migration. Doing the ownership properly made the app unbootable, and the error
590
+ // named a schema rather than the thing that was actually wrong.
591
+ //
592
+ // Reading the catalogue needs no privilege, so the ordinary case for an app role — the ledger
593
+ // already exists, created by whoever ran the migrations — now costs one SELECT and no CREATE.
594
+ if (d.kind === 'postgres') {
595
+ const [row] = await d.all("SELECT to_regclass('migrations') IS NOT NULL AS present");
596
+ if (row && row.present) return;
597
+ }
598
+
582
599
  // The LEDGER is the one table the framework owns, so it is the one DDL that must be
583
600
  // dialect-aware. Everything else is the app's.
584
- await d.exec(`
585
- CREATE TABLE IF NOT EXISTS migrations (
586
- id ${t.autoIncrementPk()},
587
- filename TEXT NOT NULL UNIQUE,
588
- applied_at TEXT NOT NULL DEFAULT (${t.now()})
589
- )`);
601
+ try {
602
+ await d.exec(`
603
+ CREATE TABLE IF NOT EXISTS migrations (
604
+ id ${t.autoIncrementPk()},
605
+ filename TEXT NOT NULL UNIQUE,
606
+ applied_at TEXT NOT NULL DEFAULT (${t.now()})
607
+ )`);
608
+ } catch (e) {
609
+ // The ledger genuinely is absent and this role cannot create it. That is a real failure, but
610
+ // `permission denied for schema public` sends the reader to the grants when the answer is
611
+ // almost always that migrations have simply never been run against this database as the role
612
+ // that is allowed to.
613
+ if (d.kind === 'postgres' && /permission denied for schema/i.test(e.message || '')) {
614
+ throw new Error(
615
+ 'db-worker: the migrations ledger does not exist and this role may not create it — '
616
+ + `${e.message}. This is the expected shape when the application connects as a role that `
617
+ + 'owns nothing (as it should). Run the migrations first as the owner/migration role: pass '
618
+ + 'runMigrations(dir, { connectionString }) with MIGRATION_DATABASE_URL, which creates the '
619
+ + 'ledger on that connection. Granting the application CREATE on the schema would also '
620
+ + 'work and is the wrong fix — it would own every table it then creates, and an owner '
621
+ + 'bypasses ENABLE ROW LEVEL SECURITY.');
622
+ }
623
+ throw e;
624
+ }
590
625
  }
591
626
 
592
627
  async function initDriver(opts = {}) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aria-framework/db-worker",
3
- "version": "0.20.0",
3
+ "version": "0.20.1",
4
4
  "description": "Aria App Framework \u2014 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,