masterrecord 0.2.6 → 0.2.7

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 (2) hide show
  1. package/Migrations/cli.js +169 -53
  2. package/package.json +1 -1
package/Migrations/cli.js CHANGED
@@ -231,49 +231,62 @@ program.option('-V', 'output the version');
231
231
  var search = `${executedLocation}/**/*${contextFileName}_contextSnapShot.json`;
232
232
  var files = globSearch.sync(search, executedLocation);
233
233
  var file = files[0];
234
- if(file){
235
- var contextSnapshot = require(file);
236
- var searchMigration = `${contextSnapshot.migrationFolder}/**/*_migration.js`;
237
- var migrationFiles = globSearch.sync(searchMigration, contextSnapshot.migrationFolder);
238
- if( migrationFiles && migrationFiles.length){
239
- // organize by time using filename timestamp or file mtime
240
- var mFiles = migrationFiles.slice().sort(function(a, b){
241
- return __getMigrationTimestamp(a) - __getMigrationTimestamp(b);
242
- });
243
- var context = require(contextSnapshot.contextLocation);
244
-
245
- for (let i = 0; i < mFiles.length; i++) {
246
- var file = mFiles[i];
247
- var migrationProjectFile = require(file);
248
-
249
- var contextInstance = new context();
250
- var newMigrationProjectInstance = new migrationProjectFile(context);
251
- var cleanEntities = migration.cleanEntities(contextInstance.__entities);
252
- var tableObj = migration.buildUpObject(contextSnapshot.schema, cleanEntities);
253
- newMigrationProjectInstance.up(tableObj);
254
- }
255
-
256
-
257
- var snap = {
258
- file : contextSnapshot.contextLocation,
259
- executedLocation : executedLocation,
260
- context : contextInstance,
261
- contextEntities : cleanEntities,
262
- contextFileName: contextFileName
263
- }
264
-
265
- migration.createSnapShot(snap);
266
- console.log("database updated");
267
- }
268
- else{
234
+ if(!file){
235
+ console.log(`Error - Cannot read or find Context snapshot '${contextFileName}_contextSnapShot.json' in '${executedLocation}'.`);
236
+ return;
237
+ }
238
+ var contextSnapshot;
239
+ try{
240
+ contextSnapshot = require(file);
241
+ }catch(_){
242
+ console.log(`Error - Cannot read context snapshot at '${file}'.`);
243
+ return;
244
+ }
245
+ var searchMigration = `${contextSnapshot.migrationFolder}/**/*_migration.js`;
246
+ var migrationFiles = globSearch.sync(searchMigration, contextSnapshot.migrationFolder);
247
+ if(!(migrationFiles && migrationFiles.length)){
269
248
  console.log("Error - Cannot read or find migration file");
270
- }
271
-
249
+ return;
272
250
  }
273
- else{
274
- console.log("Error - Cannot read or find Context file");
275
- }
276
- }catch (e){
251
+ // organize by time using filename timestamp or file mtime
252
+ var mFiles = migrationFiles.slice().sort(function(a, b){
253
+ return __getMigrationTimestamp(a) - __getMigrationTimestamp(b);
254
+ });
255
+ let ContextCtor;
256
+ try{
257
+ ContextCtor = require(contextSnapshot.contextLocation);
258
+ }catch(_){
259
+ console.log(`Error - Cannot load Context file at '${contextSnapshot.contextLocation}'.`);
260
+ return;
261
+ }
262
+ var contextInstance;
263
+ try{
264
+ contextInstance = new ContextCtor();
265
+ }catch(_){
266
+ console.log(`Error - Failed to construct Context from '${contextSnapshot.contextLocation}'.`);
267
+ return;
268
+ }
269
+ var cleanEntities = migration.cleanEntities(contextInstance.__entities);
270
+ for (let i = 0; i < mFiles.length; i++) {
271
+ var migFile = mFiles[i];
272
+ var migrationProjectFile = require(migFile);
273
+ var newMigrationProjectInstance = new migrationProjectFile(ContextCtor);
274
+ var tableObj = migration.buildUpObject(contextSnapshot.schema, cleanEntities);
275
+ newMigrationProjectInstance.up(tableObj);
276
+ }
277
+ var snap = {
278
+ file : contextSnapshot.contextLocation,
279
+ executedLocation : executedLocation,
280
+ context : contextInstance,
281
+ contextEntities : cleanEntities,
282
+ contextFileName: contextFileName
283
+ }
284
+
285
+ migration.createSnapShot(snap);
286
+ console.log("database updated");
287
+
288
+ }
289
+ catch (e){
277
290
  console.log("Error - Cannot read or find file ", e);
278
291
  }
279
292
  });
@@ -289,16 +302,27 @@ program.option('-V', 'output the version');
289
302
  var search = `${executedLocation}/**/*${contextFileName}_contextSnapShot.json`;
290
303
  var files = globSearch.sync(search, executedLocation);
291
304
  var file = files[0];
292
- if(file){
293
- var contextSnapshot = require(file);
294
- var searchMigration = `${contextSnapshot.migrationFolder}/**/*_migration.js`;
295
- var migrationFiles = globSearch.sync(searchMigration, contextSnapshot.migrationFolder);
296
- if( migrationFiles){
297
- return migrationFiles;
298
- }
305
+ if(!file){
306
+ console.log(`Error - Cannot read or find Context snapshot '${contextFileName}_contextSnapShot.json' in '${executedLocation}'.`);
307
+ return;
308
+ }
309
+ var contextSnapshot;
310
+ try{
311
+ contextSnapshot = require(file);
312
+ }catch(_){
313
+ console.log(`Error - Cannot read context snapshot at '${file}'.`);
314
+ return;
299
315
  }
300
- else{
301
- console.log("Error - Cannot read or find Context file");
316
+ var searchMigration = `${contextSnapshot.migrationFolder}/**/*_migration.js`;
317
+ var migrationFiles = globSearch.sync(searchMigration, contextSnapshot.migrationFolder);
318
+ if(!(migrationFiles && migrationFiles.length)){
319
+ console.log("No migration files found.");
320
+ return;
321
+ }
322
+ var sorted = migrationFiles.slice().sort((a,b) => __getMigrationTimestamp(a) - __getMigrationTimestamp(b));
323
+ // Print relative names for readability
324
+ for(const f of sorted){
325
+ console.log(path.basename(f));
302
326
  }
303
327
  });
