@aria-framework/db-worker 0.13.0 → 0.14.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 +27 -0
- package/database.js +71 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -229,8 +229,35 @@ Postgres sibling does nothing to an already-migrated SQLite database.
|
|
|
229
229
|
This is selection, not a portable DDL layer. An app still writes its own schema per engine —
|
|
230
230
|
the framework guarantees only which files ran and the transaction around each.
|
|
231
231
|
|
|
232
|
+
### A baseline that covers earlier migrations (0.14.0)
|
|
233
|
+
|
|
234
|
+
A baseline written for a second engine already CONTAINS everything the earlier migrations did.
|
|
235
|
+
It says so:
|
|
236
|
+
|
|
237
|
+
```sql
|
|
238
|
+
-- aria:covers-through 035
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
The runner then LEDGERS every file numbered at or below 035 instead of executing it. Without
|
|
242
|
+
that, a fresh Postgres database runs `001_baseline.postgres.sql`, builds the whole schema, and
|
|
243
|
+
then hits `002_customer_team.sql` — SQLite DDL — and fails having already changed the database.
|
|
244
|
+
|
|
245
|
+
A NUMBER, not a list: a list would have to be maintained in step with the directory, and the
|
|
246
|
+
one thing worse than no marker is one that names files which have moved.
|
|
247
|
+
|
|
248
|
+
The marker is parsed BEFORE the file is applied, so a malformed one fails with the migration
|
|
249
|
+
untouched. Like `aria:no-transaction` it is read from the HEADER only — a comment further down,
|
|
250
|
+
or inside a multi-line string literal, is not a declaration about the file — and a near miss
|
|
251
|
+
(`aria: covers-through`, `aria:covers_through`) is refused rather than ignored.
|
|
252
|
+
|
|
232
253
|
## Changelog
|
|
233
254
|
|
|
255
|
+
- **0.14.0** — `-- aria:covers-through NNN` (above).
|
|
256
|
+
- **0.13.0** — the Postgres driver had the sibling savepoint defect too. Two nested transactions
|
|
257
|
+
started together on one client destroyed each other, and the failed RELEASE aborted the
|
|
258
|
+
transaction — so the outer COMMIT became a silent ROLLBACK and the caller was told it had
|
|
259
|
+
succeeded having written nothing. Fixed the same way as SQLite: one API per nesting level,
|
|
260
|
+
each owning its children's queue.
|
|
234
261
|
- **0.12.0** — dialect-selected migration files (above).
|
|
235
262
|
|
|
236
263
|
- **0.11.0** — the tenant-context contract above: `runWithTenant()` / `currentTenant()`,
|
package/database.js
CHANGED
|
@@ -261,6 +261,54 @@ function selectForDialect(files, kind) {
|
|
|
261
261
|
return files.filter((f) => !foreign.has(f) && !specific.has(f));
|
|
262
262
|
}
|
|
263
263
|
|
|
264
|
+
// A migration may declare that it already CONTAINS everything the earlier ones did:
|
|
265
|
+
//
|
|
266
|
+
// -- aria:covers-through 035
|
|
267
|
+
//
|
|
268
|
+
// Written for a second engine, a baseline is the whole schema as of some point in the chain.
|
|
269
|
+
// The files it supersedes must be LEDGERED rather than executed — running 002_customer_team.sql
|
|
270
|
+
// (SQLite DDL) against Postgres fails, and fails having already built the schema, so the next
|
|
271
|
+
// boot re-runs a baseline over live tables.
|
|
272
|
+
//
|
|
273
|
+
// Deliberately a number rather than a list: a list would have to be maintained in step with the
|
|
274
|
+
// directory, and the one thing worse than no marker is one that names files that moved.
|
|
275
|
+
const COVERS_MARKER = /^[ \t]*--[ \t]*aria:covers-through[ \t]+(\d+)[ \t]*$/;
|
|
276
|
+
// A near miss must FAIL rather than be ignored, for the same reason the no-transaction marker
|
|
277
|
+
// has one: an author looking at a file that visibly carries the marker, while the runner did
|
|
278
|
+
// not act on it, is the worst outcome available.
|
|
279
|
+
const COVERS_NEARMISS = /^[ \t]*--[ \t]*aria:[ \t]*covers[-_ ]?through\b/i;
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* The migration number this file declares it covers, or null.
|
|
283
|
+
*
|
|
284
|
+
* Header only, like the no-transaction marker — a marker inside a string literal or a seeded
|
|
285
|
+
* row is not a declaration about the file.
|
|
286
|
+
*/
|
|
287
|
+
function coversThrough(sql, file) {
|
|
288
|
+
for (const raw of String(sql).split(/\r?\n/)) {
|
|
289
|
+
const line = raw.replace(/\uFEFF/g, '');
|
|
290
|
+
if (/^[ \t]*$/.test(line)) continue;
|
|
291
|
+
if (!/^[ \t]*--/.test(line)) return null; // first real SQL ends the header
|
|
292
|
+
const m = COVERS_MARKER.exec(line);
|
|
293
|
+
if (m) return Number(m[1]);
|
|
294
|
+
if (COVERS_NEARMISS.test(line)) {
|
|
295
|
+
throw new Error(
|
|
296
|
+
`Migration ${file || ''} has a line that looks like the covers-through marker but is not ` +
|
|
297
|
+
`exactly it: ${line.trim()}. The form is: -- aria:covers-through 035`
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return null;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** The files a covers-through declaration supersedes: numbered at or below it, and not itself. */
|
|
305
|
+
function coveredBy(file, through, files) {
|
|
306
|
+
return files.filter((f) => {
|
|
307
|
+
if (f === file) return false;
|
|
308
|
+
const n = /^(\d+)/.exec(f);
|
|
309
|
+
return n && Number(n[1]) <= through;
|
|
310
|
+
});
|
|
311
|
+
}
|
|
264
312
|
/**
|
|
265
313
|
* Apply pending numbered migrations, in filename order, each in its own transaction — unless
|
|
266
314
|
* the file opts out with `-- aria:no-transaction` (see the marker notes above).
|
|
@@ -305,6 +353,9 @@ function runMigrations(migrationsDir) {
|
|
|
305
353
|
for (const file of files) {
|
|
306
354
|
if (applied.has(file)) continue;
|
|
307
355
|
const sql = fs.readFileSync(path.join(migrationsDir, file), 'utf8');
|
|
356
|
+
// Parsed BEFORE anything is applied: a malformed marker must fail with the file
|
|
357
|
+
// untouched, not after it has already changed the schema.
|
|
358
|
+
const through = coversThrough(sql, file);
|
|
308
359
|
if (wantsNoTransaction(sql, file)) {
|
|
309
360
|
// Announced, not silent: an operator reading boot output should see which file gave up
|
|
310
361
|
// atomicity, because that is the file to inspect if the boot dies mid-migration.
|
|
@@ -324,6 +375,15 @@ function runMigrations(migrationsDir) {
|
|
|
324
375
|
}
|
|
325
376
|
count++;
|
|
326
377
|
_logger.info(`Migration applied: ${file}`);
|
|
378
|
+
|
|
379
|
+
if (through !== null) {
|
|
380
|
+
for (const covered of coveredBy(file, through, files)) {
|
|
381
|
+
if (applied.has(covered)) continue;
|
|
382
|
+
db.prepare('INSERT INTO migrations (filename) VALUES (?)').run(covered);
|
|
383
|
+
applied.add(covered);
|
|
384
|
+
}
|
|
385
|
+
_logger.info(`Migration ${file} covers everything through ${through} — ledgered, not run`);
|
|
386
|
+
}
|
|
327
387
|
}
|
|
328
388
|
_logger.info(count === 0 ? 'No pending migrations' : `Applied ${count} migration(s)`);
|
|
329
389
|
return Promise.resolve({ applied: count });
|
|
@@ -336,6 +396,9 @@ function runMigrations(migrationsDir) {
|
|
|
336
396
|
for (const file of files) {
|
|
337
397
|
if (applied.has(file)) continue;
|
|
338
398
|
const sql = fs.readFileSync(path.join(migrationsDir, file), 'utf8');
|
|
399
|
+
// Parsed BEFORE anything is applied: a malformed marker must fail with the file
|
|
400
|
+
// untouched, not after it has already changed the schema.
|
|
401
|
+
const through = coversThrough(sql, file);
|
|
339
402
|
if (wantsNoTransaction(sql, file)) {
|
|
340
403
|
_logger.warn(noTxWarning(file));
|
|
341
404
|
try {
|
|
@@ -368,6 +431,14 @@ function runMigrations(migrationsDir) {
|
|
|
368
431
|
await tx.run('INSERT INTO migrations (filename) VALUES (?)', [file]);
|
|
369
432
|
});
|
|
370
433
|
}
|
|
434
|
+
if (through !== null) {
|
|
435
|
+
for (const covered of coveredBy(file, through, files)) {
|
|
436
|
+
if (applied.has(covered)) continue;
|
|
437
|
+
await d.run('INSERT INTO migrations (filename) VALUES (?)', [covered]);
|
|
438
|
+
applied.add(covered);
|
|
439
|
+
}
|
|
440
|
+
_logger.info(`Migration ${file} covers everything through ${through} — ledgered, not run`);
|
|
441
|
+
}
|
|
371
442
|
count++;
|
|
372
443
|
_logger.info(`Migration applied: ${file}`);
|
|
373
444
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aria-framework/db-worker",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.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/migrationDialect.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/tenantRls.pg.js && node test/smoke.js && node test/tier2.js"
|
|
42
42
|
}
|
|
43
43
|
}
|