Skip to content

Commit 30ec4ff

Browse files
committed
feat: disable index validation
1 parent 0287098 commit 30ec4ff

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

spec/schemas.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2932,6 +2932,7 @@ describe('schemas', () => {
29322932
beforeEach(async () => {
29332933
await TestUtils.destroyAllDataPermanently(false);
29342934
await config.database.adapter.performInitialization({ VolatileClassesSchemas: [] });
2935+
databaseAdapter.disableIndexFieldValidation = false;
29352936
});
29362937

29372938
it('cannot create index if field does not exist', done => {
@@ -2960,6 +2961,29 @@ describe('schemas', () => {
29602961
});
29612962
});
29622963

2964+
it('can create index if field does not exist with disableIndexFieldValidation true ', async () => {
2965+
databaseAdapter.disableIndexFieldValidation = true;
2966+
await request({
2967+
url: 'http://localhost:8378/1/schemas/NewClass',
2968+
method: 'POST',
2969+
headers: masterKeyHeaders,
2970+
json: true,
2971+
body: {},
2972+
});
2973+
const response = await request({
2974+
url: 'http://localhost:8378/1/schemas/NewClass',
2975+
method: 'PUT',
2976+
headers: masterKeyHeaders,
2977+
json: true,
2978+
body: {
2979+
indexes: {
2980+
name1: { aString: 1 },
2981+
},
2982+
},
2983+
});
2984+
expect(response.data.indexes.name1).toEqual({ aString: 1 });
2985+
});
2986+
29632987
it('can create index on default field', done => {
29642988
request({
29652989
url: 'http://localhost:8378/1/schemas/NewClass',

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ export class MongoStorageAdapter implements StorageAdapter {
139139
_maxTimeMS: ?number;
140140
canSortOnJoinTables: boolean;
141141
enableSchemaHooks: boolean;
142+
disableIndexFieldValidation: boolean;
142143

143144
constructor({ uri = defaults.DefaultMongoURI, collectionPrefix = '', mongoOptions = {} }: any) {
144145
this._uri = uri;
@@ -152,7 +153,9 @@ export class MongoStorageAdapter implements StorageAdapter {
152153
this._maxTimeMS = mongoOptions.maxTimeMS;
153154
this.canSortOnJoinTables = true;
154155
this.enableSchemaHooks = !!mongoOptions.enableSchemaHooks;
156+
this.disableIndexFieldValidation = !!mongoOptions.disableIndexFieldValidation;
155157
delete mongoOptions.enableSchemaHooks;
158+
delete mongoOptions.disableIndexFieldValidation;
156159
delete mongoOptions.maxTimeMS;
157160
}
158161

@@ -287,6 +290,7 @@ export class MongoStorageAdapter implements StorageAdapter {
287290
} else {
288291
Object.keys(field).forEach(key => {
289292
if (
293+
!this.disableIndexFieldValidation &&
290294
!Object.prototype.hasOwnProperty.call(
291295
fields,
292296
key.indexOf('_p_') === 0 ? key.replace('_p_', '') : key

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -851,11 +851,14 @@ export class PostgresStorageAdapter implements StorageAdapter {
851851
_pgp: any;
852852
_stream: any;
853853
_uuid: any;
854+
disableIndexFieldValidation: boolean;
854855

855856
constructor({ uri, collectionPrefix = '', databaseOptions = {} }: any) {
856857
this._collectionPrefix = collectionPrefix;
857858
this.enableSchemaHooks = !!databaseOptions.enableSchemaHooks;
859+
this.disableIndexFieldValidation = !!databaseOptions.disableIndexFieldValidation;
858860
delete databaseOptions.enableSchemaHooks;
861+
delete databaseOptions.disableIndexFieldValidation;
859862

860863
const { client, pgp } = createClient(uri, databaseOptions);
861864
this._client = client;
@@ -975,7 +978,10 @@ export class PostgresStorageAdapter implements StorageAdapter {
975978
delete existingIndexes[name];
976979
} else {
977980
Object.keys(field).forEach(key => {
978-
if (!Object.prototype.hasOwnProperty.call(fields, key)) {
981+
if (
982+
!this.disableIndexFieldValidation &&
983+
!Object.prototype.hasOwnProperty.call(fields, key)
984+
) {
979985
throw new Parse.Error(
980986
Parse.Error.INVALID_QUERY,
981987
`Field ${key} does not exist, cannot add index.`
@@ -990,8 +996,15 @@ export class PostgresStorageAdapter implements StorageAdapter {
990996
}
991997
});
992998
await conn.tx('set-indexes-with-schema-format', async t => {
993-
if (insertedIndexes.length > 0) {
994-
await self.createIndexes(className, insertedIndexes, t);
999+
try {
1000+
if (insertedIndexes.length > 0) {
1001+
await self.createIndexes(className, insertedIndexes, t);
1002+
}
1003+
} catch (e) {
1004+
const columnDoesNotExistError = e.errors?.[0]?.code === '42703';
1005+
if (columnDoesNotExistError && !this.disableIndexFieldValidation) {
1006+
throw e;
1007+
}
9951008
}
9961009
if (deletedIndexes.length > 0) {
9971010
await self.dropIndexes(className, deletedIndexes, t);

0 commit comments

Comments
 (0)