-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbootstrap.js
391 lines (350 loc) · 23.8 KB
/
bootstrap.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/**
* Seed Function
* (sails.config.bootstrap)
*
* A function that runs just before your Sails app gets lifted.
* > Need more flexibility? You can also create a hook.
*
* For more information on seeding your app with fake data, check out:
* https://sailsjs.com/config/bootstrap
*/
// Use this for headers: http://patorjk.com/software/taag/#p=display&c=c&f=ANSI%20Shadow&t=Sails
// Use this for subheaders: http://patorjk.com/software/taag/#p=display&c=c&f=Calvin%20S&t=Woot
module.exports.bootstrap = function(next) {
/**
* DO NOT START WITH `sails lift`!
*
* This helps ensure we are "lifting" Sails using either `app.js` or `ngrok.js`.
* Using `sails lift` will bypass config overrides / custom error handlers.
*
* node app.js OR ./app.js
* node ngrok.js OR ./ngrok.js
*/
if (process.env.NOT_FROM_SAILS_LIFT !== 'true') {
console.error(''); // Blank line.
console.error(''); // Blank line.
console.error('WARNING! You can NOT use `sails lift` to run this repo!');
console.error('Instead, you should use either `node app.js` OR `./app.js`');
console.error('Or you can start with the provided Ngrok script `node ngrok.js` OR `./ngrok.js`');
console.error(''); // Blank line.
console.error(''); // Blank line.
return process.exit(1);
}
/**
* START the custom schema validation feature
*/
// Check if we need to validate our schema
if (sails.config.models.validateOnBootstrap && sails.config.models.migrate === 'safe') { // aka PRODUCTION
let waitingToFinish = 0;
_.forEach(sails.models, (model, modelName) => {
if (model.tableName !== 'archive') {
waitingToFinish++;
sails.sendNativeQuery('SHOW COLUMNS FROM `' + model.tableName + '`', (err, columns) => {
if (err) {
console.error(err);
return console.error('Table "' + model.tableName + '" does not seem to exist.');
}
let i; // we use this to make sure there aren't too many columns in the table for this database
let continueOn = true;
function notWhatWasExpected(column, foundAt, expected) {
continueOn = false;
console.error('Column "' + column + '" for "' + modelName + '" is type "' + columns.rows[foundAt].Type + '", expected it to be a(n) ' + expected);
}
// check if the model field exists in the database
for (let column in model.schema) {
// verify this isn't a special/magical property or a collection
// eslint-disable-next-line no-prototype-builtins
if (model.schema.hasOwnProperty(column) && !model.schema[column].collection) {
let foundAt = null;
for (i = 0; i < columns.rows.length; i++) {
if (columns.rows[i].Field === column) {
foundAt = i;
break;
}
}
/***
* ┌─┐┌─┐┬ ┬ ┬┌┬┐┌┐┌ ┬┌─┐ ┌┬┐┬┌─┐┌─┐┬┌┐┌┌─┐
* │ │ ││ │ │││││││ │└─┐ ││││└─┐└─┐│││││ ┬
* └─┘└─┘┴─┘└─┘┴ ┴┘└┘ ┴└─┘ ┴ ┴┴└─┘└─┘┴┘└┘└─┘
*/
if (foundAt === null) {
console.error('Column "' + column + '" does not exist in database for "' + modelName + '"');
continueOn = false;
break;
}
// validate the types of columns are correct in the database
switch (model.schema[column].type) {
/***
* ┌┐┌┬ ┬┌┬┐┌┐ ┌─┐┬─┐
* ││││ ││││├┴┐├┤ ├┬┘
* ┘└┘└─┘┴ ┴└─┘└─┘┴└─
*/
case 'number':
if (model.schema[column].autoMigrations.columnType === '_numberkey') {
/***
* ┬┌─┐ ┬┌┬┐ ┬ ┬┌┐┌┌─┐┬┌─┐┌┐┌┌─┐┌┬┐┌─┐
* │└─┐ │ │ │ ││││└─┐││ ┬│││├┤ ││ ┌┘
* ┴└─┘ ┴ ┴ └─┘┘└┘└─┘┴└─┘┘└┘└─┘─┴┘ o
*/
if (columns.rows[foundAt].Type !== 'int(11) unsigned') {
notWhatWasExpected(column, foundAt, 'int(11) unsigned');
}
/***
* ┌┬┐┌─┐┌─┐┌─┐ ┬┌┬┐ ┬┌┐┌┌─┐┬─┐┌─┐┌┬┐┌─┐┌┐┌┌┬┐┌─┐
* │││ │├┤ └─┐ │ │ │││││ ├┬┘├┤ │││├┤ │││ │ ┌┘
* ─┴┘└─┘└─┘└─┘ ┴ ┴ ┴┘└┘└─┘┴└─└─┘┴ ┴└─┘┘└┘ ┴ o
*/
if (model.schema[column].autoMigrations.autoIncrement && columns.rows[foundAt].Extra !== 'auto_increment') {
notWhatWasExpected(column, foundAt, 'int(11) unsigned (auto_increment)');
}
} else {
/***
* ┌┬┐┌─┐┌┬┐┌─┐┬ ┬┌─┐ ┌┬┐┬┌─┐┌─┐┬┌┐┌┌─┐ ┌─┐┌─┐┬ ┬ ┬┌┬┐┌┐┌ ┌┬┐┬ ┬┌─┐┌─┐
* ││││ │ ││├┤ │ │└─┐ ││││└─┐└─┐│││││ ┬ │ │ ││ │ │││││││ │ └┬┘├─┘├┤
* ┴ ┴└─┘─┴┘└─┘┴─┘ ┴└─┘ ┴ ┴┴└─┘└─┘┴┘└┘└─┘ └─┘└─┘┴─┘└─┘┴ ┴┘└┘ ┴ ┴ ┴ └─┘
*/
if (model.schema[column].autoMigrations.columnType === '_number') {
notWhatWasExpected(column, foundAt, 'MODEL IS MISSING COLUMN TYPE');
}
/***
* ┌┬┐┌─┐ ┌┬┐┬ ┬┌─┐┌─┐┌─┐ ┌┬┐┌─┐┌┬┐┌─┐┬ ┬┌─┐
* │││ │ │ └┬┘├─┘├┤ └─┐ │││├─┤ │ │ ├─┤ ┌┘
* ─┴┘└─┘ ┴ ┴ ┴ └─┘└─┘ ┴ ┴┴ ┴ ┴ └─┘┴ ┴ o
*/
if (model.schema[column].autoMigrations.columnType !== '_number' && model.schema[column].autoMigrations.columnType !== columns.rows[foundAt].Type) {
notWhatWasExpected(column, foundAt, model.schema[column].autoMigrations.columnType);
}
}
break;
/***
* ┌─┐┌┬┐┬─┐┬┌┐┌┌─┐
* └─┐ │ ├┬┘│││││ ┬
* └─┘ ┴ ┴└─┴┘└┘└─┘
*/
case 'string':
/***
* ┬┌─┐ ┌┬┐┌─┐┌─┐┌─┐┬ ┬┬ ┌┬┐ ┌─┐┌┬┐┬─┐┬┌┐┌┌─┐ ┌─┐┬┌─┐┌─┐┌─┐
* │└─┐ ││├┤ ├┤ ├─┤│ ││ │ └─┐ │ ├┬┘│││││ ┬ └─┐│┌─┘├┤ ┌┘
* ┴└─┘ ─┴┘└─┘└ ┴ ┴└─┘┴─┘┴ └─┘ ┴ ┴└─┴┘└┘└─┘ └─┘┴└─┘└─┘ o
*/
if (model.schema[column].autoMigrations.columnType === '_string' && columns.rows[foundAt].Type !== 'varchar(191)') {
notWhatWasExpected(column, foundAt, 'varchar(191)');
}
/***
* ┌┬┐┌─┐ ┌┬┐┬ ┬┌─┐┌─┐┌─┐ ┌┬┐┌─┐┌┬┐┌─┐┬ ┬┌─┐
* │││ │ │ └┬┘├─┘├┤ └─┐ │││├─┤ │ │ ├─┤ ┌┘
* ─┴┘└─┘ ┴ ┴ ┴ └─┘└─┘ ┴ ┴┴ ┴ ┴ └─┘┴ ┴ o
*/
if (model.schema[column].autoMigrations.columnType.substring(0, 7) === 'varchar' && model.schema[column].autoMigrations.columnType !== columns.rows[foundAt].Type) {
notWhatWasExpected(column, foundAt, model.schema[column].autoMigrations.columnType);
}
/***
* ┬┌─┐ ┌┬┐┌─┐┌┬┐┌─┐┌┬┐┬┌┬┐┌─┐┌─┐
* │└─┐ ││├─┤ │ ├┤ │ ││││├┤ ┌┘
* ┴└─┘ ─┴┘┴ ┴ ┴ └─┘ ┴ ┴┴ ┴└─┘ o
*/
if (model.schema[column].autoMigrations.columnType === 'datetime' && columns.rows[foundAt].Type !== 'datetime') {
notWhatWasExpected(column, foundAt, 'datetime');
}
/***
* ┬┌─┐ ┌┬┐┌─┐┌┬┐┌─┐┌─┐
* │└─┐ ││├─┤ │ ├┤ ┌┘
* ┴└─┘ ─┴┘┴ ┴ ┴ └─┘ o
*/
if (model.schema[column].autoMigrations.columnType === 'date' && columns.rows[foundAt].Type !== 'date') {
notWhatWasExpected(column, foundAt, 'date');
}
/***
* ┬┌─┐ ┌┬┐┬┌┬┐┌─┐┌─┐
* │└─┐ │ ││││├┤ ┌┘
* ┴└─┘ ┴ ┴┴ ┴└─┘ o
*/
if (model.schema[column].autoMigrations.columnType === 'time' && columns.rows[foundAt].Type !== 'time') {
notWhatWasExpected(column, foundAt, 'time');
}
break;
/***
* ┌┐ ┌─┐┌─┐┬ ┌─┐┌─┐┌┐┌
* ├┴┐│ ││ ││ ├┤ ├─┤│││
* └─┘└─┘└─┘┴─┘└─┘┴ ┴┘└┘
*/
case 'boolean':
if (columns.rows[foundAt].Type !== 'tinyint(1)') {
notWhatWasExpected(column, foundAt, 'boolean (tinyint)');
}
break;
/***
* ┬┌─┐┌─┐┌┐┌
* │└─┐│ ││││
* └┘└─┘└─┘┘└┘
*/
case 'json':
if (columns.rows[foundAt].Type !== 'longtext') {
notWhatWasExpected(column, foundAt, 'json (longtext)');
}
break;
/***
* ┬─┐┌─┐┌─┐
* ├┬┘├┤ ├┤
* ┴└─└─┘└
*/
case 'ref':
if (model.schema[column].autoMigrations.columnType !== columns.rows[foundAt].Type) {
notWhatWasExpected(column, foundAt, model.schema[column].autoMigrations.columnType);
}
break;
/***
* ┬ ┬┌┐┌┬┌─┌┐┌┌─┐┬ ┬┌┐┌
* │ ││││├┴┐││││ │││││││
* └─┘┘└┘┴ ┴┘└┘└─┘└┴┘┘└┘
*/
default:
continueOn = false;
console.error('Column definition type "' + model.schema[column].type + '" does not have any safety checks! HALTING EXECUTION FOR SAFETY!');
break;
}
/***
* ┬┌─┐ ┬┌┬┐ ┬ ┬┌┐┌┬┌─┐ ┬ ┬┌─┐┌─┐
* │└─┐ │ │ │ ││││││─┼┐│ │├┤ ┌┘
* ┴└─┘ ┴ ┴ └─┘┘└┘┴└─┘└└─┘└─┘ o
*/
if (model.schema[column].autoMigrations.unique === true && columns.rows[foundAt].Key !== 'UNI' && columns.rows[foundAt].Key !== 'PRI') {
console.error('Column "' + column + '" is not marked as "unique" in database schema for "' + modelName + '"');
continueOn = false;
}
/***
* ┬┌─┐ ┬┌┬┐ ┌─┐┬─┐┬┌┬┐┌─┐┬─┐┬ ┬┌─┐
* │└─┐ │ │ ├─┘├┬┘││││├─┤├┬┘└┬┘ ┌┘
* ┴└─┘ ┴ ┴ ┴ ┴└─┴┴ ┴┴ ┴┴└─ ┴ o
*/
if (column === model.primaryKey && columns.rows[foundAt].Key !== 'PRI') {
console.error('Column "' + column + '" is not marked as the "primary key" in database schema for "' + modelName + '"');
continueOn = false;
}
}
}
/***
* ┌┬┐┌─┐┌─┐ ┌┬┐┌─┐┌┐┌┬ ┬ ┌─┐┌─┐┬ ┬ ┬┌┬┐┌┐┌┌─┐ ┬┌┐┌ ┌┬┐┌┐┌─┐
* │ │ ││ │ │││├─┤│││└┬┘ │ │ ││ │ │││││││└─┐ ││││ ││├┴┐┌┘
* ┴ └─┘└─┘ ┴ ┴┴ ┴┘└┘ ┴ └─┘└─┘┴─┘└─┘┴ ┴┘└┘└─┘ ┴┘└┘ ─┴┘└─┘o
*/
for (i = 0; i < columns.rows.length; i++) {
if (!model.schema[columns.rows[i].Field]) {
console.error('Extra column "' + columns.rows[i].Field + '" found for ' + modelName);
continueOn = false;
}
}
if (continueOn) {
waitingToFinish--;
}
});
}
});
let movedOn = false;
(function waitForSchemaChecks() {
setTimeout(() => {
if (waitingToFinish === 0 && movedOn === false) {
// everything has finished, no red flags yet, move on...
validateIndexes();
movedOn = true;
} else if (movedOn !== true) {
waitForSchemaChecks();
}
}, 20);
})();
// something is taking WAY too long... assume the worst... BAIL!
setTimeout(() => {
if (waitingToFinish > 0) {
console.error('The database schema does not appear to match the model definitions.');
process.exit(1);
}
}, 5000);
} else {
/***
* ┌┐┌┌─┐┌┬┐ ┌─┐┬─┐┌─┐┌┬┐┬ ┬┌─┐┌┬┐┬┌─┐┌┐┌
* ││││ │ │ ├─┘├┬┘│ │ │││ ││ │ ││ ││││
* ┘└┘└─┘ ┴ ┴ ┴└─└─┘─┴┘└─┘└─┘ ┴ ┴└─┘┘└┘
*/
// no safety requirement for database modifications, bypass safeties
return next();
}
function validateIndexes() {
if (sails.config.models.enforceForeignKeys === true) {
let waitingToFinish = 0;
_.forEach(sails.models, (model, modelName) => {
if (model.tableName !== 'archive' && model.associations && model.associations.length) {
model.associations.map((association) => {
waitingToFinish++;
// make sure this isn't a collection
if (!association.collection) {
/***
* ┌─┐┌─┐┌┬┐ ┌─┐┌─┐┬─┐┌─┐┬┌─┐┌┐┌ ┬┌─┌─┐┬ ┬┌─┐
* │ ┬├┤ │ ├┤ │ │├┬┘├┤ ││ ┬│││ ├┴┐├┤ └┬┘└─┐
* └─┘└─┘ ┴ └ └─┘┴└─└─┘┴└─┘┘└┘ ┴ ┴└─┘ ┴ └─┘
*/
sails.sendNativeQuery(
'SELECT * FROM `information_schema`.`KEY_COLUMN_USAGE` WHERE `TABLE_NAME` = \'' + model.tableName
+ '\' AND `COLUMN_NAME` = \'' + association.alias
+ '\' AND `REFERENCED_TABLE_NAME` = \'' + sails.models[association.model].tableName
+ '\'',
(err, foundKeys) => {
if (err) {
console.error(err);
console.error('I can\'t seem to read the required relationship data. HALTING!');
return process.exit(1);
}
if (
!foundKeys.rows[0]
|| !foundKeys.rows[0]['REFERENCED_COLUMN_NAME']
|| foundKeys.rows[0]['REFERENCED_COLUMN_NAME'] !== sails.models[association.model].primaryKey
) {
/***
* ┌┐┌┌─┐ ┬─┐┌─┐┬ ┌─┐┌┬┐┬┌─┐┌┐┌┌─┐┬ ┬┬┌─┐ ┌─┐┌─┐┬ ┬┌┐┌┌┬┐
* ││││ │ ├┬┘├┤ │ ├─┤ │ ││ ││││└─┐├─┤│├─┘ ├┤ │ ││ ││││ ││
* ┘└┘└─┘ ┴└─└─┘┴─┘┴ ┴ ┴ ┴└─┘┘└┘└─┘┴ ┴┴┴ └ └─┘└─┘┘└┘─┴┘
*/
console.error('Column "' + association.alias + '" for "' + modelName + '" does not have a relationship setup');
} else {
/***
* ┬─┐┌─┐┬ ┌─┐┌┬┐┬┌─┐┌┐┌┌─┐┬ ┬┬┌─┐ ┌─┐┌─┐┬ ┬┌┐┌┌┬┐
* ├┬┘├┤ │ ├─┤ │ ││ ││││└─┐├─┤│├─┘ ├┤ │ ││ ││││ ││
* ┴└─└─┘┴─┘┴ ┴ ┴ ┴└─┘┘└┘└─┘┴ ┴┴┴ └ └─┘└─┘┘└┘─┴┘
*/
waitingToFinish--;
}
}
);
} else {
/***
* ┬ ┬┌─┐┌─┐ ┌─┐┌─┐┬ ┬ ┌─┐┌─┐┌┬┐┬┌─┐┌┐┌
* │││├─┤└─┐ │ │ ││ │ ├┤ │ │ ││ ││││
* └┴┘┴ ┴└─┘ └─┘└─┘┴─┘┴─┘└─┘└─┘ ┴ ┴└─┘┘└┘
*/
waitingToFinish--;
}
});
}
});
(function waitForIndexChecks() {
setTimeout(() => {
if (waitingToFinish === 0) {
return next();
} else {
waitForIndexChecks();
}
}, 10);
})();
setTimeout(() => {
if (waitingToFinish > 0) {
console.error('The database schema is missing foreign key indexes.');
process.exit(1);
}
}, 3000);
} else {
return next();
}
}
/**
* END custom schema feature
*/
};