masterrecord 0.3.67 → 1.0.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/.claude/settings.local.json +3 -1
- package/CHANGES.md +31 -2
- package/Cache/QueryCache.js +10 -4
- package/Cache/RedisQueryCache.js +3 -2
- package/Entity/entityModel.js +1 -1
- package/Entity/entityModelBuilder.js +2 -2
- package/Entity/entityTrackerModel.js +3 -3
- package/Entity/fieldTransformer.js +1 -1
- package/FOREIGN_KEY_STRING_FIX.md +2 -2
- package/MasterRecord.js +6 -43
- package/Migrations/cli.js +70 -83
- package/Migrations/dependencyGraph.js +1 -1
- package/Migrations/migrationMySQLQuery.js +1 -1
- package/Migrations/migrationPostgresQuery.js +1 -1
- package/Migrations/migrationSQLiteQuery.js +1 -1
- package/Migrations/migrationTemplate.js +10 -10
- package/Migrations/migrations.js +14 -12
- package/Migrations/pathUtils.js +2 -5
- package/Migrations/pathUtils.test.js +33 -49
- package/Migrations/schema.js +52 -89
- package/QueryLanguage/queryMethods.js +4 -4
- package/QueryLanguage/queryParameters.js +1 -1
- package/QueryLanguage/queryScript.js +4 -3
- package/SQLLiteEngine.js +26 -7
- package/Tools.js +10 -8
- package/context.js +43 -42
- package/deleteManager.js +2 -2
- package/docs/MIGRATIONS_GUIDE.md +237 -532
- package/docs/POSTGRESQL_SETUP.md +4 -4
- package/docs/QUERY_CACHING_GUIDE.md +8 -7
- package/eslint.config.js +40 -0
- package/insertManager.js +28 -20
- package/mySQLConnect.js +2 -2
- package/mySQLEngine.js +26 -6
- package/package.json +34 -5
- package/postgresEngine.js +29 -8
- package/postgresSyncConnect.js +5 -3
- package/prettier.config.js +22 -0
- package/readme.md +35 -27
- package/test/context-errors.test.js +43 -0
- package/test/esm-imports.test.js +110 -0
- package/test/fixtures/new-setter-large-string/config/environments/env.newsetter.json +1 -0
- package/test/fixtures/new-setter-large-string/db/newsettercontext.sqlite +0 -0
- package/test/fixtures/sqlite-lifecycle/config/environments/env.test.json +1 -0
- package/test/fixtures/sqlite-lifecycle/db/lifecyclecontext.sqlite +0 -0
- package/test/migration-template.test.js +47 -0
- package/test/new-setter-large-string.test.js +191 -0
- package/test/pool-key.test.js +43 -0
- package/test/sqlite-lifecycle.test.js +76 -0
- package/.eslintrc.js +0 -290
- package/.prettierrc.js +0 -109
- package/test/anyCommaStringTest.js +0 -237
- package/test/anyMethodTest.js +0 -176
- package/test/attachDetached.test.js +0 -303
- package/test/bulk-operations-test.js +0 -235
- package/test/cache-toObject-test.js +0 -105
- package/test/cacheIntegration.test.js +0 -319
- package/test/config-glob-pattern-test.js +0 -232
- package/test/double-where-bug-test.js +0 -71
- package/test/ensure-ready-test.js +0 -251
- package/test/entity-deduplication-test.js +0 -174
- package/test/entity-methods-test.js +0 -269
- package/test/findByIdTest.js +0 -227
- package/test/foreign-key-string-value-test.js +0 -406
- package/test/global-model-registry-test.js +0 -538
- package/test/id-setting-validation.js +0 -202
- package/test/includesFeatureTest.js +0 -183
- package/test/includesTransformTest.js +0 -110
- package/test/index-bug-fix-test.js +0 -188
- package/test/insert-return-test.js +0 -39
- package/test/lifecycle-hooks-test.js +0 -258
- package/test/multiContextCache.test.js +0 -230
- package/test/new-entity-migration-test.js +0 -162
- package/test/newMethodTest.js +0 -330
- package/test/newMethodUnitTest.js +0 -320
- package/test/optInCache.test.js +0 -221
- package/test/parameterizedPlaceholderTest.js +0 -159
- package/test/pool-registry-test.js +0 -303
- package/test/post-insert-tracking-test.js +0 -258
- package/test/postgresEngineTest.js +0 -463
- package/test/postgresIntegrationTest.js +0 -381
- package/test/promise-detection-test.js +0 -290
- package/test/qa-context-pattern-test.js +0 -319
- package/test/query-helpers-test.js +0 -258
- package/test/query-isolation-test.js +0 -59
- package/test/queryCache.test.js +0 -148
- package/test/securityTest.js +0 -268
- package/test/seed-data-test.js +0 -212
- package/test/seed-deduplication-test.js +0 -313
- package/test/seed-features-integration-test.js +0 -418
- package/test/seed-migration-template-test.js +0 -220
- package/test/singleDollarPlaceholderTest.js +0 -238
- package/test/tablePrefixTest.js +0 -100
- package/test/transformerTest.js +0 -287
- package/test/v0.3.34-bug-fixes-test.js +0 -428
- package/test/validation-test.js +0 -302
- package/test/whereChainingTest.js +0 -88
package/Migrations/cli.js
CHANGED
|
@@ -1,26 +1,33 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
// version 0.0
|
|
3
|
+
// version 0.1.0 - ESM only
|
|
4
4
|
// https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/migrations/
|
|
5
5
|
// how to add environment variables on cli call example - master=development masterrecord add-migration auth authContext
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
7
|
+
import { program } from 'commander';
|
|
8
|
+
import fs from 'node:fs';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
11
|
+
import { globSync } from 'glob';
|
|
12
|
+
import { resolveMigrationsDirectory } from './pathUtils.js';
|
|
13
|
+
import Migration from './migrations.js';
|
|
14
|
+
|
|
15
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
16
|
+
const __dirname = path.dirname(__filename);
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Load a user module (context, migration) via dynamic import.
|
|
20
|
+
* Unwraps `export default X` -> X; returns whole namespace if no default.
|
|
21
|
+
*
|
|
22
|
+
* @param {string} filePath - Absolute path to the user file
|
|
23
|
+
* @returns {Promise<*>} The default export (or whole module if no default)
|
|
24
|
+
*/
|
|
25
|
+
async function __loadUserModule(filePath) {
|
|
26
|
+
const mod = await import(pathToFileURL(filePath).href);
|
|
27
|
+
return (mod && mod.default !== undefined) ? mod.default : mod;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'package.json'), 'utf8'));
|
|
24
31
|
|
|
25
32
|
// Extract numeric timestamp from migration filename (e.g., 1737999999999_name_migration.js)
|
|
26
33
|
function __getMigrationTimestamp(file){
|
|
@@ -104,7 +111,7 @@ program.option('-V', 'output the version');
|
|
|
104
111
|
var migration = new Migration();
|
|
105
112
|
var contextInstance = null;
|
|
106
113
|
try{
|
|
107
|
-
var files =
|
|
114
|
+
var files = globSync(`**/*${contextFileName}_contextSnapShot.json`, { cwd: executedLocation, dot: true, windowsPathsNoEscape: true, nocase: true });
|
|
108
115
|
var file = files && files[0] ? path.resolve(executedLocation, files[0]) : null;
|
|
109
116
|
if(!file){
|
|
110
117
|
console.log(`Error - Cannot read or find Context snapshot '${contextFileName}_contextSnapShot.json' in '${executedLocation}'.`);
|
|
@@ -112,7 +119,7 @@ program.option('-V', 'output the version');
|
|
|
112
119
|
}
|
|
113
120
|
var contextSnapshot;
|
|
114
121
|
try{
|
|
115
|
-
contextSnapshot =
|
|
122
|
+
contextSnapshot = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
116
123
|
}catch(_){
|
|
117
124
|
console.log(`Error - Cannot read context snapshot at '${file}'.`);
|
|
118
125
|
return;
|
|
@@ -126,7 +133,7 @@ program.option('-V', 'output the version');
|
|
|
126
133
|
migBase = snapDir;
|
|
127
134
|
}
|
|
128
135
|
// Find latest migration file (so we can use its class which extends schema)
|
|
129
|
-
var migrationFiles =
|
|
136
|
+
var migrationFiles = globSync(`**/*_migration.js`, { cwd: migBase, dot: true, windowsPathsNoEscape: true });
|
|
130
137
|
migrationFiles = (migrationFiles || []).map(f => path.resolve(migBase, f));
|
|
131
138
|
if(!(migrationFiles && migrationFiles.length)){
|
|
132
139
|
console.log("Error - Cannot read or find migration file");
|
|
@@ -139,7 +146,7 @@ program.option('-V', 'output the version');
|
|
|
139
146
|
|
|
140
147
|
let ContextCtor;
|
|
141
148
|
try{
|
|
142
|
-
ContextCtor =
|
|
149
|
+
ContextCtor = await __loadUserModule(contextAbs);
|
|
143
150
|
}catch(err){
|
|
144
151
|
console.error(`\n❌ Error - Cannot load Context file at '${contextAbs}'`);
|
|
145
152
|
console.error(`\nDetails:`);
|
|
@@ -152,7 +159,7 @@ program.option('-V', 'output the version');
|
|
|
152
159
|
}
|
|
153
160
|
|
|
154
161
|
// Use the migration class (extends schema) so createdatabase is available
|
|
155
|
-
var MigrationCtor =
|
|
162
|
+
var MigrationCtor = await __loadUserModule(mFile);
|
|
156
163
|
var mig = new MigrationCtor(ContextCtor);
|
|
157
164
|
contextInstance = mig._context || mig.context || null;
|
|
158
165
|
|
|
@@ -195,26 +202,6 @@ program.option('-V', 'output the version');
|
|
|
195
202
|
// contextFileName = contextFileName.toLowerCase();
|
|
196
203
|
|
|
197
204
|
// try{
|
|
198
|
-
// // find context file from main folder location
|
|
199
|
-
// // find context file from main folder location
|
|
200
|
-
// var search = `${executedLocation}/**/*${contextFileName}_contextSnapShot.json`;
|
|
201
|
-
// var files = globSearch.sync(search, executedLocation);
|
|
202
|
-
// var file = files[0];
|
|
203
|
-
|
|
204
|
-
// if(file){
|
|
205
|
-
// var contextSnapshot = require(file);
|
|
206
|
-
// var context = require(contextSnapshot.contextLocation);
|
|
207
|
-
// var newSchema = new schema(context);
|
|
208
|
-
// newSchema.createDatabase(dbName);
|
|
209
|
-
// }
|
|
210
|
-
// else{
|
|
211
|
-
// console.log("Error - Cannot read or find Context file");
|
|
212
|
-
// }
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
// }catch (e){
|
|
216
|
-
// console.log("Error - Cannot read or find file ", e);
|
|
217
|
-
// }
|
|
218
205
|
// console.log("Database Created");
|
|
219
206
|
|
|
220
207
|
// });
|
|
@@ -230,7 +217,7 @@ program.option('-V', 'output the version');
|
|
|
230
217
|
var migration = new Migration();
|
|
231
218
|
try{
|
|
232
219
|
// find context file from main folder location
|
|
233
|
-
var files =
|
|
220
|
+
var files = globSync(`**/*${contextFileName}_contextSnapShot.json`, { cwd: executedLocation, dot: true, windowsPathsNoEscape: true, nocase: true });
|
|
234
221
|
var file = files && files[0] ? path.resolve(executedLocation, files[0]) : null;
|
|
235
222
|
if(!file){
|
|
236
223
|
console.log(`Error - Cannot read or find Context snapshot '${contextFileName}_contextSnapShot.json' in '${executedLocation}'. Run 'masterrecord enable-migrations ${contextFileName}'.`);
|
|
@@ -238,7 +225,7 @@ program.option('-V', 'output the version');
|
|
|
238
225
|
}
|
|
239
226
|
var contextSnapshot = null;
|
|
240
227
|
try{
|
|
241
|
-
contextSnapshot =
|
|
228
|
+
contextSnapshot = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
242
229
|
}catch(_){
|
|
243
230
|
console.log(`Error - Cannot read context snapshot at '${file}'.`);
|
|
244
231
|
return;
|
|
@@ -256,7 +243,7 @@ program.option('-V', 'output the version');
|
|
|
256
243
|
|
|
257
244
|
let ContextCtor;
|
|
258
245
|
try{
|
|
259
|
-
ContextCtor =
|
|
246
|
+
ContextCtor = await __loadUserModule(contextAbs);
|
|
260
247
|
}catch(err){
|
|
261
248
|
console.error(`\n❌ Error - Cannot load Context file at '${contextAbs}'`);
|
|
262
249
|
console.error(`\nDetails:`);
|
|
@@ -298,7 +285,7 @@ program.option('-V', 'output the version');
|
|
|
298
285
|
return;
|
|
299
286
|
}
|
|
300
287
|
|
|
301
|
-
var newEntity = migration.template(name, contextSnapshot.schema, cleanEntities, seedData, seedConfig);
|
|
288
|
+
var newEntity = migration.template(name, contextSnapshot.schema, cleanEntities, seedData, seedConfig, null);
|
|
302
289
|
if(!fs.existsSync(migBase)){
|
|
303
290
|
try{ fs.mkdirSync(migBase, { recursive: true }); }catch(_){ /* ignore */ }
|
|
304
291
|
}
|
|
@@ -330,7 +317,7 @@ program.option('-V', 'output the version');
|
|
|
330
317
|
try{
|
|
331
318
|
console.log(`\n🔍 Searching for context snapshot '${contextFileName}_contextSnapShot.json'...`);
|
|
332
319
|
// find context snapshot (cwd-based glob)
|
|
333
|
-
var files =
|
|
320
|
+
var files = globSync(`**/*${contextFileName}_contextSnapShot.json`, { cwd: executedLocation, dot: true, windowsPathsNoEscape: true, nocase: true });
|
|
334
321
|
var file = files && files[0] ? path.resolve(executedLocation, files[0]) : null;
|
|
335
322
|
|
|
336
323
|
if(!file){
|
|
@@ -345,7 +332,7 @@ program.option('-V', 'output the version');
|
|
|
345
332
|
|
|
346
333
|
var contextSnapshot;
|
|
347
334
|
try{
|
|
348
|
-
contextSnapshot =
|
|
335
|
+
contextSnapshot = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
349
336
|
}catch(err){
|
|
350
337
|
console.error(`\n❌ Error - Cannot load context snapshot`);
|
|
351
338
|
console.error(`\nFile: ${file}`);
|
|
@@ -363,7 +350,7 @@ program.option('-V', 'output the version');
|
|
|
363
350
|
}
|
|
364
351
|
|
|
365
352
|
console.log(`\n🔍 Searching for migration files in: ${migBase}`);
|
|
366
|
-
var migrationFiles =
|
|
353
|
+
var migrationFiles = globSync(`**/*_migration.js`, { cwd: migBase, dot: true, windowsPathsNoEscape: true });
|
|
367
354
|
migrationFiles = (migrationFiles || []).map(f => path.resolve(migBase, f));
|
|
368
355
|
|
|
369
356
|
if(!(migrationFiles && migrationFiles.length)){
|
|
@@ -384,8 +371,8 @@ program.option('-V', 'output the version');
|
|
|
384
371
|
var migrationProjectFile;
|
|
385
372
|
var ContextCtor;
|
|
386
373
|
try{
|
|
387
|
-
migrationProjectFile =
|
|
388
|
-
ContextCtor =
|
|
374
|
+
migrationProjectFile = await __loadUserModule(mFile);
|
|
375
|
+
ContextCtor = await __loadUserModule(contextAbs);
|
|
389
376
|
}catch(err){
|
|
390
377
|
console.error(`\n❌ Error - Cannot load Context or migration file`);
|
|
391
378
|
console.error(`\nContext file: ${contextAbs}`);
|
|
@@ -516,7 +503,7 @@ program.option('-V', 'output the version');
|
|
|
516
503
|
var migration = new Migration();
|
|
517
504
|
var contextInstance = null;
|
|
518
505
|
try{
|
|
519
|
-
var files =
|
|
506
|
+
var files = globSync(`**/*${contextFileName}_contextSnapShot.json`, { cwd: executedLocation, dot: true, windowsPathsNoEscape: true, nocase: true });
|
|
520
507
|
var file = files && files[0] ? path.resolve(executedLocation, files[0]) : null;
|
|
521
508
|
if(!file){
|
|
522
509
|
console.log(`Error - Cannot read or find Context snapshot '${contextFileName}_contextSnapShot.json' in '${executedLocation}'.`);
|
|
@@ -524,7 +511,7 @@ program.option('-V', 'output the version');
|
|
|
524
511
|
}
|
|
525
512
|
var contextSnapshot;
|
|
526
513
|
try{
|
|
527
|
-
contextSnapshot =
|
|
514
|
+
contextSnapshot = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
528
515
|
}catch(_){
|
|
529
516
|
console.log(`Error - Cannot read context snapshot at '${file}'.`);
|
|
530
517
|
return;
|
|
@@ -532,7 +519,7 @@ program.option('-V', 'output the version');
|
|
|
532
519
|
const snapDir = path.dirname(file);
|
|
533
520
|
const contextAbs = path.resolve(snapDir, contextSnapshot.contextLocation || '');
|
|
534
521
|
const migBase = path.resolve(snapDir, contextSnapshot.migrationFolder || '.');
|
|
535
|
-
var migrationFiles =
|
|
522
|
+
var migrationFiles = globSync(`**/*_migration.js`, { cwd: migBase, dot: true, windowsPathsNoEscape: true });
|
|
536
523
|
migrationFiles = (migrationFiles || []).map(f => path.resolve(migBase, f));
|
|
537
524
|
if(!(migrationFiles && migrationFiles.length)){
|
|
538
525
|
console.log("Error - Cannot read or find migration file");
|
|
@@ -547,7 +534,7 @@ program.option('-V', 'output the version');
|
|
|
547
534
|
// Prepare context and table object
|
|
548
535
|
let ContextCtor;
|
|
549
536
|
try{
|
|
550
|
-
ContextCtor =
|
|
537
|
+
ContextCtor = await __loadUserModule(contextAbs);
|
|
551
538
|
}catch(err){
|
|
552
539
|
console.error(`\n❌ Error - Cannot load Context file at '${contextAbs}'`);
|
|
553
540
|
console.error(`\nDetails:`);
|
|
@@ -578,7 +565,7 @@ program.option('-V', 'output the version');
|
|
|
578
565
|
var cleanEntities = migration.cleanEntities(contextInstance.__entities);
|
|
579
566
|
var tableObj = migration.buildUpObject(contextSnapshot.schema, cleanEntities);
|
|
580
567
|
|
|
581
|
-
var MigCtor =
|
|
568
|
+
var MigCtor = await __loadUserModule(latestFile);
|
|
582
569
|
var migInstance = new MigCtor(ContextCtor);
|
|
583
570
|
if(typeof migInstance.down === 'function'){
|
|
584
571
|
await migInstance.down(tableObj);
|
|
@@ -618,7 +605,7 @@ program.option('-V', 'output the version');
|
|
|
618
605
|
var contextInstance = null;
|
|
619
606
|
try{
|
|
620
607
|
// find context snapshot (cwd-based glob)
|
|
621
|
-
var files =
|
|
608
|
+
var files = globSync(`**/*${contextFileName}_contextSnapShot.json`, { cwd: executedLocation, dot: true, windowsPathsNoEscape: true, nocase: true });
|
|
622
609
|
var file = files && files[0] ? path.resolve(executedLocation, files[0]) : null;
|
|
623
610
|
if(!file){
|
|
624
611
|
console.log(`Error - Cannot read or find Context snapshot '${contextFileName}_contextSnapShot.json' in '${executedLocation}'.`);
|
|
@@ -626,7 +613,7 @@ program.option('-V', 'output the version');
|
|
|
626
613
|
}
|
|
627
614
|
var contextSnapshot;
|
|
628
615
|
try{
|
|
629
|
-
contextSnapshot =
|
|
616
|
+
contextSnapshot = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
630
617
|
}catch(_){
|
|
631
618
|
console.log(`Error - Cannot read context snapshot at '${file}'.`);
|
|
632
619
|
return;
|
|
@@ -634,7 +621,7 @@ program.option('-V', 'output the version');
|
|
|
634
621
|
const snapDir = path.dirname(file);
|
|
635
622
|
const contextAbs = path.resolve(snapDir, contextSnapshot.contextLocation || '');
|
|
636
623
|
const migBase = path.resolve(snapDir, contextSnapshot.migrationFolder || '.');
|
|
637
|
-
var migrationFiles =
|
|
624
|
+
var migrationFiles = globSync(`**/*_migration.js`, { cwd: migBase, dot: true, windowsPathsNoEscape: true });
|
|
638
625
|
migrationFiles = (migrationFiles || []).map(f => path.resolve(migBase, f));
|
|
639
626
|
if(!(migrationFiles && migrationFiles.length)){
|
|
640
627
|
console.log("Error - Cannot read or find migration file");
|
|
@@ -646,7 +633,7 @@ program.option('-V', 'output the version');
|
|
|
646
633
|
});
|
|
647
634
|
let ContextCtor;
|
|
648
635
|
try{
|
|
649
|
-
ContextCtor =
|
|
636
|
+
ContextCtor = await __loadUserModule(contextAbs);
|
|
650
637
|
}catch(err){
|
|
651
638
|
console.error(`\n❌ Error - Cannot load Context file at '${contextAbs}'`);
|
|
652
639
|
console.error(`\nDetails:`);
|
|
@@ -677,7 +664,7 @@ program.option('-V', 'output the version');
|
|
|
677
664
|
var cleanEntities = migration.cleanEntities(contextInstance.__entities);
|
|
678
665
|
for (let i = 0; i < mFiles.length; i++) {
|
|
679
666
|
var migFile = mFiles[i];
|
|
680
|
-
var migrationProjectFile =
|
|
667
|
+
var migrationProjectFile = await __loadUserModule(migFile);
|
|
681
668
|
var newMigrationProjectInstance = new migrationProjectFile(ContextCtor);
|
|
682
669
|
var tableObj = migration.buildUpObject(contextSnapshot.schema, cleanEntities);
|
|
683
670
|
await newMigrationProjectInstance.up(tableObj);
|
|
@@ -709,7 +696,7 @@ program.option('-V', 'output the version');
|
|
|
709
696
|
.action(function(contextFileName){
|
|
710
697
|
var executedLocation = process.cwd();
|
|
711
698
|
contextFileName = contextFileName.toLowerCase();
|
|
712
|
-
var files =
|
|
699
|
+
var files = globSync(`**/*${contextFileName}_contextSnapShot.json`, { cwd: executedLocation, dot: true, windowsPathsNoEscape: true, nocase: true });
|
|
713
700
|
var file = files && files[0] ? path.resolve(executedLocation, files[0]) : null;
|
|
714
701
|
if(!file){
|
|
715
702
|
console.log(`Error - Cannot read or find Context snapshot '${contextFileName}_contextSnapShot.json' in '${executedLocation}'.`);
|
|
@@ -717,12 +704,12 @@ program.option('-V', 'output the version');
|
|
|
717
704
|
}
|
|
718
705
|
var contextSnapshot;
|
|
719
706
|
try{
|
|
720
|
-
contextSnapshot =
|
|
707
|
+
contextSnapshot = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
721
708
|
}catch(_){
|
|
722
709
|
console.log(`Error - Cannot read context snapshot at '${file}'.`);
|
|
723
710
|
return;
|
|
724
711
|
}
|
|
725
|
-
var migrationFiles =
|
|
712
|
+
var migrationFiles = globSync(`**/*_migration.js`, { cwd: contextSnapshot.migrationFolder, dot: true, windowsPathsNoEscape: true });
|
|
726
713
|
if(!(migrationFiles && migrationFiles.length)){
|
|
727
714
|
console.log("No migration files found.");
|
|
728
715
|
return;
|
|
@@ -749,7 +736,7 @@ program.option('-V', 'output the version');
|
|
|
749
736
|
var targetName = path.basename(migrationFileName);
|
|
750
737
|
|
|
751
738
|
// Locate the target migration file anywhere under the current folder
|
|
752
|
-
var targetMatches =
|
|
739
|
+
var targetMatches = globSync(`**/${targetName}`, { cwd: executedLocation, dot: true, windowsPathsNoEscape: true });
|
|
753
740
|
if(!(targetMatches && targetMatches.length)){
|
|
754
741
|
console.log(`Error - Cannot read or find migration file '${targetName}' in '${executedLocation}'.`);
|
|
755
742
|
return;
|
|
@@ -758,7 +745,7 @@ program.option('-V', 'output the version');
|
|
|
758
745
|
var migrationFolder = path.dirname(targetFilePath);
|
|
759
746
|
|
|
760
747
|
// Find the context snapshot within the same migrations folder
|
|
761
|
-
var snapshotMatches =
|
|
748
|
+
var snapshotMatches = globSync(`**/*_contextSnapShot.json`, { cwd: migrationFolder, dot: true, windowsPathsNoEscape: true });
|
|
762
749
|
var snapshotFile = snapshotMatches && snapshotMatches[0] ? path.resolve(migrationFolder, snapshotMatches[0]) : null;
|
|
763
750
|
if(!snapshotFile){
|
|
764
751
|
console.log("Error - Cannot read or find Context snapshot in migration folder.");
|
|
@@ -767,14 +754,14 @@ program.option('-V', 'output the version');
|
|
|
767
754
|
|
|
768
755
|
var contextSnapshot;
|
|
769
756
|
try{
|
|
770
|
-
contextSnapshot =
|
|
757
|
+
contextSnapshot = JSON.parse(fs.readFileSync(snapshotFile, 'utf8'));
|
|
771
758
|
}catch(_){
|
|
772
759
|
console.log(`Error - Cannot read context snapshot at '${snapshotFile}'.`);
|
|
773
760
|
return;
|
|
774
761
|
}
|
|
775
762
|
|
|
776
763
|
// Get all migration files in this folder
|
|
777
|
-
var allMigrationFiles =
|
|
764
|
+
var allMigrationFiles = globSync(`**/*_migration.js`, { cwd: migrationFolder, dot: true, windowsPathsNoEscape: true });
|
|
778
765
|
if(!(allMigrationFiles && allMigrationFiles.length)){
|
|
779
766
|
console.log("Error - Cannot read or find migration file");
|
|
780
767
|
return;
|
|
@@ -795,7 +782,7 @@ program.option('-V', 'output the version');
|
|
|
795
782
|
// Prepare context and table object
|
|
796
783
|
let ContextCtor;
|
|
797
784
|
try{
|
|
798
|
-
ContextCtor =
|
|
785
|
+
ContextCtor = await __loadUserModule(contextAbs);
|
|
799
786
|
}catch(err){
|
|
800
787
|
console.error(`\n❌ Error - Cannot load Context file at '${contextAbs}'`);
|
|
801
788
|
console.error(`\nDetails:`);
|
|
@@ -829,7 +816,7 @@ program.option('-V', 'output the version');
|
|
|
829
816
|
// Roll back (down) all migrations newer than the target (i.e., strictly after targetIndex)
|
|
830
817
|
for (var i = sorted.length - 1; i > targetIndex; i--) {
|
|
831
818
|
var migFile = path.resolve(migrationFolder, sorted[i]);
|
|
832
|
-
var MigCtor =
|
|
819
|
+
var MigCtor = await __loadUserModule(migFile);
|
|
833
820
|
var migInstance = new MigCtor(ContextCtor);
|
|
834
821
|
if(typeof migInstance.down === 'function'){
|
|
835
822
|
await migInstance.down(tableObj);
|
|
@@ -867,7 +854,7 @@ program.option('-V', 'output the version');
|
|
|
867
854
|
var executedLocation = process.cwd();
|
|
868
855
|
var contextInstances = [];
|
|
869
856
|
try{
|
|
870
|
-
var snapshotFiles =
|
|
857
|
+
var snapshotFiles = globSync('**/*_contextSnapShot.json', { cwd: executedLocation, dot: true, windowsPathsNoEscape: true, nocase: true });
|
|
871
858
|
if(!(snapshotFiles && snapshotFiles.length)){
|
|
872
859
|
console.log('No context snapshots found. Run enable-migrations-all first.');
|
|
873
860
|
return;
|
|
@@ -877,14 +864,14 @@ program.option('-V', 'output the version');
|
|
|
877
864
|
try{
|
|
878
865
|
const snapFile = path.resolve(executedLocation, snapRel);
|
|
879
866
|
let cs;
|
|
880
|
-
try{ cs =
|
|
867
|
+
try{ cs = JSON.parse(fs.readFileSync(snapFile, 'utf8')); }catch(_){ continue; }
|
|
881
868
|
const snapDir = path.dirname(snapFile);
|
|
882
869
|
const contextAbs = path.resolve(snapDir, cs.contextLocation || '');
|
|
883
870
|
const migBase = path.resolve(snapDir, cs.migrationFolder || '.');
|
|
884
871
|
// Load context
|
|
885
872
|
let ContextCtor;
|
|
886
873
|
try{
|
|
887
|
-
ContextCtor =
|
|
874
|
+
ContextCtor = await __loadUserModule(contextAbs);
|
|
888
875
|
}catch(err){
|
|
889
876
|
console.error(`⚠️ Skipping ${path.basename(contextAbs)}: cannot load Context file`);
|
|
890
877
|
console.error(` Details: ${err.message}`);
|
|
@@ -909,7 +896,7 @@ program.option('-V', 'output the version');
|
|
|
909
896
|
console.log(`No changes detected for ${path.basename(contextAbs)}. Skipping.`);
|
|
910
897
|
continue;
|
|
911
898
|
}
|
|
912
|
-
var newEntity = migration.template(name, cs.schema, cleanEntities, seedData, seedConfig);
|
|
899
|
+
var newEntity = migration.template(name, cs.schema, cleanEntities, seedData, seedConfig, null);
|
|
913
900
|
if(!fs.existsSync(migBase)){
|
|
914
901
|
try{ fs.mkdirSync(migBase, { recursive: true }); }catch(_){ /* ignore */ }
|
|
915
902
|
}
|
|
@@ -953,7 +940,7 @@ program.option('-V', 'output the version');
|
|
|
953
940
|
var contextInstances = [];
|
|
954
941
|
try{
|
|
955
942
|
// Find all context snapshots and run update per snapshot (avoids unrelated framework contexts)
|
|
956
|
-
var snapshotFiles =
|
|
943
|
+
var snapshotFiles = globSync('**/*_contextSnapShot.json', { cwd: executedLocation, dot: true, windowsPathsNoEscape: true, nocase: true });
|
|
957
944
|
if(!(snapshotFiles && snapshotFiles.length)){
|
|
958
945
|
console.log('No context snapshots found. Run enable-migrations for each context first.');
|
|
959
946
|
return;
|
|
@@ -963,18 +950,18 @@ program.option('-V', 'output the version');
|
|
|
963
950
|
for(const snapRel of snapshotFiles){
|
|
964
951
|
const snapFile = path.resolve(executedLocation, snapRel);
|
|
965
952
|
let cs;
|
|
966
|
-
try{ cs =
|
|
953
|
+
try{ cs = JSON.parse(fs.readFileSync(snapFile, 'utf8')); }catch(_){ continue; }
|
|
967
954
|
const snapDir = path.dirname(snapFile);
|
|
968
955
|
const contextAbs = path.resolve(snapDir, cs.contextLocation || '');
|
|
969
956
|
let migBase = path.resolve(snapDir, cs.migrationFolder || '.');
|
|
970
957
|
const nameFromPath = path.basename(snapFile).replace(/_contextSnapShot\.json$/i, '').toLowerCase();
|
|
971
958
|
const ctxName = contextAbs ? path.basename(contextAbs).replace(/\.js$/i, '').toLowerCase() : nameFromPath;
|
|
972
959
|
// Find migrations in snapshot's migrationFolder; fallback to <ContextDir>/db/migrations
|
|
973
|
-
let migRel =
|
|
960
|
+
let migRel = globSync('**/*_migration.js', { cwd: migBase, dot: true, windowsPathsNoEscape: true, nocase: true }) || [];
|
|
974
961
|
if(!(migRel && migRel.length)){
|
|
975
962
|
// Fallback: find migrations directory using shared utility (prevents duplicate paths)
|
|
976
963
|
const defaultFolder = resolveMigrationsDirectory(contextAbs || snapFile);
|
|
977
|
-
migRel =
|
|
964
|
+
migRel = globSync('**/*_migration.js', { cwd: defaultFolder, dot: true, windowsPathsNoEscape: true, nocase: true }) || [];
|
|
978
965
|
if(migRel && migRel.length){ migBase = defaultFolder; }
|
|
979
966
|
}
|
|
980
967
|
const migs = migRel.map(f => path.resolve(migBase, f));
|
|
@@ -1001,7 +988,7 @@ program.option('-V', 'output the version');
|
|
|
1001
988
|
|
|
1002
989
|
var ContextCtor;
|
|
1003
990
|
try{
|
|
1004
|
-
ContextCtor =
|
|
991
|
+
ContextCtor = await __loadUserModule(entry.contextAbs);
|
|
1005
992
|
}catch(err){
|
|
1006
993
|
console.error(`⚠️ Skipping ${entry.ctxName}: cannot load Context file`);
|
|
1007
994
|
console.error(` Details: ${err.message}`);
|
|
@@ -1016,7 +1003,7 @@ program.option('-V', 'output the version');
|
|
|
1016
1003
|
console.error(` Details: ${err.message}`);
|
|
1017
1004
|
continue;
|
|
1018
1005
|
}
|
|
1019
|
-
var migrationProjectFile =
|
|
1006
|
+
var migrationProjectFile = await __loadUserModule(mFile);
|
|
1020
1007
|
var newMigrationProjectInstance = new migrationProjectFile(ContextCtor);
|
|
1021
1008
|
var cleanEntities = migration.cleanEntities(contextInstance.__entities);
|
|
1022
1009
|
var tableObj = migration.buildUpObject(entry.cs.schema, cleanEntities);
|
|
@@ -1063,7 +1050,7 @@ program.option('-V', 'output the version');
|
|
|
1063
1050
|
var executedLocation = process.cwd();
|
|
1064
1051
|
try{
|
|
1065
1052
|
// Find candidate Context files
|
|
1066
|
-
var candidates =
|
|
1053
|
+
var candidates = globSync('**/*Context.js', { cwd: executedLocation, dot: true, windowsPathsNoEscape: true, nocase: true }) || [];
|
|
1067
1054
|
if(!(candidates && candidates.length)){
|
|
1068
1055
|
console.log('No Context files found.');
|
|
1069
1056
|
return;
|
|
@@ -1079,7 +1066,7 @@ program.option('-V', 'output the version');
|
|
|
1079
1066
|
// Heuristic filter: file must look like a MasterRecord context
|
|
1080
1067
|
let text = '';
|
|
1081
1068
|
try{ text = fs.readFileSync(abs, 'utf8'); }catch(_){ continue; }
|
|
1082
|
-
const looksLikeContext = /extends\s+masterrecord\.context/i.test(text) || /
|
|
1069
|
+
const looksLikeContext = /extends\s+masterrecord\.context/i.test(text) || /import\s+masterrecord\s+from\s+['"]masterrecord['"]/i.test(text);
|
|
1083
1070
|
if(!looksLikeContext){ continue; }
|
|
1084
1071
|
const ctxName = path.basename(abs).replace(/\.js$/i,'');
|
|
1085
1072
|
const key = ctxName.toLowerCase();
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
1
|
// https://channel9.msdn.com/Blogs/EF/Migrations-Under-the-Hood
|
|
4
|
-
// version 0.0.
|
|
2
|
+
// version 0.0.4
|
|
3
|
+
|
|
4
|
+
import os from 'node:os';
|
|
5
5
|
|
|
6
|
-
const os = require('os');
|
|
7
6
|
class MigrationTemplate {
|
|
8
7
|
|
|
9
8
|
constructor(name) {
|
|
@@ -13,10 +12,11 @@ class MigrationTemplate {
|
|
|
13
12
|
#up = ''
|
|
14
13
|
#down = ''
|
|
15
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Render the migration file source (ESM only).
|
|
17
|
+
*/
|
|
16
18
|
get(){
|
|
17
|
-
return `
|
|
18
|
-
|
|
19
|
-
var masterrecord = require('masterrecord');
|
|
19
|
+
return `import masterrecord from 'masterrecord';
|
|
20
20
|
|
|
21
21
|
class ${this.name} extends masterrecord.schema {
|
|
22
22
|
constructor(context){
|
|
@@ -33,8 +33,8 @@ class ${this.name} extends masterrecord.schema {
|
|
|
33
33
|
${this.#down}
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
export default ${this.name};
|
|
37
|
+
`;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
alterColumn(type, name, parent){
|
|
@@ -298,5 +298,5 @@ module.exports = ${this.name};
|
|
|
298
298
|
|
|
299
299
|
}
|
|
300
300
|
|
|
301
|
-
|
|
301
|
+
export default MigrationTemplate;
|
|
302
302
|
|
package/Migrations/migrations.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
// version 0.0.
|
|
1
|
+
// version 0.0.11
|
|
2
2
|
// learn more about seeding info - https://www.pauric.blog/Database-Updates-and-Migrations-with-Entity-Framework/
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { pathToFileURL } from 'node:url';
|
|
7
|
+
import * as diff from 'deep-object-diff';
|
|
8
|
+
import { globSync } from 'glob';
|
|
9
|
+
import MigrationTemplate from './migrationTemplate.js';
|
|
10
|
+
import { resolveMigrationsDirectory } from './pathUtils.js';
|
|
10
11
|
|
|
11
12
|
// https://blog.tekspace.io/code-first-multiple-db-context-migration/
|
|
12
13
|
|
|
@@ -357,7 +358,7 @@ class Migrations{
|
|
|
357
358
|
|
|
358
359
|
|
|
359
360
|
findContextFile(executedLocation, contextFileName){
|
|
360
|
-
var files =
|
|
361
|
+
var files = globSync(`**/*${contextFileName}.js`, {
|
|
361
362
|
cwd: executedLocation,
|
|
362
363
|
dot: true,
|
|
363
364
|
windowsPathsNoEscape: true
|
|
@@ -366,8 +367,8 @@ class Migrations{
|
|
|
366
367
|
return file;
|
|
367
368
|
}
|
|
368
369
|
|
|
369
|
-
findContext(executedLocation, contextFileName){
|
|
370
|
-
var files =
|
|
370
|
+
async findContext(executedLocation, contextFileName){
|
|
371
|
+
var files = globSync(`**/*${contextFileName}.js`, {
|
|
371
372
|
cwd: executedLocation,
|
|
372
373
|
dot: true,
|
|
373
374
|
windowsPathsNoEscape: true
|
|
@@ -376,7 +377,8 @@ class Migrations{
|
|
|
376
377
|
if(!file){
|
|
377
378
|
return null;
|
|
378
379
|
}
|
|
379
|
-
|
|
380
|
+
const mod = await import(pathToFileURL(file).href);
|
|
381
|
+
const context = (mod && mod.default !== undefined) ? mod.default : mod;
|
|
380
382
|
return {
|
|
381
383
|
context : context,
|
|
382
384
|
fileLocation : file
|
|
@@ -583,4 +585,4 @@ class Migrations{
|
|
|
583
585
|
|
|
584
586
|
}
|
|
585
587
|
|
|
586
|
-
|
|
588
|
+
export default Migrations;
|
package/Migrations/pathUtils.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Handles path resolution to prevent duplicate db/migrations in paths
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
import path from 'node:path';
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Resolve migrations directory, avoiding duplicate db/migrations paths
|
|
@@ -70,7 +70,4 @@ function isInMigrationsDirectory(filePath) {
|
|
|
70
70
|
return normalized.includes('/db/migrations') || normalized.includes('\\db\\migrations');
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
|
|
74
|
-
resolveMigrationsDirectory,
|
|
75
|
-
isInMigrationsDirectory
|
|
76
|
-
};
|
|
73
|
+
export { resolveMigrationsDirectory, isInMigrationsDirectory };
|