masterrecord 1.30.0 → 1.30.2

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/CHANGES.md CHANGED
@@ -1,5 +1,53 @@
1
1
  # MasterRecord Changelog
2
2
 
3
+ ## v1.30.2 — pool reuse is a debug message
4
+
5
+ Every context a server constructs reuses the process's connection pool, and each
6
+ reuse printed `[PostgreSQL] Reusing pool for <db> (refs: N)` (or `[MySQL] …`) with
7
+ `console.log`, whatever the log level. A long-running server writes thousands of
8
+ these a minute. In CodeBook's production log they buried the one line that said
9
+ why a deploy was failing.
10
+
11
+ Reuse now goes through the logger at `debug`, so it is hidden at the default
12
+ `info` level and visible with `configureLogging({ level: 'debug' })`. Creating a
13
+ pool, which happens once per process, is logged as before. Nothing else changed:
14
+ the refcount is unchanged and still only decides when an idle pool may close.
15
+
16
+ New test: `test/pool-reuse-log-level.test.js`.
17
+
18
+ ## v1.30.1 — `baseline` is scoped to one context, as everything in EF is
19
+
20
+ **Data-corrupting bug.** `baseline <ctx> --all` globbed `**/*_migration.js` from the working
21
+ directory and recorded **every context's migrations into the one target context's history
22
+ table**. In a repo with several contexts that meant a context could end up with 141 history
23
+ rows for 8 real migrations, listing migrations owned by entirely unrelated contexts.
24
+
25
+ This was a divergence from EF, which never searches the filesystem for migrations: EF
26
+ resolves them by *ownership* — `IMigrationsAssembly.Migrations` contains only the migrations
27
+ whose `[DbContext(typeof(T))]` matches the context being operated on, so another context's
28
+ migration is simply not visible. `baseline` now resolves through the context's own snapshot
29
+ and migration folder via `MigrationsAssembly`, the same path every other command already
30
+ used; it was the only command still globbing from the working directory.
31
+
32
+ - `baseline <ctx> --all` records only the migrations that context owns.
33
+ - `baseline <ctx> <name>` resolves the name through `MigrationsAssembly.getMigrationId()`
34
+ (EF's `MigrationsAssemblyExtensions.GetMigrationId`), so a full id, a file name or the
35
+ bare name all work — and a migration the context does not own is refused with the list of
36
+ the ones it does own, instead of being silently written into its history.
37
+ - Removed the unused `__buildSnapshot()` helper, which derived `latestMigration` from the
38
+ history table. 1.30.0 made snapshot writing authoring-only, which orphaned it; with a
39
+ polluted history it could have reported a foreign migration as a context's latest.
40
+
41
+ **Repairing an already-polluted history.** Upgrading stops new pollution but does not remove
42
+ rows already written. For each affected context, delete the history rows naming migrations
43
+ that are not in its own migrations folder, then confirm with `masterrecord migrations-status
44
+ <ctx>` — it reports recorded-but-missing files, which is exactly what the foreign rows look
45
+ like.
46
+
47
+ New tests: `test/baseline-context-scoped.test.js` — a two-context repo proves `--all` records
48
+ only the named context's migrations, that a foreign migration is refused by name and nothing
49
+ is written on failure, and that a baselined migration is not replayed by `update-database`.
50
+
3
51
  ## v1.30.0 — the snapshot belongs to authoring, as in EF Core (migrations now replay from empty)
4
52
 
5
53
  **Behaviour change. Read this before upgrading a project with existing migrations.**
package/Migrations/cli.js CHANGED
@@ -104,34 +104,6 @@ async function __resolveMigrationPlan(contextFileName){
104
104
  }
105
105
 
106
106
  // Helper to cleanup context and exit
107
- /**
108
- * The snapshot shape EVERY command writes.
109
- *
110
- * `update-database` used to record `latestMigration` but not `contextSeedConfig`, while
111
- * `update-database-all` did the reverse — so running one after the other rewrote the same
112
- * files back and forth, and the batch command recorded `latestMigration: null` even after
113
- * applying migrations. `latestMigration` now comes from the history table (what actually
114
- * applied) rather than the last file on disk, so a partially-failed run cannot claim a
115
- * migration it never ran.
116
- */
117
- async function __buildSnapshot({ contextInstance, contextAbs, executedLocation, cleanEntities, contextFileName }){
118
- let latestMigration = null;
119
- try {
120
- const applied = await contextInstance.database.getAppliedMigrations();
121
- latestMigration = applied.length ? applied[applied.length - 1] : null;
122
- } catch (_) { /* no history table yet — leave null */ }
123
- return {
124
- file: contextAbs,
125
- executedLocation,
126
- context: contextInstance,
127
- contextEntities: cleanEntities,
128
- contextSeedData: contextInstance.__contextSeedData || {},
129
- contextSeedConfig: contextInstance.__contextSeedConfig || {},
130
- contextFileName,
131
- latestMigration,
132
- };
133
- }
134
-
135
107
  async function __cleanupAndExit(contextInstance, exitCode = 0) {
136
108
  try {
137
109
  if (contextInstance && typeof contextInstance.close === 'function') {
@@ -1566,24 +1538,32 @@ program
1566
1538
  .description('Record a migration as applied WITHOUT running it — brings a database that already has the schema under migration control (EF: insert the history row). Use --all for every migration on disk.')
1567
1539
  .option('-a, --all', 'baseline every migration file found, not just one')
1568
1540
  .action(async function(contextFileName, migrationName, options){
1569
- const executedLocation = process.cwd();
1570
1541
  let ctx = null;
1571
1542
  try {
1572
1543
  if (!migrationName && !options.all) {
1573
1544
  console.error('❌ baseline needs a migration name, or --all.');
1574
1545
  process.exit(1);
1575
1546
  }
1576
- const ContextCtor = await __resolveContextCtor(executedLocation, contextFileName.toLowerCase());
1577
- ctx = await instantiateReadyContext(ContextCtor);
1547
+ // EF scopes migrations by OWNERSHIP, never by searching the filesystem:
1548
+ // `IMigrationsAssembly.Migrations` contains only the migrations whose
1549
+ // `[DbContext(typeof(T))]` matches the context being operated on. This used to glob
1550
+ // `**/*_migration.js` from the working directory, so in a repo with several contexts
1551
+ // `--all` recorded EVERY context's migrations into the one target context's history
1552
+ // (141 rows for 8 real migrations). Resolve through the context's own snapshot and
1553
+ // migration folder — the same MigrationsAssembly every other command uses.
1554
+ const plan = await __resolveMigrationPlan(contextFileName);
1555
+ ctx = await instantiateReadyContext(plan.ContextCtor);
1556
+ const migrationsAssembly = new MigrationsAssembly({ files: plan.mFiles });
1578
1557
 
1579
1558
  let ids;
1580
1559
  if (options.all) {
1581
- ids = globSync('**/*_migration.js', { cwd: executedLocation, dot: true, windowsPathsNoEscape: true, ignore: ['**/node_modules/**'] })
1582
- .map(f => path.basename(f))
1583
- .sort((a, b) => __getMigrationTimestamp(a) - __getMigrationTimestamp(b));
1584
- if (!ids.length) { console.log('No migration files found.'); await __cleanupAndExit(ctx, 0); return; }
1560
+ ids = migrationsAssembly.migrationIds;
1561
+ if (!ids.length) { console.log(`No migration files found for '${contextFileName}' in ${plan.migBase}.`); await __cleanupAndExit(ctx, 0); return; }
1585
1562
  } else {
1586
- ids = [path.basename(migrationName)];
1563
+ // Resolves a full id, a file name or the bare name, and throws listing the
1564
+ // context's known migrations if it owns no such migration (EF:
1565
+ // MigrationsAssemblyExtensions.GetMigrationId).
1566
+ ids = [migrationsAssembly.getMigrationId(path.basename(migrationName))];
1587
1567
  }
1588
1568
 
1589
1569
  let recorded = 0;
package/context.js CHANGED
@@ -25,6 +25,7 @@ import deleteManager from './deleteManager.js';
25
25
  import EntityTrackerModel from './Entity/entityTrackerModel.js';
26
26
  import { ConcurrencyError } from './errors.js';
27
27
  import { withRetry } from './resilience.js';
28
+ import { log } from './logging.js';
28
29
  import { globSync } from 'glob';
29
30
  import fs from 'node:fs';
30
31
  import path from 'node:path';
@@ -532,13 +533,13 @@ class context {
532
533
  const result = await cached.promise;
533
534
  this._SQLEngine = result.engine;
534
535
  this.isMySQL = true;
535
- console.log(`[MySQL] Reusing pool for ${env.database} (refs: ${cached.refCount})`);
536
+ log('debug', `[MySQL] Reusing pool for ${env.database} (refs: ${cached.refCount})`);
536
537
  return result.client;
537
538
  }
538
539
  // Already resolved
539
540
  this._SQLEngine = cached.engine;
540
541
  this.isMySQL = true;
541
- console.log(`[MySQL] Reusing pool for ${env.database} (refs: ${cached.refCount})`);
542
+ log('debug', `[MySQL] Reusing pool for ${env.database} (refs: ${cached.refCount})`);
542
543
  return cached.client;
543
544
  }
544
545
 
@@ -644,13 +645,13 @@ class context {
644
645
  const result = await cached.promise;
645
646
  this._SQLEngine = result.engine;
646
647
  this._SQLEngine.__name = sqlName;
647
- console.log(`[PostgreSQL] Reusing pool for ${env.database} (refs: ${cached.refCount})`);
648
+ log('debug', `[PostgreSQL] Reusing pool for ${env.database} (refs: ${cached.refCount})`);
648
649
  return result.pool;
649
650
  }
650
651
  // Already resolved
651
652
  this._SQLEngine = cached.engine;
652
653
  this._SQLEngine.__name = sqlName;
653
- console.log(`[PostgreSQL] Reusing pool for ${env.database} (refs: ${cached.refCount})`);
654
+ log('debug', `[PostgreSQL] Reusing pool for ${env.database} (refs: ${cached.refCount})`);
654
655
  return cached.pool;
655
656
  }
656
657
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "masterrecord",
3
- "version": "1.30.0",
3
+ "version": "1.30.2",
4
4
  "description": "An Object-relational mapping for the Master framework. Master Record connects classes to relational database tables to establish a database with almost zero-configuration ",
5
5
  "type": "module",
6
6
  "main": "./MasterRecord.js",