masterrecord 0.2.11 → 0.2.12

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/Migrations/cli.js CHANGED
@@ -576,79 +576,68 @@ program.option('-V', 'output the version');
576
576
  .action(function(){
577
577
  var executedLocation = process.cwd();
578
578
  try{
579
- // Find all Context files (e.g., chatContext.js)
580
- var allFiles = globSearch.sync(`${executedLocation}/**/*Context.js`, executedLocation);
581
- if(!(allFiles && allFiles.length)){
582
- console.log('No Context files found.');
579
+ // Find all context snapshots and run update per snapshot (avoids unrelated framework contexts)
580
+ var snapshotFiles = globSearch.sync(`${executedLocation}/**/*_contextSnapShot.json`, executedLocation);
581
+ if(!(snapshotFiles && snapshotFiles.length)){
582
+ console.log('No context snapshots found. Run enable-migrations for each context first.');
583
583
  return;
584
584
  }
585
- // Deduplicate by basename (case-insensitive)
586
- var seen = new Set();
587
- var contextFiles = [];
588
- for(const f of allFiles){
589
- var name = path.basename(f).toLowerCase();
590
- if(!seen.has(name)){
591
- seen.add(name);
592
- contextFiles.push(f);
593
- }
585
+ // Group snapshots by context name (case-insensitive) and pick best per group
586
+ var groups = {};
587
+ for(const snapFile of snapshotFiles){
588
+ let cs;
589
+ try{ cs = require(snapFile); }catch(_){ continue; }
590
+ const nameFromPath = path.basename(snapFile).replace(/_contextSnapShot\.json$/i, '').toLowerCase();
591
+ const ctxName = (cs && cs.contextLocation)
592
+ ? path.basename(cs.contextLocation).replace(/\.js$/i, '').toLowerCase()
593
+ : nameFromPath;
594
+ const searchMigration = `${cs.migrationFolder}/**/*_migration.js`;
595
+ const migs = globSearch.sync(searchMigration, cs.migrationFolder) || [];
596
+ if(!groups[ctxName]) groups[ctxName] = [];
597
+ groups[ctxName].push({ snapFile, cs, ctxName, migs });
594
598
  }
599
+
595
600
  var migration = new Migration();
596
- // Process each context independently
597
- for(const ctxFile of contextFiles){
601
+ var ctxNames = Object.keys(groups);
602
+ for(const name of ctxNames){
598
603
  try{
599
- var base = path.basename(ctxFile);
600
- var ctxName = base.replace(/\.js$/i, '').toLowerCase();
601
- // Locate snapshot for this context
602
- var snapMatches = globSearch.sync(`${executedLocation}/**/*${ctxName}_contextSnapShot.json`, executedLocation);
603
- var snapFile = snapMatches && snapMatches[0];
604
- if(!snapFile){
605
- console.log(`Skipping ${ctxName}: snapshot not found. Run 'masterrecord enable-migrations ${ctxName}'.`);
606
- continue;
607
- }
608
- var contextSnapshot;
609
- try{ contextSnapshot = require(snapFile); }catch(_){
610
- console.log(`Skipping ${ctxName}: cannot read context snapshot at '${snapFile}'.`);
611
- continue;
612
- }
613
- // Get migration files for this snapshot folder
614
- var searchMigration = `${contextSnapshot.migrationFolder}/**/*_migration.js`;
615
- var migrationFiles = globSearch.sync(searchMigration, contextSnapshot.migrationFolder);
616
- if(!(migrationFiles && migrationFiles.length)){
617
- console.log(`Skipping ${ctxName}: no migration files found.`);
604
+ var list = groups[name];
605
+ // Prefer entries that actually have migration files
606
+ var withMigs = list.filter(e => e.migs && e.migs.length > 0);
607
+ var entry = withMigs.length ? withMigs[withMigs.length - 1] : list[0];
608
+ if(!(entry.migs && entry.migs.length)){
609
+ console.log(`Skipping ${entry.ctxName}: no migration files found.`);
618
610
  continue;
619
611
  }
620
- // Pick latest
621
- var mFiles = migrationFiles.slice().sort(function(a, b){
612
+ var mFiles = entry.migs.slice().sort(function(a, b){
622
613
  return __getMigrationTimestamp(a) - __getMigrationTimestamp(b);
623
614
  });
624
615
  var mFile = mFiles[mFiles.length - 1];
625
616
 
626
617
  var ContextCtor;
627
- try{ ContextCtor = require(contextSnapshot.contextLocation); }catch(_){
628
- console.log(`Skipping ${ctxName}: cannot load Context at '${contextSnapshot.contextLocation}'.`);
618
+ try{ ContextCtor = require(entry.cs.contextLocation); }catch(_){
619
+ console.log(`Skipping ${entry.ctxName}: cannot load Context at '${entry.cs.contextLocation}'.`);
629
620
  continue;
630
621
  }
631
622
  var contextInstance;
632
623
  try{ contextInstance = new ContextCtor(); }catch(_){
633
- console.log(`Skipping ${ctxName}: failed to construct Context.`);
624
+ console.log(`Skipping ${entry.ctxName}: failed to construct Context.`);
634
625
  continue;
635
626
  }
636
627
  var migrationProjectFile = require(mFile);
637
628
  var newMigrationProjectInstance = new migrationProjectFile(ContextCtor);
638
629
  var cleanEntities = migration.cleanEntities(contextInstance.__entities);
639
- var tableObj = migration.buildUpObject(contextSnapshot.schema, cleanEntities);
640
- // Run up for this context
630
+ var tableObj = migration.buildUpObject(entry.cs.schema, cleanEntities);
641
631
  newMigrationProjectInstance.up(tableObj);
642
- // Update snapshot
643
632
  var snap = {
644
- file : contextSnapshot.contextLocation,
633
+ file : entry.cs.contextLocation,
645
634
  executedLocation : executedLocation,
646
635
  context : contextInstance,
647
636
  contextEntities : cleanEntities,
648
- contextFileName: ctxName
637
+ contextFileName: entry.ctxName
649
638
  }
650
639
  migration.createSnapShot(snap);
651
- console.log(`database updated for ${ctxName}`);
640
+ console.log(`database updated for ${entry.ctxName}`);
652
641
  }catch(errCtx){
653
642
  console.log('Error updating context: ', errCtx);
654
643
  }
@@ -15,7 +15,7 @@ class migrationMySQLQuery {
15
15
  }
16
16
  // Map belongsTo to its foreignKey name
17
17
  var name = (col.relationshipType === 'belongsTo' && col.foreignKey) ? col.foreignKey : col.name;
18
- columnList.push(name);
18
+ columnList.push(`\`${name}\``);
19
19
  }
20
20
  }
21
21
  return columnList.join(',');
@@ -63,7 +63,7 @@ class migrationMySQLQuery {
63
63
  }
64
64
  }
65
65
 
66
- return `${tableName} ${type}${nullName}${defaultValue}${unique}${primaryKey}${auto}`;
66
+ return `\`${tableName}\` ${type}${nullName}${defaultValue}${unique}${primaryKey}${auto}`;
67
67
  }
68
68
 
69
69
  boolType(type){
@@ -135,7 +135,7 @@ class migrationMySQLQuery {
135
135
 
136
136
  if(table){
137
137
 
138
- return `ALTER TABLE ${table.tableName} MODIFY COLUMN ${this.#columnMapping(table.table)} `;
138
+ return `ALTER TABLE \`${table.tableName}\` MODIFY COLUMN ${this.#columnMapping(table.table)} `;
139
139
  }
140
140
  else{
141
141
  console.log("table information is null");
@@ -152,8 +152,8 @@ class migrationMySQLQuery {
152
152
 
153
153
 
154
154
  addColum(table){
155
- return `ALTER TABLE ${table.tableName}
156
- ADD ${table.name} ${table.realDataType}`;
155
+ return `ALTER TABLE \`${table.tableName}\`
156
+ ADD \`${table.name}\` ${table.realDataType}`;
157
157
 
158
158
  /*
159
159
  column definations
@@ -172,7 +172,7 @@ class migrationMySQLQuery {
172
172
  is indexed
173
173
  appears in a view
174
174
  */
175
- return `ALTER TABLE ${table.tableName} DROP COLUMN ${table.name}`;
175
+ return `ALTER TABLE \`${table.tableName}\` DROP COLUMN \`${table.name}\``;
176
176
  }
177
177
 
178
178
  insertInto(name, table){
@@ -193,7 +193,7 @@ class migrationMySQLQuery {
193
193
  }
194
194
  }
195
195
 
196
- var completeQuery = `CREATE TABLE IF NOT EXISTS ${table.__name} (${queryVar.replace(/,\s*$/, "")});`;
196
+ var completeQuery = `CREATE TABLE IF NOT EXISTS \`${table.__name}\` (${queryVar.replace(/,\s*$/, "")});`;
197
197
  return completeQuery;
198
198
 
199
199
  /*
@@ -213,15 +213,15 @@ class migrationMySQLQuery {
213
213
 
214
214
 
215
215
  dropTable(name){
216
- return `DROP TABLE ${name}`
216
+ return `DROP TABLE \`${name}\``
217
217
  }
218
218
 
219
219
  renameTable(table){
220
- return `ALTER TABLE ${table.tableName} RENAME TO ${table.newName}`;
220
+ return `ALTER TABLE \`${table.tableName}\` RENAME TO \`${table.newName}\``;
221
221
  }
222
222
 
223
223
  renameColumn(table){
224
- return `ALTER TABLE ${table.tableName} RENAME COLUMN ${table.name} TO ${table.newName}`
224
+ return `ALTER TABLE \`${table.tableName}\` RENAME COLUMN \`${table.name}\` TO \`${table.newName}\``
225
225
  }
226
226
 
227
227
 
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.11",
12
+ "version": "0.2.12",
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": {