304
328
 
@@ -309,10 +333,102 @@ program.option('-V', 'output the version');
309
333
  .description('Apply pending migrations to database - down method call')
310
334
  .action(function(migrationFileName){
311
335
  // this will call all the down methods until it gets to the one your looking for. First it needs to validate that there is such a file.
312
- // TODO:
313
-
314
- });
336
+ var executedLocation = process.cwd();
337
+ var migration = new Migration();
338
+ try{
339
+ // Accept either a bare filename or a path; normalize to basename
340
+ var targetName = path.basename(migrationFileName);
341
+
342
+ // Locate the target migration file anywhere under the current folder
343
+ var searchTarget = `${executedLocation}/**/${targetName}`;
344
+ var targetMatches = globSearch.sync(searchTarget, executedLocation);
345
+ if(!(targetMatches && targetMatches.length)){
346
+ console.log(`Error - Cannot read or find migration file '${targetName}' in '${executedLocation}'.`);
347
+ return;
348
+ }
349
+ var targetFilePath = targetMatches[0];
350
+ var migrationFolder = path.dirname(targetFilePath);
351
+
352
+ // Find the context snapshot within the same migrations folder
353
+ var snapshotMatches = globSearch.sync(`${migrationFolder}/**/*_contextSnapShot.json`, migrationFolder);
354
+ var snapshotFile = snapshotMatches && snapshotMatches[0];
355
+ if(!snapshotFile){
356
+ console.log("Error - Cannot read or find Context snapshot in migration folder.");
357
+ return;
358
+ }
315
359
 
360
+ var contextSnapshot;
361
+ try{
362
+ contextSnapshot = require(snapshotFile);
363
+ }catch(_){
364
+ console.log(`Error - Cannot read context snapshot at '${snapshotFile}'.`);
365
+ return;
366
+ }
367
+
368
+ // Get all migration files in this folder
369
+ var allMigrationFiles = globSearch.sync(`${migrationFolder}/**/*_migration.js`, migrationFolder);
370
+ if(!(allMigrationFiles && allMigrationFiles.length)){
371
+ console.log("Error - Cannot read or find migration file");
372
+ return;
373
+ }
374
+
375
+ // Sort chronologically
376
+ var sorted = allMigrationFiles.slice().sort(function(a, b){
377
+ return __getMigrationTimestamp(a) - __getMigrationTimestamp(b);
378
+ });
379
+
380
+ // Find target index by basename match
381
+ var targetIndex = sorted.findIndex(function(f){ return path.basename(f) === targetName; });
382
+ if(targetIndex === -1){
383
+ console.log(`Error - Target migration '${targetName}' not found.`);
384
+ return;
385
+ }
386
+
387
+ // Prepare context and table object
388
+ let ContextCtor;
389
+ try{
390
+ ContextCtor = require(contextSnapshot.contextLocation);
391
+ }catch(_){
392
+ console.log(`Error - Cannot load Context file at '${contextSnapshot.contextLocation}'.`);
393
+ return;
394
+ }
395
+ var contextInstance;
396
+ try{
397
+ contextInstance = new ContextCtor();
398
+ }catch(_){
399
+ console.log(`Error - Failed to construct Context from '${contextSnapshot.contextLocation}'.`);
400
+ return;
401
+ }
402
+ var cleanEntities = migration.cleanEntities(contextInstance.__entities);
403
+ var tableObj = migration.buildUpObject(contextSnapshot.schema, cleanEntities);
404
+
405
+ // Roll back (down) all migrations newer than the target (i.e., strictly after targetIndex)
406
+ for (var i = sorted.length - 1; i > targetIndex; i--) {
407
+ var migFile = sorted[i];
408
+ var MigCtor = require(migFile);
409
+ var migInstance = new MigCtor(ContextCtor);
410
+ if(typeof migInstance.down === 'function'){
411
+ migInstance.down(tableObj);
412
+ } else {
413
+ console.log(`Warning - Migration '${path.basename(migFile)}' has no down method; skipping.`);
414
+ }
415
+ }
416
+
417
+ // Update snapshot
418
+ var snap = {
419
+ file : contextSnapshot.contextLocation,
420
+ executedLocation : executedLocation,
421
+ context : contextInstance,
422
+ contextEntities : cleanEntities,
423
+ contextFileName: path.basename(snapshotFile).replace('_contextSnapShot.json','')
424
+ }
425
+ migration.createSnapShot(snap);
426
+ console.log("database updated");
427
+
428
+ }catch (e){
429
+ console.log("Error - Cannot read or find file ", e);
430
+ }
431
+ });
316
432
 
317
433
 
318
434
  program.parse(process.argv);
package/package.json CHANGED
@@ -9,7 +9,7 @@
9
9
  "app-root-path": "^3.1.0",
10
10
  "better-sqlite3": "^12.4.1"
11
11
  },
12
- "version": "0.2.6",
12
+ "version": "0.2.7",
13
13
  "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 ",
14
14
  "homepage": "https://github.com/Tailor/MasterRecord#readme",
15
15
  "repository": {