Skip to content

Commit a3ab545

Browse files
committed
remove improvements
1 parent cf65542 commit a3ab545

File tree

3 files changed

+46
-38
lines changed

3 files changed

+46
-38
lines changed

spec/PostgresStorageAdapter.spec.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -385,14 +385,23 @@ describe_only_db('postgres')('PostgresStorageAdapter', () => {
385385
const { database } = Config.get(Parse.applicationId);
386386
const { adapter } = database;
387387

388-
spyOn(adapter, 'watch');
389388
spyOn(adapter, '_onchange');
390-
const schema = await database.loadSchema();
391-
// Create a valid class
392-
await schema.validateObject('Stuff', { foo: 'bar' });
393-
await new Promise(resolve => setTimeout(resolve, 500));
394389

395-
expect(adapter.watch).toHaveBeenCalled();
390+
const otherInstance = new PostgresStorageAdapter({ uri: databaseURI });
391+
otherInstance._listenToSchema();
392+
393+
await otherInstance.createClass('Stuff', {
394+
className: 'Stuff',
395+
fields: {
396+
objectId: { type: 'String' },
397+
createdAt: { type: 'Date' },
398+
updatedAt: { type: 'Date' },
399+
_rperm: { type: 'Array' },
400+
_wperm: { type: 'Array' },
401+
},
402+
classLevelPermissions: undefined,
403+
});
404+
await new Promise(resolve => setTimeout(resolve, 500));
396405
expect(adapter._onchange).toHaveBeenCalled();
397406
});
398407
});

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { createClient } from './PostgresClient';
44
import Parse from 'parse/node';
55
// @flow-disable-next
66
import _ from 'lodash';
7+
import { v4 as uuidv4 } from 'uuid';
78
import sql from './sql';
89

910
const PostgresRelationDoesNotExistError = '42P01';
@@ -801,6 +802,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
801802
_onchange: any;
802803
_pgp: any;
803804
_stream: any;
805+
_uuid: any;
804806

805807
constructor({ uri, collectionPrefix = '', databaseOptions }: any) {
806808
this._collectionPrefix = collectionPrefix;
@@ -809,6 +811,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
809811
this._onchange = () => {};
810812
this._pgp = pgp;
811813
this.canSortOnJoinTables = false;
814+
this.uuid = uuidv4();
812815
}
813816

