-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathloopback-db-migrate.js
executable file
·195 lines (171 loc) · 6.86 KB
/
loopback-db-migrate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env node
'use strict';
var fs = require('fs'),
prompt = require('cli-prompt'),
dbNameFlag = process.argv.indexOf('--datasource'),
dbName = (dbNameFlag > -1) ? process.argv[dbNameFlag + 1] : 'db',
dateSinceFlag = process.argv.indexOf('--since'),
dateSinceFilter = (dateSinceFlag > -1) ? process.argv[dateSinceFlag + 1] : '',
migrationsFolderFlag = process.argv.indexOf('--directory'),
migrationsFolder = process.cwd() + ( migrationsFolderFlag > -1 ? process.argv[migrationsFolderFlag + 1].replace(/\/?$/, '/') : '/server/migrations/'),
dbMigrationsFolder = migrationsFolder+dbName,
datasource = require(process.cwd() + '/server/server.js').dataSources[dbName];
if (!datasource) {
console.log('datasource \'' + dbName + '\' not found!');
process.exit(1);
}
datasource.createModel('Migration', {
"name": {
"id": true,
"type": "String",
"required": true,
"length": 100
},
"db": {
"type": "String",
"length": 100,
"required": true
},
"runDtTm": {
"type": "Date",
"required": true
}
});
// make migration folders if they don't exist
try {
fs.mkdirSync(migrationsFolder);
} catch (e) {}
try {
fs.mkdirSync(dbMigrationsFolder);
} catch (e) {}
function mapScriptObjName(scriptObj){
return scriptObj.name;
}
function findScriptsToRun(upOrDown, cb) {
var filters = {
where: {
name: { gte: dateSinceFilter+'' || '' }
},
order: (upOrDown === 'up' ) ? 'name ASC' : 'name DESC'
};
// get all local scripts and filter for only .js files
var localScriptNames = fs.readdirSync(dbMigrationsFolder).filter(function(fileName) {
return fileName.substring(fileName.length - 3, fileName.length) === '.js';
});
// create table if not exists
datasource.autoupdate('Migration', function (err) {
if (err) {
console.log('Error retrieving migrations:');
console.log(err.stack);
process.exit(1);
}
// get all scripts that have been run from DB
datasource.models.Migration.find(filters, function (err, scriptsRun) {
if (err) {
console.log('Error retrieving migrations:');
console.log(err.stack);
process.exit(1);
}
if (upOrDown === 'up') {
var runScriptsNames = scriptsRun.map(mapScriptObjName);
// return scripts that exist on disk but not in the db
cb(localScriptNames.filter(function (scriptName) {
return runScriptsNames.indexOf(scriptName) < 0;
}));
} else {
// return all db script names
cb(scriptsRun.map(mapScriptObjName));
}
});
});
}
function migrateScripts(upOrDown) {
return function findAndRunScripts() {
findScriptsToRun(upOrDown, function runScripts(scriptsToRun) {
var migrationCallStack = [],
migrationCallIndex = 0;
scriptsToRun.forEach(function (localScriptName) {
migrationCallStack.push(function () {
// keep calling scripts recursively until we are done, then exit
function runNextScript(err) {
if (err) {
console.log('Error saving migration', localScriptName, 'to database!');
console.log(err.stack);
process.exit(1);;
}
console.log(localScriptName, 'finished sucessfully.');
migrationCallIndex++;
if (migrationCallIndex < migrationCallStack.length) {
migrationCallStack[migrationCallIndex]();
} else {
process.exit();
}
}
try {
// include the script, run the up/down function, update the migrations table, and continue
console.log(localScriptName, 'running.');
require(dbMigrationsFolder + '/' + localScriptName)[upOrDown](datasource, function (err) {
if (err) {
console.log(localScriptName, 'error:');
console.log(err.stack);
process.exit(1);
} else if (upOrDown === 'up') {
datasource.models.Migration.create({
name: localScriptName,
db: dbName,
runDtTm: new Date()
}, runNextScript);
} else {
datasource.models.Migration.destroyAll({
name: localScriptName
}, runNextScript);
}
});
} catch (e) {
console.log('Error running migration', localScriptName);
console.log(e.stack);
process.exit(1);
}
});
});
// kick off recursive calls
if (migrationCallStack.length) {
migrationCallStack[migrationCallIndex]();
} else {
console.log('No new migrations to run.');
process.exit();
}
});
}
}
function stringifyAndPadLeading(num) {
var str = num + '';
return (str.length === 1) ? '0' + str : str;
}
var cmds = {
up: migrateScripts('up'),
down: migrateScripts('down'),
create: function create(name) {
var cmdLineName = name || process.argv[process.argv.indexOf('create') + 1];
if (!cmdLineName) {
return prompt('Enter migration script name:', create);
}
var d = new Date(),
year = d.getFullYear() + '',
month = stringifyAndPadLeading(d.getMonth()+1),
day = stringifyAndPadLeading(d.getDate()),
hours = stringifyAndPadLeading(d.getHours()),
minutes = stringifyAndPadLeading(d.getMinutes()),
seconds = stringifyAndPadLeading(d.getSeconds()),
dateString = year + month + day + hours + minutes + seconds,
fileName = '/' + dateString + (cmdLineName && cmdLineName.indexOf('--') === -1 ? '-' + cmdLineName : '') + '.js';
fs.writeFileSync(dbMigrationsFolder + fileName, fs.readFileSync(__dirname + '/migration-skeleton.js'));
process.exit();
}
};
var cmdNames = Object.keys(cmds);
for ( var i = 0 ; i < cmdNames.length; i++ ) {
if (process.argv.indexOf(cmdNames[i]) > -1) {
return cmds[cmdNames[i]]();
}
}