@aria-framework/db-worker 0.11.0 → 0.13.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/README.md +26 -0
- package/database.js +46 -2
- package/driver/postgres.js +48 -16
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -205,8 +205,34 @@ holds it down.
|
|
|
205
205
|
On a serial driver (SQLite) there is no row-level security and nothing is set. The context is
|
|
206
206
|
still established so a model can read it via `currentTenant()`, and no transaction is forced.
|
|
207
207
|
|
|
208
|
+
## Migrations per engine (0.12.0)
|
|
209
|
+
|
|
210
|
+
A migration file may name the engine it is for. The runner picks; it never translates.
|
|
211
|
+
|
|
212
|
+
```
|
|
213
|
+
001_baseline.sql runs on sqlite
|
|
214
|
+
001_baseline.postgres.sql runs on postgres, and supersedes 001_baseline.sql there
|
|
215
|
+
002_records.sql runs on both — no sibling, so it is dialect-neutral
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
The suffix is the **driver kind**, spelled exactly (`sqlite`, `postgres`). There is no `pg`
|
|
219
|
+
alias on purpose: two names for one thing is how a migration ends up silently never running.
|
|
220
|
+
A suffix outside that set (`002_records.v2.sql`) is just part of the name and runs everywhere.
|
|
221
|
+
|
|
222
|
+
**Why the plain file keeps its name.** The obvious symmetrical scheme — rename the baseline to
|
|
223
|
+
`001_baseline.sqlite.sql` — cannot be done. The ledger is keyed by FILENAME, and every database
|
|
224
|
+
this framework has created has `001_baseline.sql` in its `migrations` table. Renaming it makes
|
|
225
|
+
the runner see an unapplied migration and re-run a baseline over live tables. The rule is
|
|
226
|
+
additive for that reason and no other, and `test/migrationDialect.js` proves that adding a
|
|
227
|
+
Postgres sibling does nothing to an already-migrated SQLite database.
|
|
228
|
+
|
|
229
|
+
This is selection, not a portable DDL layer. An app still writes its own schema per engine —
|
|
230
|
+
the framework guarantees only which files ran and the transaction around each.
|
|
231
|
+
|
|
208
232
|
## Changelog
|
|
209
233
|
|
|
234
|
+
- **0.12.0** — dialect-selected migration files (above).
|
|
235
|
+
|
|
210
236
|
- **0.11.0** — the tenant-context contract above: `runWithTenant()` / `currentTenant()`,
|
|
211
237
|
serialised across the worker boundary, applied as one `set_config` statement inside a
|
|
212
238
|
per-invoke transaction on pooled drivers. Fail-closed when a pooled driver gets no context.
|
package/database.js
CHANGED
|
@@ -222,6 +222,45 @@ function execUnwrappedSqlite(db, sql) {
|
|
|
222
222
|
}
|
|
223
223
|
}
|
|
224
224
|
|
|
225
|
+
/** Engines a migration file may name. A suffix outside this set is just part of the name. */
|
|
226
|
+
const MIGRATION_DIALECTS = new Set(['sqlite', 'postgres']);
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Choose the migration files that belong to THIS engine.
|
|
230
|
+
*
|
|
231
|
+
* 001_baseline.sql runs on sqlite
|
|
232
|
+
* 001_baseline.postgres.sql runs on postgres, and supersedes 001_baseline.sql there
|
|
233
|
+
* 002_records.sql runs on both — no sibling, so it is dialect-neutral
|
|
234
|
+
*
|
|
235
|
+
* The suffix is the DRIVER KIND, spelled exactly. One spelling, no alias table: two ways to
|
|
236
|
+
* name the same thing is precisely the sort of detail that rots into a file silently never
|
|
237
|
+
* running.
|
|
238
|
+
*
|
|
239
|
+
* WHY NOT SIMPLY RENAME the baseline to 001_baseline.sqlite.sql and be symmetrical: the ledger
|
|
240
|
+
* is keyed by FILENAME. Every database this framework has ever created has 001_baseline.sql in
|
|
241
|
+
* its migrations table. Renaming it makes the runner see an unapplied migration and re-run a
|
|
242
|
+
* baseline over live data. The rule is additive for that reason and no other.
|
|
243
|
+
*
|
|
244
|
+
* This is SELECTION, not translation. An app still writes its own DDL per engine — the header
|
|
245
|
+
* below is explicit that a portable DDL layer would be a leaky translation of the one thing
|
|
246
|
+
* engines differ on most. All this decides is which of the app's files to hand to which engine.
|
|
247
|
+
*/
|
|
248
|
+
function selectForDialect(files, kind) {
|
|
249
|
+
const specific = new Map(); // plain name -> the file that supersedes it here
|
|
250
|
+
const foreign = new Set(); // files that name a DIFFERENT engine
|
|
251
|
+
|
|
252
|
+
for (const f of files) {
|
|
253
|
+
const m = /^(.*)\.([A-Za-z0-9]+)\.sql$/.exec(f);
|
|
254
|
+
if (!m) continue;
|
|
255
|
+
const suffix = m[2].toLowerCase();
|
|
256
|
+
if (!MIGRATION_DIALECTS.has(suffix)) continue; // e.g. 002_records.v2.sql — just a name
|
|
257
|
+
if (suffix === kind) specific.set(m[1] + '.sql', f);
|
|
258
|
+
else foreign.add(f);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return files.filter((f) => !foreign.has(f) && !specific.has(f));
|
|
262
|
+
}
|
|
263
|
+
|
|
225
264
|
/**
|
|
226
265
|
* Apply pending numbered migrations, in filename order, each in its own transaction — unless
|
|
227
266
|
* the file opts out with `-- aria:no-transaction` (see the marker notes above).
|
|
@@ -251,7 +290,12 @@ function runMigrations(migrationsDir) {
|
|
|
251
290
|
_logger.warn('No migrations directory found');
|
|
252
291
|
return Promise.resolve({ applied: 0 });
|
|
253
292
|
}
|
|
254
|
-
const
|
|
293
|
+
const all = fs.readdirSync(migrationsDir).filter((f) => f.endsWith('.sql')).sort();
|
|
294
|
+
const files = selectForDialect(all, d.kind);
|
|
295
|
+
for (const skipped of all.filter((f) => !files.includes(f))) {
|
|
296
|
+
// Announced, because a migration that silently never runs is the worst kind of absent.
|
|
297
|
+
_logger.info(`Migration skipped (not for ${d.kind}): ${skipped}`);
|
|
298
|
+
}
|
|
255
299
|
|
|
256
300
|
// ── the synchronous path, unchanged in behaviour since 0.1.0 ──────────────────────────────
|
|
257
301
|
if (d.kind === 'sqlite') {
|
|
@@ -415,4 +459,4 @@ async function initDriver(opts = {}) {
|
|
|
415
459
|
return _driver;
|
|
416
460
|
}
|
|
417
461
|
|
|
418
|
-
module.exports = { init, initDriver, runMigrations, getDb, driver, close };
|
|
462
|
+
module.exports = { init, initDriver, runMigrations, getDb, driver, close, selectForDialect };
|
package/driver/postgres.js
CHANGED
|
@@ -165,24 +165,56 @@ function createPostgresDriver(opts = {}) {
|
|
|
165
165
|
*/
|
|
166
166
|
async transaction(fn) {
|
|
167
167
|
const client = await pool.connect();
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
168
|
+
const q = (sql, params) => client.query(sql, params);
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* ONE API PER NESTING LEVEL, each owning the queue for its own children.
|
|
172
|
+
*
|
|
173
|
+
* A shared depth counter is not enough, and the reason is that a savepoint stack is not a
|
|
174
|
+
* set: RELEASE frees every savepoint opened after the named one. Two siblings started
|
|
175
|
+
* together therefore destroy each other as soon as the first finishes — the second's
|
|
176
|
+
* RELEASE finds nothing, that error aborts the transaction, and the outer COMMIT silently
|
|
177
|
+
* becomes a ROLLBACK. The outer call then reports success having written nothing.
|
|
178
|
+
*
|
|
179
|
+
* So siblings take turns. Only one transaction may be innermost on a client at a time.
|
|
180
|
+
* (The SQLite driver has the same rule for the same reason; it reached it first because
|
|
181
|
+
* it also has to serialise TOP-level transactions, which a pool does not.)
|
|
182
|
+
*/
|
|
183
|
+
const levelApi = (depth) => {
|
|
184
|
+
let queue = Promise.resolve();
|
|
185
|
+
return apiOver(q, {
|
|
186
|
+
// Nested transaction() reuses the same CLIENT (a second pooled connection would
|
|
187
|
+
// deadlock against this one's locks) and brackets the body in a SAVEPOINT, so an
|
|
188
|
+
// inner failure rolls back only its own work rather than discarding the outer.
|
|
189
|
+
async transaction(inner) {
|
|
190
|
+
const run = () => runNested(inner, depth + 1);
|
|
191
|
+
// Chained on BOTH settle paths, so one failed child does not wedge its siblings.
|
|
192
|
+
const mine = queue.then(run, run);
|
|
193
|
+
queue = mine.then(() => {}, () => {});
|
|
194
|
+
return mine;
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
async function runNested(inner, depth) {
|
|
200
|
+
const sp = `aria_sp_${depth}`;
|
|
201
|
+
await q(`SAVEPOINT ${sp}`);
|
|
202
|
+
try {
|
|
203
|
+
const out = await inner(levelApi(depth));
|
|
204
|
+
await q(`RELEASE SAVEPOINT ${sp}`);
|
|
205
|
+
return out;
|
|
206
|
+
} catch (e) {
|
|
207
|
+
// ROLLBACK TO rewinds to the savepoint but LEAVES it on the stack. Releasing it as
|
|
208
|
+
// well keeps the stack balanced for whatever the outer transaction does next.
|
|
176
209
|
try {
|
|
177
|
-
|
|
178
|
-
await
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
try { await client.query(`ROLLBACK TO SAVEPOINT ${sp}`); } catch (_) { /* noop */ }
|
|
182
|
-
throw e;
|
|
183
|
-
} finally { depth--; }
|
|
210
|
+
await q(`ROLLBACK TO SAVEPOINT ${sp}`);
|
|
211
|
+
await q(`RELEASE SAVEPOINT ${sp}`);
|
|
212
|
+
} catch (_) { /* noop */ }
|
|
213
|
+
throw e;
|
|
184
214
|
}
|
|
185
|
-
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const txApi = levelApi(0);
|
|
186
218
|
try {
|
|
187
219
|
await client.query('BEGIN');
|
|
188
220
|
const out = await fn(txApi);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aria-framework/db-worker",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
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,
|
|
@@ -38,6 +38,6 @@
|
|
|
38
38
|
"pg": "^8.23.0"
|
|
39
39
|
},
|
|
40
40
|
"scripts": {
|
|
41
|
-
"test": "node test/driverContract.js && node test/migrations.js && node test/migrations-notx.js && node test/regressions.js && node test/transactions.js && node test/tenantContext.js && node test/smoke.js && node test/tier2.js"
|
|
41
|
+
"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/smoke.js && node test/tier2.js"
|
|
42
42
|
}
|
|
43
43
|
}
|