814817
watch(callback: () => void): void {
@@ -835,11 +838,26 @@ export class PostgresStorageAdapter implements StorageAdapter {
835838
this._client.$pool.end();
836839
}
837840

841+
async _listenToSchema() {
842+
if (!this._stream) {
843+
this._stream = await this._client.connect({ direct: true });
844+
this._stream.client.on('notification', data => {
845+
const payload = JSON.parse(data.payload);
846+
if (payload.senderId !== this.uuid) {
847+
this._onchange();
848+
}
849+
});
850+
await this._stream.none('LISTEN $1~', 'schema.change');
851+
}
852+
}
853+
838854
_notifySchemaChange() {
839855
if (this._stream) {
840-
this._stream.none('NOTIFY $1~, $2', ['schema.change', '']).catch(error => {
841-
console.log('Failed to Notify:', error); // unlikely to ever happen
842-
});
856+
this._stream
857+
.none('NOTIFY $1~, $2', ['schema.change', { senderId: this.uuid }])
858+
.catch(error => {
859+
console.log('Failed to Notify:', error); // unlikely to ever happen
860+
});
843861
}
844862
}
845863

@@ -948,7 +966,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
948966

949967
async createClass(className: string, schema: SchemaType, conn: ?any) {
950968
conn = conn || this._client;
951-
return conn
969+
const parseSchema = await conn
952970
.tx('create-class', async t => {
953971
await this.createTable(className, schema, t);
954972
await t.none(
@@ -964,6 +982,8 @@ export class PostgresStorageAdapter implements StorageAdapter {
964982
}
965983
throw err;
966984
});
985+
this._notifySchemaChange();
986+
return parseSchema;
967987
}
968988

969989
// Just create a table, do not insert in schema
@@ -2270,11 +2290,6 @@ export class PostgresStorageAdapter implements StorageAdapter {
22702290
async performInitialization({ VolatileClassesSchemas }: any) {
22712291
// TODO: This method needs to be rewritten to make proper use of connections (@vitaly-t)
22722292
debug('performInitialization');
2273-
if (!this._stream) {
2274-
this._stream = await this._client.connect({ direct: true });
2275-
this._stream.client.on('notification', () => this._onchange());
2276-
await this._stream.none('LISTEN $1~', 'schema.change');
2277-
}
22782293
const promises = VolatileClassesSchemas.map(schema => {
22792294
return this.createTable(schema.className, schema)
22802295
.catch(err => {
@@ -2288,6 +2303,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
22882303
})
22892304
.then(() => this.schemaUpgrade(schema.className, schema));
22902305
});
2306+
promises.push(this._listenToSchema());
22912307
return Promise.all(promises)
22922308
.then(() => {
22932309
return this._client.tx('perform-initialization', async t => {

src/Controllers/SchemaController.js

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -808,19 +808,7 @@ export default class SchemaController {
808808
className,
809809
})
810810
)
811-
.then(adapterSchema => {
812-
const parseSchema = convertAdapterSchemaToParseSchema(adapterSchema);
813-
this._cache.allClasses = this._cache.allClasses || [];
814-
const index = this._cache.allClasses.findIndex(
815-
cached => cached.className === parseSchema.className
816-
);
817-
if (index >= 0) {
818-
this._cache.allClasses[index] = parseSchema;
819-
} else {
820-
this._cache.allClasses.push(parseSchema);
821-
}
822-
return parseSchema;
823-
})
811+
.then(convertAdapterSchemaToParseSchema)
824812
.catch(error => {
825813
if (error && error.code === Parse.Error.DUPLICATE_VALUE) {
826814
throw new Parse.Error(
@@ -946,7 +934,7 @@ export default class SchemaController {
946934
return (
947935
this.addClassIfNotExists(className)
948936
// The schema update succeeded. Reload the schema
949-
.then(() => this.reloadData())
937+
.then(() => this.reloadData({ clearCache: true }))
950938
.catch(() => {
951939
// The schema update failed. This can be okay - it might
952940
// have failed because there's a race condition and a different
@@ -1132,12 +1120,6 @@ export default class SchemaController {
11321120
return Promise.resolve();
11331121
})
11341122
.then(() => {
1135-
const cached = (this._cache.allClasses || []).find(
1136-
schema => schema.className === className
1137-
);
1138-
if (cached && !cached.fields[fieldName]) {
1139-
cached.fields[fieldName] = type;
1140-
}
11411123
return {
11421124
className,
11431125
fieldName,
@@ -1230,7 +1212,7 @@ export default class SchemaController {
12301212
async validateObject(className: string, object: any, query: any) {
12311213
let geocount = 0;
12321214
const schema = await this.enforceClassExists(className);
1233-
const results = [];
1215+
const promises = [];
12341216

12351217
for (const fieldName in object) {
12361218
if (object[fieldName] === undefined) {
@@ -1257,12 +1239,13 @@ export default class SchemaController {
12571239
// Every object has ACL implicitly.
12581240
continue;
12591241
}
1260-
results.push(await schema.enforceFieldExists(className, fieldName, expected));
1242+
promises.push(schema.enforceFieldExists(className, fieldName, expected));
12611243
}
1244+
const results = await Promise.all(promises);
12621245
const enforceFields = results.filter(result => !!result);
12631246

12641247
if (enforceFields.length !== 0) {
1265-
await this.reloadData();
1248+
await this.reloadData({ clearCache: true });
12661249
}
12671250
this.ensureFields(enforceFields);
12681251

0 commit comments

Comments
 (0)