Skip to content

Commit 89fad46

Browse files
feat: Add option keepUnknownIndexes to retain indexes which are not specified in schema (#9857)
1 parent b7faabb commit 89fad46

File tree

6 files changed

+64
-2
lines changed

6 files changed

+64
-2
lines changed

spec/DefinedSchemas.spec.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ describe('DefinedSchemas', () => {
371371
expect(schema.indexes).toEqual(indexes);
372372
});
373373

374-
it('should delete removed indexes', async () => {
374+
it('should delete unknown indexes when keepUnknownIndexes is not set', async () => {
375375
const server = await reconfigureServer();
376376

377377
let indexes = { complex: { createdAt: 1, updatedAt: 1 } };
@@ -393,6 +393,53 @@ describe('DefinedSchemas', () => {
393393
cleanUpIndexes(schema);
394394
expect(schema.indexes).toBeUndefined();
395395
});
396+
397+
it('should delete unknown indexes when keepUnknownIndexes is set to false', async () => {
398+
const server = await reconfigureServer();
399+
400+
let indexes = { complex: { createdAt: 1, updatedAt: 1 } };
401+
402+
let schemas = { definitions: [{ className: 'Test', indexes }], keepUnknownIndexes: false };
403+
await new DefinedSchemas(schemas, server.config).execute();
404+
405+
indexes = {};
406+
schemas = { definitions: [{ className: 'Test', indexes }], keepUnknownIndexes: false };
407+
// Change indexes
408+
await new DefinedSchemas(schemas, server.config).execute();
409+
let schema = await new Parse.Schema('Test').get();
410+
cleanUpIndexes(schema);
411+
expect(schema.indexes).toBeUndefined();
412+
413+
// Update
414+
await new DefinedSchemas(schemas, server.config).execute();
415+
schema = await new Parse.Schema('Test').get();
416+
cleanUpIndexes(schema);
417+
expect(schema.indexes).toBeUndefined();
418+
});
419+
420+
it('should not delete unknown indexes when keepUnknownIndexes is set to true', async () => {
421+
const server = await reconfigureServer();
422+
423+
const indexes = { complex: { createdAt: 1, updatedAt: 1 } };
424+
425+
let schemas = { definitions: [{ className: 'Test', indexes }], keepUnknownIndexes: true };
426+
await new DefinedSchemas(schemas, server.config).execute();
427+
428+
schemas = { definitions: [{ className: 'Test', indexes: {} }], keepUnknownIndexes: true };
429+
430+
// Change indexes
431+
await new DefinedSchemas(schemas, server.config).execute();
432+
let schema = await new Parse.Schema('Test').get();
433+
cleanUpIndexes(schema);
434+
expect(schema.indexes).toEqual({ complex: { createdAt: 1, updatedAt: 1 } });
435+
436+
// Update
437+
await new DefinedSchemas(schemas, server.config).execute();
438+
schema = await new Parse.Schema('Test').get();
439+
cleanUpIndexes(schema);
440+
expect(schema.indexes).toEqual(indexes);
441+
});
442+
396443
xit('should keep protected indexes', async () => {
397444
const server = await reconfigureServer();
398445

src/Options/Definitions.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ module.exports.SchemaOptions = {
2828
action: parsers.booleanParser,
2929
default: false,
3030
},
31+
keepUnknownIndexes: {
32+
env: 'PARSE_SERVER_SCHEMA_KEEP_UNKNOWN_INDEXES',
33+
help:
34+
"(Optional) Keep indexes that are present in the database but not defined in the schema. Set this to `true` if you are adding indexes manually, so that they won't be removed when running schema migration. Default is `false`.",
35+
action: parsers.booleanParser,
36+
default: false,
37+
},
3138
lockSchemas: {
3239
env: 'PARSE_SERVER_SCHEMA_LOCK_SCHEMAS',
3340
help:

src/Options/docs.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Options/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ export interface SchemaOptions {
2525
/* Is true if Parse Server will reject any attempts to modify the schema while the server is running.
2626
:DEFAULT: false */
2727
lockSchemas: ?boolean;
28+
/* (Optional) Keep indexes that are present in the database but not defined in the schema. Set this to `true` if you are adding indexes manually, so that they won't be removed when running schema migration. Default is `false`.
29+
:DEFAULT: false */
30+
keepUnknownIndexes: ?boolean;
2831
/* Execute a callback before running schema migrations. */
2932
beforeMigration: ?() => void | Promise<void>;
3033
/* Execute a callback after running schema migrations. */

src/SchemaMigrations/DefinedSchemas.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,10 @@ export class DefinedSchemas {
349349
Object.keys(cloudSchema.indexes).forEach(indexName => {
350350
if (!this.isProtectedIndex(localSchema.className, indexName)) {
351351
if (!localSchema.indexes || !localSchema.indexes[indexName]) {
352-
newLocalSchema.deleteIndex(indexName);
352+
// If keepUnknownIndex is falsy, then delete all unknown indexes from the db.
353+
if(!this.schemaOptions.keepUnknownIndexes){
354+
newLocalSchema.deleteIndex(indexName);
355+
}
353356
} else if (
354357
!this.paramsAreEquals(localSchema.indexes[indexName], cloudSchema.indexes[indexName])
355358
) {

src/SchemaMigrations/Migrations.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface SchemaOptions {
66
deleteExtraFields: ?boolean;
77
recreateModifiedFields: ?boolean;
88
lockSchemas: ?boolean;
9+
keepUnknownIndexes: ?boolean;
910
beforeMigration: ?() => void | Promise<void>;
1011
afterMigration: ?() => void | Promise<void>;
1112
}

0 commit comments

Comments
 (0)