masterrecord 1.30.0 → 1.30.1

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,38 @@
1
1
  # MasterRecord Changelog
2
2
 
3
+ ## v1.30.1 — `baseline` is scoped to one context, as everything in EF is
4
+
5
+ **Data-corrupting bug.** `baseline <ctx> --all` globbed `**/*_migration.js` from the working
6
+ directory and recorded **every context's migrations into the one target context's history
7
+ table**. In a repo with several contexts that meant a context could end up with 141 history
8
+ rows for 8 real migrations, listing migrations owned by entirely unrelated contexts.
9
+
10
+ This was a divergence from EF, which never searches the filesystem for migrations: EF
11
+ resolves them by *ownership* — `IMigrationsAssembly.Migrations` contains only the migrations
12
+ whose `[DbContext(typeof(T))]` matches the context being operated on, so another context's
13
+ migration is simply not visible. `baseline` now resolves through the context's own snapshot
14
+ and migration folder via `MigrationsAssembly`, the same path every other command already
15
+ used; it was the only command still globbing from the working directory.
16
+
17
+ - `baseline <ctx> --all` records only the migrations that context owns.
18
+ - `baseline <ctx> <name>` resolves the name through `MigrationsAssembly.getMigrationId()`
19
+ (EF's `MigrationsAssemblyExtensions.GetMigrationId`), so a full id, a file name or the
20
+ bare name all work — and a migration the context does not own is refused with the list of
21
+ the ones it does own, instead of being silently written into its history.
22
+ - Removed the unused `__buildSnapshot()` helper, which derived `latestMigration` from the
23
+ history table. 1.30.0 made snapshot writing authoring-only, which orphaned it; with a
24
+ polluted history it could have reported a foreign migration as a context's latest.
25
+
26
+ **Repairing an already-polluted history.** Upgrading stops new pollution but does not remove
27
+ rows already written. For each affected context, delete the history rows naming migrations
28
+ that are not in its own migrations folder, then confirm with `masterrecord migrations-status
29
+ <ctx>` — it reports recorded-but-missing files, which is exactly what the foreign rows look
30
+ like.
31
+
32
+ New tests: `test/baseline-context-scoped.test.js` — a two-context repo proves `--all` records
33
+ only the named context's migrations, that a foreign migration is refused by name and nothing
34
+ is written on failure, and that a baselined migration is not replayed by `update-database`.
35
+
3
36
  ## v1.30.0 — the snapshot belongs to authoring, as in EF Core (migrations now replay from empty)
4
37
 
5
38
  **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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "masterrecord",
3
- "version": "1.30.0",
3
+ "version": "1.30.1",
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",