Skip to content
49 changes: 48 additions & 1 deletion spec/DefinedSchemas.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ describe('DefinedSchemas', () => {
expect(schema.indexes).toEqual(indexes);
});

it('should delete removed indexes', async () => {
it('should delete unknown indexes when dropUnknownIndexes is not set', async () => {
const server = await reconfigureServer();

let indexes = { complex: { createdAt: 1, updatedAt: 1 } };
Expand All @@ -393,6 +393,53 @@ describe('DefinedSchemas', () => {
cleanUpIndexes(schema);
expect(schema.indexes).toBeUndefined();
});

it('should delete unknown indexes when dropUnknownIndexes is set to true', async () => {
const server = await reconfigureServer();

let indexes = { complex: { createdAt: 1, updatedAt: 1 } };

let schemas = { definitions: [{ className: 'Test', indexes }], dropUnknownIndexes: true };
await new DefinedSchemas(schemas, server.config).execute();

indexes = {};
schemas = { definitions: [{ className: 'Test', indexes }], dropUnknownIndexes: true };
// Change indexes
await new DefinedSchemas(schemas, server.config).execute();
let schema = await new Parse.Schema('Test').get();
cleanUpIndexes(schema);
expect(schema.indexes).toBeUndefined();

// Update
await new DefinedSchemas(schemas, server.config).execute();
schema = await new Parse.Schema('Test').get();
cleanUpIndexes(schema);
expect(schema.indexes).toBeUndefined();
});

it('should not delete unknown indexes when dropUnknownIndexes is set to false', async () => {
const server = await reconfigureServer();

const indexes = { complex: { createdAt: 1, updatedAt: 1 } };

let schemas = { definitions: [{ className: 'Test', indexes }], dropUnknownIndexes: false };
await new DefinedSchemas(schemas, server.config).execute();

schemas = { definitions: [{ className: 'Test', indexes: {} }], dropUnknownIndexes: false };

// Change indexes
await new DefinedSchemas(schemas, server.config).execute();
let schema = await new Parse.Schema('Test').get();
cleanUpIndexes(schema);
expect(schema.indexes).toEqual({ complex: { createdAt: 1, updatedAt: 1 } });

// Update
await new DefinedSchemas(schemas, server.config).execute();
schema = await new Parse.Schema('Test').get();
cleanUpIndexes(schema);
expect(schema.indexes).toEqual(indexes);
});

xit('should keep protected indexes', async () => {
const server = await reconfigureServer();

Expand Down
7 changes: 7 additions & 0 deletions src/Options/Definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ module.exports.SchemaOptions = {
action: parsers.booleanParser,
default: false,
},
dropUnknownIndexes: {
env: 'PARSE_SERVER_SCHEMA_DROP_UNKNOWN_INDEXES',
help:
'Drops indexes that are not defined in the schema and are present in the database. Set this false if you are adding indexes manually so that it wont be dropped when you run schema migration',
action: parsers.booleanParser,
default: true,
},
lockSchemas: {
env: 'PARSE_SERVER_SCHEMA_LOCK_SCHEMAS',
help:
Expand Down
1 change: 1 addition & 0 deletions src/Options/docs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export interface SchemaOptions {
/* Is true if Parse Server will reject any attempts to modify the schema while the server is running.
:DEFAULT: false */
lockSchemas: ?boolean;
/* Drops indexes that are not defined in the schema and are present in the database. Set this false if you are adding indexes manually so that it wont be dropped when you run schema migration
:DEFAULT: true */
dropUnknownIndexes: ?boolean;
/* Execute a callback before running schema migrations. */
beforeMigration: ?() => void | Promise<void>;
/* Execute a callback after running schema migrations. */
Expand Down
5 changes: 4 additions & 1 deletion src/SchemaMigrations/DefinedSchemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,10 @@ export class DefinedSchemas {
Object.keys(cloudSchema.indexes).forEach(indexName => {
if (!this.isProtectedIndex(localSchema.className, indexName)) {
if (!localSchema.indexes || !localSchema.indexes[indexName]) {
newLocalSchema.deleteIndex(indexName);
// Only delete indexes which are present in database if dropUnknownIndexes is `true`
if(this.schemaOptions.dropUnknownIndexes !== false){
newLocalSchema.deleteIndex(indexName);
}
} else if (
!this.paramsAreEquals(localSchema.indexes[indexName], cloudSchema.indexes[indexName])
) {
Expand Down
1 change: 1 addition & 0 deletions src/SchemaMigrations/Migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface SchemaOptions {
deleteExtraFields: ?boolean;
recreateModifiedFields: ?boolean;
lockSchemas: ?boolean;
dropUnknownIndexes: ?boolean;
beforeMigration: ?() => void | Promise<void>;
afterMigration: ?() => void | Promise<void>;
}
Expand Down
Loading