@aria-framework/db-worker 0.20.5 → 0.21.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/package.json +2 -2
- package/tenancyMigration.js +37 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aria-framework/db-worker",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.0",
|
|
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,
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"pg": "^8.23.0"
|
|
40
40
|
},
|
|
41
41
|
"scripts": {
|
|
42
|
-
"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/dialectIntrospection.js && node test/tenantRls.pg.js && node test/nestedTransaction.pg.js && node test/migrationRole.pg.js && node test/smoke.js && node test/tier2.js",
|
|
42
|
+
"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/tenancyNullable.pg.js && node test/dialectIntrospection.js && node test/tenantRls.pg.js && node test/nestedTransaction.pg.js && node test/migrationRole.pg.js && node test/smoke.js && node test/tier2.js",
|
|
43
43
|
"prepublishOnly": "node ../../test/packaging.js db-worker"
|
|
44
44
|
}
|
|
45
45
|
}
|
package/tenancyMigration.js
CHANGED
|
@@ -81,14 +81,49 @@ function generateTenancyMigration(opts = {}) {
|
|
|
81
81
|
// as the OWNER role.
|
|
82
82
|
const sqlLiteral = (v) => `'${String(v).replace(/'/g, "''")}'`;
|
|
83
83
|
|
|
84
|
+
const { dialectFor } = require('./dialect');
|
|
85
|
+
const sqliteDialect = dialectFor('sqlite');
|
|
86
|
+
|
|
87
|
+
// ── WHICH TABLE HOLDS THE PRINCIPAL, AND WHAT THE SCOPE COLUMN IS CALLED ────────────────────
|
|
88
|
+
//
|
|
89
|
+
// This emitted `ALTER TABLE users ADD COLUMN scope_kind` unconditionally — a framework package
|
|
90
|
+
// hardcoding one app's table name. An app whose principal table is `staff`, `accounts` or
|
|
91
|
+
// `app_user` got a migration that failed at `relation "users" does not exist` AFTER
|
|
92
|
+
// CREATE TABLE partners and every partner_id column had already applied, leaving the chain
|
|
93
|
+
// half-way. Neither name appears in the documented manifest contract (scoped / WIDEN_UNIQUE /
|
|
94
|
+
// POLICY_EXPRESSION), so nothing told an author the dependency existed.
|
|
95
|
+
//
|
|
96
|
+
// Defaulted to the old values so existing callers are unchanged, and CHECKED against the schema
|
|
97
|
+
// being read: a name that is not there fails HERE, with the tables that do exist named, rather
|
|
98
|
+
// than in the middle of a migration.
|
|
99
|
+
const principalTable = opts.principalTable || 'users';
|
|
100
|
+
const scopeColumn = opts.scopeColumn || 'scope_kind';
|
|
101
|
+
|
|
84
102
|
const db = new Database(dbPath, { readonly: true });
|
|
85
103
|
try {
|
|
104
|
+
const present = db.prepare("SELECT name FROM sqlite_master WHERE type = 'table'").all()
|
|
105
|
+
.map((r) => r.name);
|
|
106
|
+
if (!present.includes(principalTable)) {
|
|
107
|
+
throw new Error(`generateTenancyMigration: no table named "${principalTable}" in this schema, `
|
|
108
|
+
+ `so the ${scopeColumn} column has nowhere to go. Pass { principalTable } naming the table `
|
|
109
|
+
+ `that holds your signed-in principals. Tables here: ${present.sort().join(', ')}`);
|
|
110
|
+
}
|
|
111
|
+
|
|
86
112
|
const scoped = manifest.scoped();
|
|
87
113
|
const scopedNames = new Set(scoped.map((s) => s.table));
|
|
88
114
|
const ordered = [...scoped].sort((a, b) => a.table.localeCompare(b.table));
|
|
89
115
|
|
|
90
116
|
/** Foreign keys from one tenant-scoped table to another. These become composite. */
|
|
91
|
-
|
|
117
|
+
// THE DIALECT'S OWN SQL, not a hand-rolled PRAGMA. dialect.foreignKeys() was added to this
|
|
118
|
+
// package for exactly this, with both engines aliased to the PRAGMA's own from/table/to column
|
|
119
|
+
// names — and this generator, added in the same release, inlined the PRAGMA anyway with
|
|
120
|
+
// hand-written quotes. Using it centralises the identifier quoting and means the one place
|
|
121
|
+
// that knows how to ask a schema about its foreign keys is the one place that does it.
|
|
122
|
+
//
|
|
123
|
+
// STILL SQLITE-ONLY, and honestly so: this reads a SQLite file by construction (dbPath +
|
|
124
|
+
// Database). Sharing the SQL does not make it engine-agnostic — that would need the caller to
|
|
125
|
+
// pass a driver instead of a file — it removes a second spelling of the same question.
|
|
126
|
+
const tenantFks = (table) => db.prepare(sqliteDialect.foreignKeys(table)).all()
|
|
92
127
|
.filter((f) => scopedNames.has(f.table))
|
|
93
128
|
.map((f) => ({ from: f.from, table: f.table, to: f.to || 'id', onDelete: f.on_delete }))
|
|
94
129
|
// PRAGMA lists newest first; sorting keeps a regenerated file diff-free.
|
|
@@ -143,7 +178,7 @@ function generateTenancyMigration(opts = {}) {
|
|
|
143
178
|
'-- user of the house partner" has to consult another table, which a CHECK cannot do on',
|
|
144
179
|
'-- either engine. It is enforced in the application and by a test; recording the limitation',
|
|
145
180
|
'-- here beats leaving somebody to assume the database is holding it.',
|
|
146
|
-
|
|
181
|
+
`ALTER TABLE ${principalTable} ADD COLUMN ${scopeColumn} TEXT NOT NULL DEFAULT 'partner';`
|
|
147
182
|
].join('\n');
|
|
148
183
|
}
|
|
149
184
|
|