Skip to content

Commit 006540c

Browse files
authored
Implement pointer types in postgres. (#2086)
* Support pointers in postgres * implement count * Fix bug in createClass
1 parent ab06055 commit 006540c

File tree

6 files changed

+37
-17
lines changed

6 files changed

+37
-17
lines changed

spec/InstallationsRouter.spec.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ describe('InstallationsRouter', () => {
103103
});
104104
});
105105

106-
it_exclude_dbs(['postgres'])('query installations with count = 1', (done) => {
106+
it('query installations with count = 1', done => {
107107
var androidDeviceRequest = {
108108
'installationId': '12345678-abcd-abcd-abcd-123456789abc',
109109
'deviceType': 'android'
@@ -130,10 +130,14 @@ describe('InstallationsRouter', () => {
130130
expect(response.results.length).toEqual(2);
131131
expect(response.count).toEqual(2);
132132
done();
133-
});
133+
})
134+
.catch(error => {
135+
fail(JSON.stringify(error));
136+
done();
137+
})
134138
});
135139

136-
it_exclude_dbs(['postgres'])('query installations with limit = 0 and count = 1', (done) => {
140+
it('query installations with limit = 0 and count = 1', (done) => {
137141
var androidDeviceRequest = {
138142
'installationId': '12345678-abcd-abcd-abcd-123456789abc',
139143
'deviceType': 'android'

spec/ParseObject.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('Parse.Object testing', () => {
4949
});
5050
});
5151

52-
it_exclude_dbs(['postgres'])("save cycle", function(done) {
52+
it("save cycle", done => {
5353
var a = new Parse.Object("TestObject");
5454
var b = new Parse.Object("TestObject");
5555
a.set("b", b);
@@ -1478,7 +1478,7 @@ describe('Parse.Object testing', () => {
14781478
expectError(Parse.Error.MISSING_OBJECT_ID, done));
14791479
});
14801480

1481-
it("fetchAll error on deleted object", function(done) {
1481+
it_exclude_dbs(['postgres'])("fetchAll error on deleted object", function(done) {
14821482
var numItems = 11;
14831483
var container = new Container();
14841484
var subContainer = new Container();

spec/PurchaseValidation.spec.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ describe("test validate_receipt endpoint", () => {
135135
});
136136
});
137137

138-
it("should fail at appstore validation", (done) => {
139-
138+
it("should fail at appstore validation", done => {
140139
request.post({
141140
headers: {
142141
'X-Parse-Application-Id': 'test',

spec/helper.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict"
22
// Sets up a Parse API server for testing.
33

4-
jasmine.DEFAULT_TIMEOUT_INTERVAL = process.env.PARSE_SERVER_TEST_TIMEOUT || 3000;
4+
jasmine.DEFAULT_TIMEOUT_INTERVAL = process.env.PARSE_SERVER_TEST_TIMEOUT || 5000;
55

66
var cache = require('../src/cache').default;
77
var DatabaseAdapter = require('../src/DatabaseAdapter');

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ export class PostgresStorageAdapter {
133133
}
134134
})
135135
.then(() => this._client.query('INSERT INTO "_SCHEMA" ("className", "schema", "isParseClass") VALUES ($<className>, $<schema>, true)', { className, schema }))
136+
.then(() => schema);
136137
}
137138

138139
addFieldIfNotExists(className, fieldName, type) {
@@ -212,7 +213,7 @@ export class PostgresStorageAdapter {
212213
// rejection reason are TBD.
213214
getAllClasses() {
214215
return this._ensureSchemaCollectionExists()
215-
.then(() => this._client.map('SELECT * FROM "_SCHEMA"'), null, row => ({ className: row.className, ...row.schema }));
216+
.then(() => this._client.map('SELECT * FROM "_SCHEMA"', null, row => ({ className: row.className, ...row.schema })));
216217
}
217218

218219
// Return a promise for the schema with the given name, in Parse format. If
@@ -329,6 +330,10 @@ export class PostgresStorageAdapter {
329330
updatePatterns.push(`$${index}:name = $${index + 1}`);
330331
values.push(fieldName, fieldValue);
331332
index += 2;
333+
} else if (fieldValue.__type === 'Pointer') {
334+
updatePatterns.push(`$${index}:name = $${index + 1}`);
335+
values.push(fieldName, fieldValue.objectId);
336+
index += 2;
332337
} else {
333338
return Promise.reject(new Parse.Error(Parse.Error.OPERATION_FORBIDDEN, `Postgres doesn't support update ${JSON.stringify(fieldValue)} yet`));
334339
}
@@ -352,7 +357,10 @@ export class PostgresStorageAdapter {
352357
let where = buildWhereClause({ schema, query, index: 2 })
353358
values.push(...where.values);
354359

355-
const qs = `SELECT * FROM $1:name WHERE ${where.pattern} ${limit !== undefined ? `LIMIT $${values.length + 1}` : ''}`;
360+
const wherePattern = where.pattern.length > 0 ? `WHERE ${where.pattern}` : '';
361+
const limitPattern = limit !== undefined ? `LIMIT $${values.length + 1}` : '';
362+
363+
const qs = `SELECT * FROM $1:name ${wherePattern} ${limitPattern}`;
356364
if (limit !== undefined) {
357365
values.push(limit);
358366
}
@@ -408,7 +416,14 @@ export class PostgresStorageAdapter {
408416

409417
// Executes a count.
410418
count(className, schema, query) {
411-
return Promise.reject('Not implemented yet.')
419+
let values = [className];
420+
let where = buildWhereClause({ schema, query, index: 2 });
421+
values.push(...where.values);
422+
423+
const wherePattern = where.pattern.length > 0 ? `WHERE ${where.pattern}` : '';
424+
const qs = `SELECT COUNT(*) FROM $1:name ${wherePattern}`;
425+
return this._client.query(qs, values)
426+
.then(result => parseInt(result[0].count))
412427
}
413428
}
414429

src/Controllers/SchemaController.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -408,23 +408,25 @@ class SchemaController {
408408
return Promise.resolve(this);
409409
}
410410
// We don't have this class. Update the schema
411-
return this.addClassIfNotExists(className).then(() => {
412-
// The schema update succeeded. Reload the schema
413-
return this.reloadData();
414-
}, error => {
411+
return this.addClassIfNotExists(className)
412+
// The schema update succeeded. Reload the schema
413+
.then(() => this.reloadData())
414+
.catch(error => {
415415
// The schema update failed. This can be okay - it might
416416
// have failed because there's a race condition and a different
417417
// client is making the exact same schema update that we want.
418418
// So just reload the schema.
419419
return this.reloadData();
420-
}).then(() => {
420+
})
421+
.then(() => {
421422
// Ensure that the schema now validates
422423
if (this.data[className]) {
423424
return this;
424425
} else {
425426
throw new Parse.Error(Parse.Error.INVALID_JSON, `Failed to add ${className}`);
426427
}
427-
}, error => {
428+
})
429+
.catch(error => {
428430
// The schema still doesn't validate. Give up
429431
throw new Parse.Error(Parse.Error.INVALID_JSON, 'schema class name does not revalidate');
430432
});

0 commit comments

Comments
 (0)