forked from parse-community/parse-server
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDefinedSchemas.js
442 lines (392 loc) · 14.5 KB
/
DefinedSchemas.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// @flow
// @flow-disable-next Cannot resolve module `parse/node`.
const Parse = require('parse/node');
import { logger } from '../logger';
import Config from '../Config';
import { internalCreateSchema, internalUpdateSchema } from '../Routers/SchemasRouter';
import { defaultColumns, systemClasses } from '../Controllers/SchemaController';
import { ParseServerOptions } from '../Options';
import * as Migrations from './Migrations';
export class DefinedSchemas {
config: ParseServerOptions;
schemaOptions: Migrations.SchemaOptions;
localSchemas: Migrations.JSONSchema[];
retries: number;
maxRetries: number;
allCloudSchemas: Parse.Schema[];
constructor(schemaOptions: Migrations.SchemaOptions, config: ParseServerOptions) {
this.localSchemas = [];
this.config = Config.get(config.appId);
this.schemaOptions = schemaOptions;
if (schemaOptions && schemaOptions.definitions) {
if (!Array.isArray(schemaOptions.definitions)) {
throw `"schema.definitions" must be an array of schemas`;
}
this.localSchemas = schemaOptions.definitions;
}
this.retries = 0;
this.maxRetries = 3;
}
async saveSchemaToDB(schema: Parse.Schema): Promise<void> {
const payload = {
className: schema.className,
fields: schema._fields,
indexes: schema._indexes,
classLevelPermissions: schema._clp,
};
await internalCreateSchema(schema.className, payload, this.config);
this.resetSchemaOps(schema);
}
resetSchemaOps(schema: Parse.Schema) {
// Reset ops like SDK
schema._fields = {};
schema._indexes = {};
}
// Simulate update like the SDK
// We cannot use SDK since routes are disabled
async updateSchemaToDB(schema: Parse.Schema) {
const payload = {
className: schema.className,
fields: schema._fields,
indexes: schema._indexes,
classLevelPermissions: schema._clp,
};
await internalUpdateSchema(schema.className, payload, this.config);
this.resetSchemaOps(schema);
}
async execute() {
try {
logger.info('Running Migrations');
if (this.schemaOptions && this.schemaOptions.beforeMigration) {
await Promise.resolve(this.schemaOptions.beforeMigration());
}
await this.executeMigrations();
if (this.schemaOptions && this.schemaOptions.afterMigration) {
await Promise.resolve(this.schemaOptions.afterMigration());
}
logger.info('Running Migrations Completed');
} catch (e) {
logger.error(`Failed to run migrations: ${e}`);
if (process.env.NODE_ENV === 'production') process.exit(1);
}
}
async executeMigrations() {
let timeout = null;
try {
// Set up a time out in production
// if we fail to get schema
// pm2 or K8s and many other process managers will try to restart the process
// after the exit
if (process.env.NODE_ENV === 'production') {
timeout = setTimeout(() => {
logger.error('Timeout occurred during execution of migrations. Exiting...');
process.exit(1);
}, 20000);
}
// Hack to force session schema to be created
await this.createDeleteSession();
this.allCloudSchemas = await Parse.Schema.all();
clearTimeout(timeout);
await Promise.all(this.localSchemas.map(async localSchema => this.saveOrUpdate(localSchema)));
this.checkForMissingSchemas();
await this.enforceCLPForNonProvidedClass();
} catch (e) {
if (timeout) clearTimeout(timeout);
if (this.retries < this.maxRetries) {
this.retries++;
// first retry 1sec, 2sec, 3sec total 6sec retry sequence
// retry will only happen in case of deploying multi parse server instance
// at the same time. Modern systems like k8 avoid this by doing rolling updates
await this.wait(1000 * this.retries);
await this.executeMigrations();
} else {
logger.error(`Failed to run migrations: ${e}`);
if (process.env.NODE_ENV === 'production') process.exit(1);
}
}
}
checkForMissingSchemas() {
if (this.schemaOptions.strict !== true) {
return;
}
const cloudSchemas = this.allCloudSchemas.map(s => s.className);
const localSchemas = this.localSchemas.map(s => s.className);
const missingSchemas = cloudSchemas.filter(
c => !localSchemas.includes(c) && !systemClasses.includes(c)
);
if (new Set(localSchemas).size !== localSchemas.length) {
logger.error(
`The list of schemas provided contains duplicated "className" "${localSchemas.join(
'","'
)}"`
);
process.exit(1);
}
if (this.schemaOptions.strict && missingSchemas.length) {
logger.warn(
`The following schemas are currently present in the database, but not explicitly defined in a schema: "${missingSchemas.join(
'", "'
)}"`
);
}
}
// Required for testing purpose
wait(time: number) {
return new Promise<void>(resolve => setTimeout(resolve, time));
}
async enforceCLPForNonProvidedClass(): Promise<void> {
const nonProvidedClasses = this.allCloudSchemas.filter(
cloudSchema =>
!this.localSchemas.some(localSchema => localSchema.className === cloudSchema.className)
);
await Promise.all(
nonProvidedClasses.map(async schema => {
const parseSchema = new Parse.Schema(schema.className);
this.handleCLP(schema, parseSchema);
await this.updateSchemaToDB(parseSchema);
})
);
}
// Create a fake session since Parse do not create the _Session until
// a session is created
async createDeleteSession() {
const session = new Parse.Session();
await session.save(null, { useMasterKey: true });
await session.destroy({ useMasterKey: true });
}
async saveOrUpdate(localSchema: Migrations.JSONSchema) {
const cloudSchema = this.allCloudSchemas.find(sc => sc.className === localSchema.className);
if (cloudSchema) {
try {
await this.updateSchema(localSchema, cloudSchema);
} catch (e) {
throw `Error during update of schema for type ${cloudSchema.className}: ${e}`;
}
} else {
try {
await this.saveSchema(localSchema);
} catch (e) {
throw `Error while saving Schema for type ${localSchema.className}: ${e}`;
}
}
}
async saveSchema(localSchema: Migrations.JSONSchema) {
const newLocalSchema = new Parse.Schema(localSchema.className);
if (localSchema.fields) {
// Handle fields
Object.keys(localSchema.fields)
.filter(fieldName => !this.isProtectedFields(localSchema.className, fieldName))
.forEach(fieldName => {
if (localSchema.fields) {
const field = localSchema.fields[fieldName];
this.handleFields(newLocalSchema, fieldName, field);
}
});
}
// Handle indexes
if (localSchema.indexes) {
Object.keys(localSchema.indexes).forEach(indexName => {
if (localSchema.indexes && !this.isProtectedIndex(localSchema.className, indexName)) {
newLocalSchema.addIndex(indexName, localSchema.indexes[indexName]);
}
});
}
this.handleCLP(localSchema, newLocalSchema);
return await this.saveSchemaToDB(newLocalSchema);
}
async updateSchema(localSchema: Migrations.JSONSchema, cloudSchema: Parse.Schema) {
const newLocalSchema = new Parse.Schema(localSchema.className);
// Handle fields
// Check addition
if (localSchema.fields) {
Object.keys(localSchema.fields)
.filter(fieldName => !this.isProtectedFields(localSchema.className, fieldName))
.forEach(fieldName => {
// @flow-disable-next
const field = localSchema.fields[fieldName];
if (!cloudSchema.fields[fieldName]) {
this.handleFields(newLocalSchema, fieldName, field);
}
});
}
const fieldsToDelete: string[] = [];
const fieldsToRecreate: {
fieldName: string,
from: { type: string, targetClass?: string },
to: { type: string, targetClass?: string },
}[] = [];
const fieldsWithChangedParams: string[] = [];
// Check deletion
Object.keys(cloudSchema.fields)
.filter(fieldName => !this.isProtectedFields(localSchema.className, fieldName))
.forEach(fieldName => {
const field = cloudSchema.fields[fieldName];
if (!localSchema.fields || !localSchema.fields[fieldName]) {
fieldsToDelete.push(fieldName);
return;
}
const localField = localSchema.fields[fieldName];
// Check if field has a changed type
if (
!this.paramsAreEquals(
{ type: field.type, targetClass: field.targetClass },
{ type: localField.type, targetClass: localField.targetClass }
)
) {
fieldsToRecreate.push({
fieldName,
from: { type: field.type, targetClass: field.targetClass },
to: { type: localField.type, targetClass: localField.targetClass },
});
return;
}
// Check if something changed other than the type (like required, defaultValue)
if (!this.paramsAreEquals(field, localField)) {
fieldsWithChangedParams.push(fieldName);
}
});
if (this.schemaOptions.deleteExtraFields === true) {
fieldsToDelete.forEach(fieldName => {
newLocalSchema.deleteField(fieldName);
});
// Delete fields from the schema then apply changes
await this.updateSchemaToDB(newLocalSchema);
} else if (this.schemaOptions.strict === true && fieldsToDelete.length) {
logger.warn(
`The following fields exist in the database for "${
localSchema.className
}", but are missing in the schema : "${fieldsToDelete.join('" ,"')}"`
);
}
if (this.schemaOptions.recreateModifiedFields === true) {
fieldsToRecreate.forEach(field => {
newLocalSchema.deleteField(field.fieldName);
});
// Delete fields from the schema then apply changes
await this.updateSchemaToDB(newLocalSchema);
fieldsToRecreate.forEach(fieldInfo => {
if (localSchema.fields) {
const field = localSchema.fields[fieldInfo.fieldName];
this.handleFields(newLocalSchema, fieldInfo.fieldName, field);
}
});
} else if (this.schemaOptions.strict === true && fieldsToRecreate.length) {
fieldsToRecreate.forEach(field => {
const from =
field.from.type + (field.from.targetClass ? ` (${field.from.targetClass})` : '');
const to = field.to.type + (field.to.targetClass ? ` (${field.to.targetClass})` : '');
logger.warn(
`The field "${field.fieldName}" type differ between the schema and the database for "${localSchema.className}"; Schema is defined as "${to}" and current database type is "${from}"`
);
});
}
fieldsWithChangedParams.forEach(fieldName => {
if (localSchema.fields) {
const field = localSchema.fields[fieldName];
this.handleFields(newLocalSchema, fieldName, field);
}
});
// Handle Indexes
// Check addition
if (localSchema.indexes) {
Object.keys(localSchema.indexes).forEach(indexName => {
if (
(!cloudSchema.indexes || !cloudSchema.indexes[indexName]) &&
!this.isProtectedIndex(localSchema.className, indexName)
) {
if (localSchema.indexes) {
newLocalSchema.addIndex(indexName, localSchema.indexes[indexName]);
}
}
});
}
const indexesToAdd = [];
// Check deletion
if (cloudSchema.indexes) {
Object.keys(cloudSchema.indexes).forEach(indexName => {
if (!this.isProtectedIndex(localSchema.className, indexName)) {
if (!localSchema.indexes || !localSchema.indexes[indexName]) {
newLocalSchema.deleteIndex(indexName);
} else if (
!this.paramsAreEquals(localSchema.indexes[indexName], cloudSchema.indexes[indexName])
) {
newLocalSchema.deleteIndex(indexName);
if (localSchema.indexes) {
indexesToAdd.push({
indexName,
index: localSchema.indexes[indexName],
});
}
}
}
});
}
this.handleCLP(localSchema, newLocalSchema, cloudSchema);
// Apply changes
await this.updateSchemaToDB(newLocalSchema);
// Apply new/changed indexes
if (indexesToAdd.length) {
logger.debug(
`Updating indexes for "${newLocalSchema.className}" : ${indexesToAdd.join(' ,')}`
);
indexesToAdd.forEach(o => newLocalSchema.addIndex(o.indexName, o.index));
await this.updateSchemaToDB(newLocalSchema);
}
}
handleCLP(
localSchema: Migrations.JSONSchema,
newLocalSchema: Parse.Schema,
cloudSchema: Parse.Schema
) {
if (!localSchema.classLevelPermissions && !cloudSchema) {
logger.warn(`classLevelPermissions not provided for ${localSchema.className}.`);
}
// Use spread to avoid read only issue (encountered by Moumouls using directAccess)
const clp = ({ ...localSchema.classLevelPermissions } || {}: Parse.CLP.PermissionsMap);
// To avoid inconsistency we need to remove all rights on addField
clp.addField = {};
newLocalSchema.setCLP(clp);
}
isProtectedFields(className: string, fieldName: string) {
return (
!!defaultColumns._Default[fieldName] ||
!!(defaultColumns[className] && defaultColumns[className][fieldName])
);
}
isProtectedIndex(className: string, indexName: string) {
const indexes = ['_id_'];
switch (className) {
case '_User':
indexes.push(
'case_insensitive_username',
'case_insensitive_email',
'username_1',
'email_1'
);
break;
case '_Role':
indexes.push('name_1');
break;
case '_Idempotency':
indexes.push('reqId_1');
break;
}
return indexes.indexOf(indexName) !== -1;
}
paramsAreEquals<T: { [key: string]: any }>(objA: T, objB: T) {
const keysA: string[] = Object.keys(objA);
const keysB: string[] = Object.keys(objB);
// Check key name
if (keysA.length !== keysB.length) return false;
return keysA.every(k => objA[k] === objB[k]);
}
handleFields(newLocalSchema: Parse.Schema, fieldName: string, field: Migrations.FieldType) {
if (field.type === 'Relation') {
newLocalSchema.addRelation(fieldName, field.targetClass);
} else if (field.type === 'Pointer') {
newLocalSchema.addPointer(fieldName, field.targetClass, field);
} else {
newLocalSchema.addField(fieldName, field.type, field);
}
}
}