@aria-framework/db-worker 0.11.0 → 0.12.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.
Files changed (3) hide show
  1. package/README.md +26 -0
  2. package/database.js +46 -2
  3. 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 files = fs.readdirSync(migrationsDir).filter((f) => f.endsWith('.sql')).sort();
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aria-framework/db-worker",
3
- "version": "0.11.0",
3
+ "version": "0.12.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
  }