masterrecord 0.0.32 → 0.0.33
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 +12 -12
- package/Migrations/migrationSQLiteQuery.js +62 -66
- package/Migrations/migrationTemplate.js +25 -21
- package/Migrations/migrations.js +139 -100
- package/Migrations/schema.js +24 -27
- package/package.json +1 -1
- package/Migrations/schemaTemplate.js +0 -1
- package/Migrations/test.js +0 -34
package/Migrations/cli.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
// version 0.0.
|
|
3
|
+
// version 0.0.5
|
|
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
|
|
|
@@ -9,7 +9,6 @@ let fs = require('fs');
|
|
|
9
9
|
let path = require('path');
|
|
10
10
|
var Migration = require('./migrations');
|
|
11
11
|
var globSearch = require("glob");
|
|
12
|
-
const { table } = require('console');
|
|
13
12
|
|
|
14
13
|
const [,, ...args] = process.argv
|
|
15
14
|
|
|
@@ -17,7 +16,7 @@ const [,, ...args] = process.argv
|
|
|
17
16
|
|
|
18
17
|
program
|
|
19
18
|
.version('0.0.2')
|
|
20
|
-
.option('-v, --version', '0.0.
|
|
19
|
+
.option('-v, --version', '0.0.33')
|
|
21
20
|
.description('A ORM framework that facilitates the creation and use of business objects whose data requires persistent storage to a database');
|
|
22
21
|
|
|
23
22
|
// Instructions : to run command you must go to main project folder is located and run the command using the context file name.
|
|
@@ -31,12 +30,13 @@ program
|
|
|
31
30
|
// location of folder where command is being executed..
|
|
32
31
|
var executedLocation = process.cwd();
|
|
33
32
|
// find context file from main folder location
|
|
34
|
-
var contextInstance = migration.
|
|
35
|
-
|
|
33
|
+
var contextInstance = migration.findContext(executedLocation, contextFileName);
|
|
34
|
+
var context = new contextInstance.context();
|
|
36
35
|
var snap = {
|
|
37
36
|
file : contextInstance.fileLocation,
|
|
38
37
|
executedLocation : executedLocation,
|
|
39
|
-
context :
|
|
38
|
+
context : context,
|
|
39
|
+
contextEntities : [],
|
|
40
40
|
contextFileName: contextFileName.toLowerCase()
|
|
41
41
|
}
|
|
42
42
|
|
|
@@ -62,7 +62,7 @@ program
|
|
|
62
62
|
var contextSnapshot = require(files[0]);
|
|
63
63
|
var context = require(contextSnapshot.contextLocation);
|
|
64
64
|
var contextInstance = new context();
|
|
65
|
-
var newEntity = migration.
|
|
65
|
+
var newEntity = migration.template(name, contextSnapshot.schema, contextInstance .__entities);
|
|
66
66
|
var migrationDate = Date.now();
|
|
67
67
|
var file = `${contextSnapshot.migrationFolder}/${migrationDate}_${name}_migration.js`
|
|
68
68
|
fs.writeFile(file, newEntity, 'utf8', function (err) {
|
|
@@ -107,15 +107,15 @@ program
|
|
|
107
107
|
var context = require(contextSnapshot.contextLocation);
|
|
108
108
|
var contextInstance = new context();
|
|
109
109
|
var newMigrationInstance = new migrationFile(context);
|
|
110
|
-
|
|
111
|
-
var tableObj = migration.
|
|
110
|
+
|
|
111
|
+
var tableObj = migration.up(contextSnapshot.schema, contextInstance.__entities);
|
|
112
112
|
newMigrationInstance.up(tableObj);
|
|
113
|
-
|
|
114
|
-
|
|
113
|
+
|
|
115
114
|
var snap = {
|
|
116
|
-
file :
|
|
115
|
+
file : contextSnapshot.contextLocation,
|
|
117
116
|
executedLocation : executedLocation,
|
|
118
117
|
context : contextInstance,
|
|
118
|
+
contextEntities : contextInstance.__entities,
|
|
119
119
|
contextFileName: contextFileName
|
|
120
120
|
}
|
|
121
121
|
|
|
@@ -1,9 +1,63 @@
|
|
|
1
1
|
|
|
2
|
-
// verison 0.0.
|
|
2
|
+
// verison 0.0.3
|
|
3
3
|
class migrationSQLiteQuery {
|
|
4
4
|
|
|
5
|
-
tempTableName = "_temp_alter_column_update"
|
|
5
|
+
#tempTableName = "_temp_alter_column_update"
|
|
6
|
+
|
|
7
|
+
#getTableColumns(table){
|
|
8
|
+
var columnList = [];
|
|
9
|
+
for (var key in table) {
|
|
10
|
+
if(typeof table[key] === "object"){
|
|
11
|
+
columnList.push(table[key].name);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return columnList.join(',');;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
#columnMapping(table){
|
|
18
|
+
/*
|
|
19
|
+
var mapping = {
|
|
20
|
+
"name": "id", // if this chnages then call rename column
|
|
21
|
+
"type": "integer", // if this changes then call altercolumn
|
|
22
|
+
"primary": false, // is primary key
|
|
23
|
+
"nullable": false, // is nullable
|
|
24
|
+
"unique": true, // vlaue has to be uniqe
|
|
25
|
+
"auto": true, // sets the value to AUTOINCREMENT
|
|
26
|
+
"cascadeOnDelete": true,
|
|
27
|
+
"lazyLoading": true,
|
|
28
|
+
"isNavigational": false
|
|
6
29
|
|
|
30
|
+
}
|
|
31
|
+
*/
|
|
32
|
+
// name TEXT NOT NULL,
|
|
33
|
+
|
|
34
|
+
var auto = table.auto ? " AUTOINCREMENT":"";
|
|
35
|
+
var primaryKey = table.primary ? " PRIMARY KEY" : "";
|
|
36
|
+
var nullName = table.nullable ? "" : " NOT NULL";
|
|
37
|
+
var unique = table.unique ? " UNIQUE" : "";
|
|
38
|
+
var type = this.#typeManager(table.type);
|
|
39
|
+
|
|
40
|
+
return `${table.name} ${type}${nullName}${unique}${primaryKey}${auto}`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
#typeManager(type){
|
|
44
|
+
switch(type) {
|
|
45
|
+
case "string":
|
|
46
|
+
return "TEXT"
|
|
47
|
+
break;
|
|
48
|
+
case "time":
|
|
49
|
+
return "TEXT"
|
|
50
|
+
break;
|
|
51
|
+
case "boolean":
|
|
52
|
+
return "INTEGER"
|
|
53
|
+
break;
|
|
54
|
+
case "integer":
|
|
55
|
+
return "INTEGER"
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
}
|
|
60
|
+
|
|
7
61
|
alterColumn(fullTable, table){
|
|
8
62
|
if(table){
|
|
9
63
|
table.newName = this.tempTableName;
|
|
@@ -34,11 +88,6 @@ class migrationSQLiteQuery {
|
|
|
34
88
|
*/
|
|
35
89
|
}
|
|
36
90
|
|
|
37
|
-
insertInto(name, table){
|
|
38
|
-
return `INSERT INTO ${name} (${this.getTableColumns(table)})
|
|
39
|
-
SELECT ${this.getTableColumns(table)} FROM ${this.tempTableName}`;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
91
|
dropColumn(table){
|
|
43
92
|
/*
|
|
44
93
|
COLUMNS CANNOT BE DROPPED - RULES
|
|
@@ -49,51 +98,20 @@ class migrationSQLiteQuery {
|
|
|
49
98
|
return `ALTER TABLE ${table.tableName} DROP COLUMN ${table.name}`
|
|
50
99
|
}
|
|
51
100
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
"name": "id", // if this chnages then call rename column
|
|
56
|
-
"type": "integer", // if this changes then call altercolumn
|
|
57
|
-
"primary": false, // is primary key
|
|
58
|
-
"nullable": false, // is nullable
|
|
59
|
-
"unique": true, // vlaue has to be uniqe
|
|
60
|
-
"auto": true, // sets the value to AUTOINCREMENT
|
|
61
|
-
"cascadeOnDelete": true,
|
|
62
|
-
"lazyLoading": true,
|
|
63
|
-
"isNavigational": false
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
*/
|
|
67
|
-
// name TEXT NOT NULL,
|
|
68
|
-
|
|
69
|
-
var auto = table.auto ? " AUTOINCREMENT":"";
|
|
70
|
-
var primaryKey = table.primary ? " PRIMARY KEY" : "";
|
|
71
|
-
var nullName = table.nullable ? "" : " NOT NULL";
|
|
72
|
-
var unique = table.unique ? " UNIQUE" : "";
|
|
73
|
-
var type = this.typeManager(table.type);
|
|
74
|
-
|
|
75
|
-
return `${table.name} ${type}${nullName}${unique}${primaryKey}${auto}`;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
getTableColumns(table){
|
|
79
|
-
var columnList = [];
|
|
80
|
-
for (var key in table) {
|
|
81
|
-
if(typeof table[key] === "object"){
|
|
82
|
-
columnList.push(table[key].name);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
return columnList.join(',');;
|
|
101
|
+
insertInto(name, table){
|
|
102
|
+
return `INSERT INTO ${name} (${this.#getTableColumns(table)})
|
|
103
|
+
SELECT ${this.#getTableColumns(table)} FROM ${this.tempTableName}`;
|
|
86
104
|
}
|
|
87
105
|
|
|
88
|
-
createTable(
|
|
106
|
+
createTable(table){
|
|
89
107
|
var queryVar = "";
|
|
90
108
|
for (var key in table) {
|
|
91
109
|
if(typeof table[key] === "object"){
|
|
92
|
-
queryVar += `${this
|
|
110
|
+
queryVar += `${this.#columnMapping(table[key])}, `;
|
|
93
111
|
}
|
|
94
112
|
}
|
|
95
113
|
|
|
96
|
-
return `CREATE TABLE ${
|
|
114
|
+
return `CREATE TABLE ${table.__name} (${queryVar.replace(/,\s*$/, "")});`;
|
|
97
115
|
|
|
98
116
|
/*
|
|
99
117
|
INTEGER PRIMARY KEY AUTOINCREMENT
|
|
@@ -115,10 +133,6 @@ class migrationSQLiteQuery {
|
|
|
115
133
|
return `DROP TABLE ${name}`
|
|
116
134
|
}
|
|
117
135
|
|
|
118
|
-
dropIndex(){
|
|
119
|
-
|
|
120
|
-
}
|
|
121
|
-
|
|
122
136
|
renameTable(table){
|
|
123
137
|
return `ALTER TABLE ${table.tableName} RENAME TO ${table.newName}`;
|
|
124
138
|
}
|
|
@@ -127,24 +141,6 @@ class migrationSQLiteQuery {
|
|
|
127
141
|
return `ALTER TABLE ${table.tableName} RENAME COLUMN ${table.name} TO ${table.newName}`
|
|
128
142
|
}
|
|
129
143
|
|
|
130
|
-
typeManager(type){
|
|
131
|
-
switch(type) {
|
|
132
|
-
case "string":
|
|
133
|
-
return "TEXT"
|
|
134
|
-
break;
|
|
135
|
-
case "time":
|
|
136
|
-
return "TEXT"
|
|
137
|
-
break;
|
|
138
|
-
case "boolean":
|
|
139
|
-
return "INTEGER"
|
|
140
|
-
break;
|
|
141
|
-
case "integer":
|
|
142
|
-
return "INTEGER"
|
|
143
|
-
break;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
}
|
|
147
|
-
|
|
148
144
|
|
|
149
145
|
}
|
|
150
146
|
|
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
2
|
|
|
3
3
|
// https://channel9.msdn.com/Blogs/EF/Migrations-Under-the-Hood
|
|
4
|
+
// version 0.0.3
|
|
4
5
|
|
|
6
|
+
const os = require('os');
|
|
5
7
|
class MigrationTemplate {
|
|
6
|
-
|
|
7
|
-
down = ''
|
|
8
|
+
|
|
8
9
|
constructor(name) {
|
|
9
|
-
|
|
10
|
+
this.name = name;
|
|
10
11
|
}
|
|
11
12
|
|
|
13
|
+
#up = ''
|
|
14
|
+
#down = ''
|
|
15
|
+
|
|
12
16
|
get(){
|
|
13
17
|
return `
|
|
14
18
|
|
|
@@ -20,61 +24,61 @@ class ${this.name} extends masterrecord.schema {
|
|
|
20
24
|
}
|
|
21
25
|
|
|
22
26
|
up(table){
|
|
23
|
-
this.
|
|
24
|
-
${this
|
|
27
|
+
this.init(table);
|
|
28
|
+
${this.#up}
|
|
25
29
|
}
|
|
26
30
|
|
|
27
31
|
down(table){
|
|
28
|
-
this.
|
|
29
|
-
${this
|
|
32
|
+
this.init(table);
|
|
33
|
+
${this.#down}
|
|
30
34
|
}
|
|
31
35
|
}
|
|
32
36
|
module.exports = ${this.name};
|
|
33
37
|
`
|
|
34
38
|
}
|
|
35
39
|
|
|
36
|
-
alterColumn(type, name){
|
|
40
|
+
alterColumn(type, name, parent){
|
|
37
41
|
if(type === "up"){
|
|
38
|
-
this
|
|
42
|
+
this.#up += os.EOL + ` this.alterColumn(table.${parent}.${name});`
|
|
39
43
|
}
|
|
40
44
|
else{
|
|
41
|
-
this
|
|
45
|
+
this.#down += os.EOL + ` this.alterColumn(table.${parent}.${name});`
|
|
42
46
|
}
|
|
43
47
|
}
|
|
44
48
|
createTable(type, name){
|
|
45
49
|
if(type === "up"){
|
|
46
|
-
this
|
|
50
|
+
this.#up += os.EOL + ` this.createTable(table.${name});`
|
|
47
51
|
}
|
|
48
52
|
else{
|
|
49
|
-
this
|
|
53
|
+
this.#down += os.EOL + ` this.createTable(table.${name});`
|
|
50
54
|
}
|
|
51
55
|
}
|
|
52
56
|
|
|
53
|
-
addColumn(type, name){
|
|
57
|
+
addColumn(type, name, parent){
|
|
54
58
|
if(type === "up"){
|
|
55
|
-
this
|
|
59
|
+
this.#up += os.EOL + ` this.addColumn(table.${parent}.${name});`
|
|
56
60
|
}
|
|
57
61
|
else{
|
|
58
|
-
this
|
|
62
|
+
this.#down += os.EOL + ` this.addColumn(table.${parent}.${name});`
|
|
59
63
|
}
|
|
60
64
|
}
|
|
61
65
|
|
|
62
66
|
|
|
63
67
|
dropTable(type, name){
|
|
64
68
|
if(type === "up"){
|
|
65
|
-
this
|
|
69
|
+
this.#down += os.EOL + ` this.droptable(table.${name});`
|
|
66
70
|
}
|
|
67
71
|
else{
|
|
68
|
-
this
|
|
72
|
+
this.#down += os.EOL + ` this.droptable(table.${name});`
|
|
69
73
|
}
|
|
70
74
|
}
|
|
71
75
|
|
|
72
|
-
dropColumn(type, name){
|
|
76
|
+
dropColumn(type, name, parent){
|
|
73
77
|
if(type === "up"){
|
|
74
|
-
this
|
|
78
|
+
this.#up += os.EOL + ` this.dropColumn(table.${parent}.${name});`
|
|
75
79
|
}
|
|
76
80
|
else{
|
|
77
|
-
this
|
|
81
|
+
this.#down += os.EOL + ` this.dropColumn(table.${parent}.${name});`
|
|
78
82
|
}
|
|
79
83
|
}
|
|
80
84
|
|
package/Migrations/migrations.js
CHANGED
|
@@ -1,77 +1,59 @@
|
|
|
1
|
-
// version 0.0.
|
|
1
|
+
// version 0.0.5
|
|
2
2
|
// learn more about seeding info - https://www.pauric.blog/Database-Updates-and-Migrations-with-Entity-Framework/
|
|
3
3
|
|
|
4
4
|
var fs = require('fs');
|
|
5
5
|
var diff = require("deep-object-diff");
|
|
6
6
|
var MigrationTemplate = require("./migrationTemplate");
|
|
7
7
|
var globSearch = require("glob");
|
|
8
|
-
const { table } = require('console');
|
|
9
8
|
|
|
10
9
|
// https://blog.tekspace.io/code-first-multiple-db-context-migration/
|
|
11
10
|
|
|
12
11
|
// node masterrecord add-migration josh C:\Users\rbatista\Downloads\kollege\freshmen\app\models\context
|
|
13
12
|
class Migrations{
|
|
14
13
|
|
|
15
|
-
|
|
16
|
-
var search = `${executedLocation}/**/*${contextFileName}.js`
|
|
17
|
-
var files = globSearch.sync(search, executedLocation);
|
|
18
|
-
var file = files[0];
|
|
19
|
-
var context = require(file);
|
|
20
|
-
return {
|
|
21
|
-
context : context,
|
|
22
|
-
fileLocation : file
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
createSnapShot(snap){
|
|
27
|
-
var migrationsDirectory = `${snap.executedLocation}/db/migrations`;
|
|
28
|
-
if (!fs.existsSync(migrationsDirectory)){
|
|
29
|
-
fs.mkdirSync(migrationsDirectory);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
var content = {
|
|
33
|
-
contextLocation: snap.file,
|
|
34
|
-
migrationFolder: `${snap.executedLocation}/db/migrations`,
|
|
35
|
-
snapShotLocation: `${snap.executedLocation}/db/migrations/${snap.contextFileName}_contextSnapShot.json`,
|
|
36
|
-
schema : snap.context.__entities
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
const jsonContent = JSON.stringify(content, null, 2);
|
|
40
|
-
try{
|
|
41
|
-
// will replace the whole file if it exist
|
|
42
|
-
fs.writeFileSync(`${migrationsDirectory}/${snap.contextFileName}_contextSnapShot.json`, jsonContent);
|
|
43
|
-
}catch (e){
|
|
44
|
-
console.log("Cannot write file ", e);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
organizeSchemaByTables(oldSchema, newSchema){
|
|
14
|
+
#organizeSchemaByTables(oldSchema, newSchema){
|
|
49
15
|
var tables = []
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
oldSchema.forEach(function (oldItem, index) {
|
|
63
|
-
var oldItemName = oldItem["__name"];
|
|
64
|
-
if(table.name === oldItemName){
|
|
65
|
-
table.old = oldItem;
|
|
66
|
-
tables.push(table);
|
|
16
|
+
if(oldSchema.length === 0){
|
|
17
|
+
newSchema.forEach(function (item, index) {
|
|
18
|
+
var table = {
|
|
19
|
+
name: item["__name"],
|
|
20
|
+
new :item,
|
|
21
|
+
old : {},
|
|
22
|
+
newColumns : [],
|
|
23
|
+
newTables : [],
|
|
24
|
+
deletedColumns : [],
|
|
25
|
+
updatedColumns : []
|
|
67
26
|
}
|
|
27
|
+
tables.push(table);
|
|
68
28
|
});
|
|
69
|
-
}
|
|
29
|
+
}
|
|
30
|
+
else{
|
|
31
|
+
newSchema.forEach(function (item, index) {
|
|
32
|
+
var table = {
|
|
33
|
+
name: item["__name"],
|
|
34
|
+
old: null,
|
|
35
|
+
new :item,
|
|
36
|
+
newColumns : [],
|
|
37
|
+
newTables : [],
|
|
38
|
+
deletedColumns : [],
|
|
39
|
+
updatedColumns : []
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
oldSchema.forEach(function (oldItem, index) {
|
|
43
|
+
var oldItemName = oldItem["__name"];
|
|
44
|
+
if(table.name === oldItemName){
|
|
45
|
+
table.old = oldItem;
|
|
46
|
+
tables.push(table);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
});
|
|
51
|
+
}
|
|
70
52
|
|
|
71
53
|
return tables;
|
|
72
54
|
}
|
|
73
55
|
|
|
74
|
-
|
|
56
|
+
#findDeletedColumns(tables){
|
|
75
57
|
tables.forEach(function (item, index) {
|
|
76
58
|
var deletedColumn = null;
|
|
77
59
|
if(item.new && item.old){
|
|
@@ -93,7 +75,7 @@ class Migrations{
|
|
|
93
75
|
return tables;
|
|
94
76
|
}
|
|
95
77
|
|
|
96
|
-
findUpdatedColumns(tables){
|
|
78
|
+
#findUpdatedColumns(tables){
|
|
97
79
|
tables.forEach(function (item, index) {
|
|
98
80
|
var UD = diff.updatedDiff(item.old, item.new);
|
|
99
81
|
const isEmpty = Object.keys(UD).length === 0;
|
|
@@ -112,49 +94,114 @@ class Migrations{
|
|
|
112
94
|
return tables;
|
|
113
95
|
}
|
|
114
96
|
|
|
115
|
-
|
|
97
|
+
#findNewColumns(tables){
|
|
116
98
|
tables.forEach(function (item, index) {
|
|
117
|
-
var newColumn = null;
|
|
118
99
|
if(item.new && item.old){
|
|
119
100
|
Object.keys(item.new).forEach(function (key) {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
101
|
+
if(typeof item.new[key] === "object"){
|
|
102
|
+
var value = item.new[key].name;
|
|
103
|
+
var columnNotFound = false;
|
|
104
|
+
Object.keys(item.old).forEach(function (oldKey) {
|
|
105
|
+
if(typeof item.old[oldKey] === "object"){
|
|
106
|
+
var oldValue = item.old[oldKey].name;
|
|
107
|
+
if(value === oldValue){
|
|
108
|
+
columnNotFound = true;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
if(columnNotFound === false){
|
|
114
|
+
// this means it did not find the column
|
|
115
|
+
item.newColumns.push(value);
|
|
126
116
|
}
|
|
127
|
-
});
|
|
128
|
-
if(newColumn === null){
|
|
129
|
-
item.newColumns.push(value);
|
|
130
117
|
}
|
|
118
|
+
|
|
131
119
|
});
|
|
132
120
|
}
|
|
121
|
+
else{
|
|
122
|
+
console.log("Table object has no old or new values");
|
|
123
|
+
}
|
|
133
124
|
});
|
|
134
125
|
return tables;
|
|
135
126
|
}
|
|
136
127
|
|
|
128
|
+
#findNewTables(tables){
|
|
129
|
+
// find new tables
|
|
130
|
+
tables.forEach(function (item, index) {
|
|
131
|
+
if(item.new && item.old){
|
|
132
|
+
if(Object.keys(item.old).length === 0){
|
|
133
|
+
item.newTables.push(item);
|
|
134
|
+
}
|
|
135
|
+
}else{
|
|
136
|
+
console.log("Cannot find NEW or and Old Objects");
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
});
|
|
140
|
+
return tables;
|
|
141
|
+
}
|
|
142
|
+
|
|
137
143
|
// build table to build new migration snapshot
|
|
138
|
-
buildMigrationObject(oldSchema, newSchema){
|
|
139
|
-
var tables = this
|
|
140
|
-
tables = this
|
|
141
|
-
tables = this
|
|
142
|
-
tables = this
|
|
144
|
+
#buildMigrationObject(oldSchema, newSchema){
|
|
145
|
+
var tables = this.#organizeSchemaByTables(oldSchema, newSchema);
|
|
146
|
+
tables = this.#findNewTables(tables);
|
|
147
|
+
tables = this.#findNewColumns(tables);
|
|
148
|
+
tables = this.#findDeletedColumns(tables);
|
|
149
|
+
tables = this.#findUpdatedColumns(tables);
|
|
143
150
|
return tables;
|
|
144
151
|
}
|
|
145
152
|
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
findContext(executedLocation, contextFileName){
|
|
156
|
+
var search = `${executedLocation}/**/*${contextFileName}.js`
|
|
157
|
+
var files = globSearch.sync(search, executedLocation);
|
|
158
|
+
var file = files[0];
|
|
159
|
+
var context = require(file);
|
|
160
|
+
return {
|
|
161
|
+
context : context,
|
|
162
|
+
fileLocation : file
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
createSnapShot(snap){
|
|
167
|
+
var migrationsDirectory = `${snap.executedLocation}/db/migrations`;
|
|
168
|
+
if (!fs.existsSync(migrationsDirectory)){
|
|
169
|
+
fs.mkdirSync(migrationsDirectory);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
var content = {
|
|
173
|
+
contextLocation: snap.file,
|
|
174
|
+
migrationFolder: `${snap.executedLocation}/db/migrations`,
|
|
175
|
+
snapShotLocation: `${snap.executedLocation}/db/migrations/${snap.contextFileName}_contextSnapShot.json`,
|
|
176
|
+
schema : snap.contextEntities
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const jsonContent = JSON.stringify(content, null, 2);
|
|
180
|
+
try{
|
|
181
|
+
// will replace the whole file if it exist
|
|
182
|
+
fs.writeFileSync(`${migrationsDirectory}/${snap.contextFileName}_contextSnapShot.json`, jsonContent);
|
|
183
|
+
}catch (e){
|
|
184
|
+
console.log("Cannot write file ", e);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
146
188
|
//
|
|
147
|
-
|
|
189
|
+
up(oldSchema, newSchema){
|
|
148
190
|
var tableObj = {}
|
|
149
|
-
var tables = this
|
|
191
|
+
var tables = this.#buildMigrationObject(oldSchema, newSchema);
|
|
150
192
|
tables.forEach(function (item, index) {
|
|
151
193
|
// add new columns for table
|
|
152
194
|
var columnInfo = tables[index];
|
|
195
|
+
|
|
153
196
|
item.newColumns.forEach(function (column, ind) {
|
|
154
197
|
columnInfo.new[column].tableName = item.name;
|
|
155
198
|
tableObj[column] = columnInfo.new[column];
|
|
156
199
|
});
|
|
157
200
|
|
|
201
|
+
item.newTables.forEach(function (column, ind) {
|
|
202
|
+
tableObj[item.name] = columnInfo.new;
|
|
203
|
+
});
|
|
204
|
+
|
|
158
205
|
item.deletedColumns.forEach(function (column, ind) {
|
|
159
206
|
columnInfo.old[column].tableName = item.name;
|
|
160
207
|
tableObj[column] = columnInfo.old[column];
|
|
@@ -164,65 +211,57 @@ class Migrations{
|
|
|
164
211
|
tableObj[column.table.name] = column;
|
|
165
212
|
});
|
|
166
213
|
|
|
167
|
-
if(item.old === null){
|
|
168
|
-
columnInfo.new.tableName = item.name;
|
|
169
|
-
tableObj[column] = columnInfo.new;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
214
|
if(item.new === null){
|
|
173
215
|
columnInfo.old.tableName = item.name;
|
|
174
216
|
tableObj[column] = columnInfo.old;
|
|
175
217
|
}
|
|
218
|
+
|
|
176
219
|
tableObj.___table = item;
|
|
177
220
|
});
|
|
178
221
|
return tableObj;
|
|
179
222
|
}
|
|
180
223
|
|
|
181
|
-
|
|
182
|
-
|
|
224
|
+
template(name, oldSchema, newSchema){
|
|
183
225
|
var MT = new MigrationTemplate(name);
|
|
184
|
-
var tables = this
|
|
226
|
+
var tables = this.#buildMigrationObject(oldSchema, newSchema);
|
|
185
227
|
tables.forEach(function (item, index) {
|
|
186
228
|
// add new columns for table
|
|
187
229
|
item.newColumns.forEach(function (column, index) {
|
|
188
|
-
MT.addColumn("up", column);
|
|
189
|
-
MT.dropColumn("down", column);
|
|
230
|
+
MT.addColumn("up", column, item.name);
|
|
231
|
+
MT.dropColumn("down", column, item.name);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
item.newTables.forEach(function (column, ind) {
|
|
235
|
+
MT.createTable("up", item.name);
|
|
236
|
+
MT.dropTable("down", item.name);
|
|
190
237
|
});
|
|
191
238
|
|
|
192
239
|
item.deletedColumns.forEach(function (column, index) {
|
|
193
|
-
MT.dropColumn("up", column);
|
|
194
|
-
MT.addColumn("down",column);
|
|
240
|
+
MT.dropColumn("up", column, item.name);
|
|
241
|
+
MT.addColumn("down",column, item.name);
|
|
195
242
|
});
|
|
196
243
|
|
|
197
244
|
item.updatedColumns.forEach(function (column, index) {
|
|
198
245
|
const isEmpty = Object.keys(column).length === 0;
|
|
199
246
|
if(!isEmpty){
|
|
200
|
-
MT.alterColumn("up", column.table.name);
|
|
201
|
-
MT.alterColumn("down", column.table.name);
|
|
247
|
+
MT.alterColumn("up", column.table.name, item.name);
|
|
248
|
+
MT.alterColumn("down", column.table.name, item.name);
|
|
202
249
|
}
|
|
203
250
|
});
|
|
204
251
|
|
|
205
252
|
if(item.old === null){
|
|
206
|
-
MT.createTable("up", column);
|
|
207
|
-
MT.dropTable("down", column);
|
|
253
|
+
MT.createTable("up", column, item.name);
|
|
254
|
+
MT.dropTable("down", column, item.name);
|
|
208
255
|
|
|
209
256
|
}
|
|
210
257
|
if(item.new === null){
|
|
211
|
-
MT.dropTable("up", column);
|
|
212
|
-
MT.createTable("down", column);
|
|
258
|
+
MT.dropTable("up", column, item.name);
|
|
259
|
+
MT.createTable("down", column, item.name);
|
|
213
260
|
}
|
|
214
261
|
|
|
215
262
|
});
|
|
216
263
|
|
|
217
264
|
return MT.get();
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
migrationCodeGenerator(name, column, migrationDate){
|
|
223
|
-
// will create migration file with data needed
|
|
224
|
-
// using the migration template
|
|
225
|
-
|
|
226
265
|
}
|
|
227
266
|
|
|
228
267
|
|
package/Migrations/schema.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
// version
|
|
2
|
-
|
|
1
|
+
// version 0.0.3
|
|
3
2
|
class schema{
|
|
4
3
|
|
|
5
4
|
constructor(context){
|
|
@@ -7,23 +6,24 @@ class schema{
|
|
|
7
6
|
}
|
|
8
7
|
|
|
9
8
|
|
|
10
|
-
|
|
9
|
+
init(table){
|
|
11
10
|
this.fullTable = table.___table;
|
|
12
11
|
}
|
|
13
12
|
|
|
14
13
|
// create obj to convert into create sql
|
|
15
14
|
addColumn(table){
|
|
16
|
-
console.log("----------addColumn ------");
|
|
17
15
|
if(this.context.isSQite){
|
|
18
16
|
var sqliteQuery = require("./migrationSQLiteQuery");
|
|
17
|
+
var queryBuilder = new sqliteQuery();
|
|
18
|
+
var queryObj = queryBuilder.alterColumn(this.fullTable.new, table);
|
|
19
|
+
for (var key in queryObj) {
|
|
20
|
+
var query = queryObj[key];
|
|
21
|
+
this.context._execute(query);
|
|
22
|
+
}
|
|
19
23
|
}
|
|
20
24
|
// add column to database
|
|
21
25
|
}
|
|
22
26
|
|
|
23
|
-
createTable(table){
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
|
|
27
27
|
dropColumn(table){
|
|
28
28
|
if(this.fullTable){
|
|
29
29
|
// drop column
|
|
@@ -37,14 +37,27 @@ class schema{
|
|
|
37
37
|
console.log("Must call the addTable function.");
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
|
+
|
|
41
|
+
createTable(table){
|
|
42
|
+
if(this.context.isSQite){
|
|
43
|
+
var sqliteQuery = require("./migrationSQLiteQuery");
|
|
44
|
+
var queryBuilder = new sqliteQuery();
|
|
45
|
+
var query = queryBuilder.createTable(table);
|
|
46
|
+
this.context._execute(query);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
40
49
|
|
|
41
|
-
dropTable(table){
|
|
42
50
|
|
|
51
|
+
dropTable(table){
|
|
52
|
+
if(this.context.isSQite){
|
|
53
|
+
var sqliteQuery = require("./migrationSQLiteQuery");
|
|
54
|
+
var queryBuilder = new sqliteQuery();
|
|
55
|
+
var query = queryBuilder.dropTable(table.__name);
|
|
56
|
+
this.context._execute(query);
|
|
57
|
+
}
|
|
43
58
|
}
|
|
44
59
|
|
|
45
|
-
dropIndex(){
|
|
46
60
|
|
|
47
|
-
}
|
|
48
61
|
//"dbo.People", "Location"
|
|
49
62
|
alterColumn(table){
|
|
50
63
|
if(this.fullTable){
|
|
@@ -69,24 +82,8 @@ class schema{
|
|
|
69
82
|
seed(){
|
|
70
83
|
|
|
71
84
|
}
|
|
72
|
-
|
|
73
|
-
// will get the data and create the file
|
|
74
|
-
done(){
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
78
85
|
|
|
79
86
|
}
|
|
80
87
|
|
|
81
88
|
|
|
82
|
-
/*
|
|
83
|
-
up and down function..
|
|
84
|
-
on commmand line call of run migrations with folder location of context. it will
|
|
85
|
-
load context and all the objects.
|
|
86
|
-
it will then match objects with migration data.
|
|
87
|
-
if it's a new item it will generate a new migration dependent on what comes from migration.
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
*/
|
|
91
|
-
|
|
92
89
|
module.exports = schema;
|
package/package.json
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
"glob" : "^8.0.3",
|
|
7
7
|
"deep-object-diff" : "^1.1.7"
|
|
8
8
|
},
|
|
9
|
-
"version": "0.0.
|
|
9
|
+
"version": "0.0.33",
|
|
10
10
|
"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 ",
|
|
11
11
|
"homepage": "https://github.com/Tailor/MasterRecord#readme",
|
|
12
12
|
"repository": {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
// will be the template used to create the scheme section
|
package/Migrations/test.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
class temp {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
hatml =
|
|
5
|
-
buildMOdal(className, header, body){
|
|
6
|
-
|
|
7
|
-
`
|
|
8
|
-
Copy
|
|
9
|
-
<div class="modal" tabindex="-1" role="dialog" ${className}>
|
|
10
|
-
<div class="modal-dialog" role="document">
|
|
11
|
-
<div class="modal-content">
|
|
12
|
-
<div class="modal-header">
|
|
13
|
-
${ buildheader(header)}
|
|
14
|
-
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
|
15
|
-
<span aria-hidden="true">×</span>
|
|
16
|
-
</button>
|
|
17
|
-
</div>
|
|
18
|
-
<div class="modal-body">
|
|
19
|
-
${body}
|
|
20
|
-
</div>
|
|
21
|
-
<div class="modal-footer">
|
|
22
|
-
<button type="button" class="btn btn-primary">Save changes</button>
|
|
23
|
-
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
|
|
24
|
-
</div>
|
|
25
|
-
</div>
|
|
26
|
-
</div>
|
|
27
|
-
</div>`
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
buildheader(title){
|
|
32
|
-
return ` <h5 class="modal-title">${title}</h5>`
|
|
33
|
-
}
|
|
34
|
-
}
|