Postgres adapter (#2012)
* Remove adaptiveCollection * Remove an adaptiveCollection use * Remove an adaptiveCollection * make adaptiveCollection private * Remove collection from mongoadapter * Move schema collection usage into mongo adapter * stop relying on mongo format for removing join tables * reduce usage of schemaCollection * remove uses of _collection * Move CLP setting into mongo adapter * remove all uses of schemaCollection * make schemaCollection private * remove transform from schemaCollection * rename some stuff * Tweak paramaters and stuff * reorder some params * reorder find() arguments * finishsh touching up argument order * Accept a database adapter as a parameter * First passing test with postgres! * Actually use the provided className * index on unique-indexes: c454180 Revert "Log objects rather than JSON stringified objects (#1922)" * Start dealing with test shittyness * Make specific server config for tests async * Fix email validation * Fix broken cloud code * Save callback to variable * undo * Fix tests * Setup travis * fix travis maybe * try removing db user * indentation? * remove postgres version setting * sudo maybe? * use postgres username * fix check for _PushStatus * excludes * remove db=mongo * allow postgres to fail * Fix allow failure * postgres 9.4 * Remove mongo implementations and fix test * Fix test leaving behind connections
This commit is contained in:
@@ -84,15 +84,11 @@ function DatabaseController(adapter, { skipValidation } = {}) {
|
||||
}
|
||||
|
||||
DatabaseController.prototype.WithoutValidation = function() {
|
||||
return new DatabaseController(this.adapter, {collectionPrefix: this.collectionPrefix, skipValidation: true});
|
||||
return new DatabaseController(this.adapter, { skipValidation: true });
|
||||
}
|
||||
|
||||
DatabaseController.prototype.schemaCollection = function() {
|
||||
return this.adapter.schemaCollection();
|
||||
};
|
||||
|
||||
DatabaseController.prototype.collectionExists = function(className) {
|
||||
return this.adapter.collectionExists(className);
|
||||
return this.adapter.classExists(className);
|
||||
};
|
||||
|
||||
DatabaseController.prototype.validateClassName = function(className) {
|
||||
@@ -105,16 +101,11 @@ DatabaseController.prototype.validateClassName = function(className) {
|
||||
return Promise.resolve();
|
||||
};
|
||||
|
||||
// Returns a promise for a schema object.
|
||||
// If we are provided a acceptor, then we run it on the schema.
|
||||
// If the schema isn't accepted, we reload it at most once.
|
||||
// Returns a promise for a schemaController.
|
||||
DatabaseController.prototype.loadSchema = function() {
|
||||
|
||||
if (!this.schemaPromise) {
|
||||
this.schemaPromise = this.schemaCollection().then(collection => {
|
||||
delete this.schemaPromise;
|
||||
return SchemaController.load(collection, this.adapter);
|
||||
});
|
||||
this.schemaPromise = SchemaController.load(this.adapter);
|
||||
this.schemaPromise.then(() => delete this.schemaPromise);
|
||||
}
|
||||
return this.schemaPromise;
|
||||
};
|
||||
@@ -232,11 +223,11 @@ DatabaseController.prototype.update = function(className, query, update, {
|
||||
}
|
||||
update = transformObjectACL(update);
|
||||
if (many) {
|
||||
return this.adapter.updateObjectsByQuery(className, query, schema, update);
|
||||
return this.adapter.updateObjectsByQuery(className, schema, query, update);
|
||||
} else if (upsert) {
|
||||
return this.adapter.upsertOneObject(className, query, schema, update);
|
||||
return this.adapter.upsertOneObject(className, schema, query, update);
|
||||
} else {
|
||||
return this.adapter.findOneAndUpdate(className, query, schema, update);
|
||||
return this.adapter.findOneAndUpdate(className, schema, query, update);
|
||||
}
|
||||
});
|
||||
})
|
||||
@@ -324,7 +315,7 @@ DatabaseController.prototype.addRelation = function(key, fromClassName, fromId,
|
||||
relatedId: toId,
|
||||
owningId : fromId
|
||||
};
|
||||
return this.adapter.upsertOneObject(`_Join:${key}:${fromClassName}`, doc, relationSchema, doc);
|
||||
return this.adapter.upsertOneObject(`_Join:${key}:${fromClassName}`, relationSchema, doc, doc);
|
||||
};
|
||||
|
||||
// Removes a relation.
|
||||
@@ -335,7 +326,7 @@ DatabaseController.prototype.removeRelation = function(key, fromClassName, fromI
|
||||
relatedId: toId,
|
||||
owningId: fromId
|
||||
};
|
||||
return this.adapter.deleteObjectsByQuery(`_Join:${key}:${fromClassName}`, doc, relationSchema)
|
||||
return this.adapter.deleteObjectsByQuery(`_Join:${key}:${fromClassName}`, relationSchema, doc)
|
||||
.catch(error => {
|
||||
// We don't care if they try to delete a non-existent relation.
|
||||
if (error.code == Parse.Error.OBJECT_NOT_FOUND) {
|
||||
@@ -380,7 +371,7 @@ DatabaseController.prototype.destroy = function(className, query, { acl } = {})
|
||||
}
|
||||
throw error;
|
||||
})
|
||||
.then(parseFormatSchema => this.adapter.deleteObjectsByQuery(className, query, parseFormatSchema))
|
||||
.then(parseFormatSchema => this.adapter.deleteObjectsByQuery(className, parseFormatSchema, query))
|
||||
.catch(error => {
|
||||
// When deleting sessions while changing passwords, don't throw an error if they don't have any sessions.
|
||||
if (className === "_Session" && error.code === Parse.Error.OBJECT_NOT_FOUND) {
|
||||
@@ -409,7 +400,7 @@ DatabaseController.prototype.create = function(className, object, { acl } = {})
|
||||
.then(() => this.handleRelationUpdates(className, null, object))
|
||||
.then(() => schemaController.enforceClassExists(className))
|
||||
.then(() => schemaController.getOneSchema(className, true))
|
||||
.then(schema => this.adapter.createObject(className, object, schema))
|
||||
.then(schema => this.adapter.createObject(className, schema, object))
|
||||
.then(result => sanitizeDatabaseResult(originalObject, result.ops[0]));
|
||||
})
|
||||
};
|
||||
@@ -434,7 +425,7 @@ DatabaseController.prototype.canAddField = function(schema, className, object, a
|
||||
// Returns a promise.
|
||||
DatabaseController.prototype.deleteEverything = function() {
|
||||
this.schemaPromise = null;
|
||||
return this.adapter.deleteAllSchemas();
|
||||
return this.adapter.deleteAllClasses();
|
||||
};
|
||||
|
||||
// Finds the keys in a query. Returns a Set. REST format only
|
||||
@@ -454,14 +445,14 @@ function keysForQuery(query) {
|
||||
// Returns a promise for a list of related ids given an owning id.
|
||||
// className here is the owning className.
|
||||
DatabaseController.prototype.relatedIds = function(className, key, owningId) {
|
||||
return this.adapter.find(joinTableName(className, key), { owningId }, relationSchema, {})
|
||||
return this.adapter.find(joinTableName(className, key), relationSchema, { owningId }, {})
|
||||
.then(results => results.map(result => result.relatedId));
|
||||
};
|
||||
|
||||
// Returns a promise for a list of owning ids given some related ids.
|
||||
// className here is the owning className.
|
||||
DatabaseController.prototype.owningIds = function(className, key, relatedIds) {
|
||||
return this.adapter.find(joinTableName(className, key), { relatedId: { '$in': relatedIds } }, relationSchema, {})
|
||||
return this.adapter.find(joinTableName(className, key), relationSchema, { relatedId: { '$in': relatedIds } }, {})
|
||||
.then(results => results.map(result => result.owningId));
|
||||
};
|
||||
|
||||
@@ -689,9 +680,9 @@ DatabaseController.prototype.find = function(className, query, {
|
||||
}
|
||||
validateQuery(query);
|
||||
if (count) {
|
||||
return this.adapter.count(className, query, schema);
|
||||
return this.adapter.count(className, schema, query);
|
||||
} else {
|
||||
return this.adapter.find(className, query, schema, { skip, limit, sort })
|
||||
return this.adapter.find(className, schema, query, { skip, limit, sort })
|
||||
.then(objects => objects.map(object => {
|
||||
object = untransformObjectACL(object);
|
||||
return filterSensitiveData(isMaster, aclGroup, className, object)
|
||||
@@ -727,19 +718,33 @@ const untransformObjectACL = ({_rperm, _wperm, ...output}) => {
|
||||
}
|
||||
|
||||
DatabaseController.prototype.deleteSchema = function(className) {
|
||||
return this.collectionExists(className)
|
||||
.then(exist => {
|
||||
if (!exist) {
|
||||
return Promise.resolve();
|
||||
return this.loadSchema()
|
||||
.then(schemaController => schemaController.getOneSchema(className))
|
||||
.catch(error => {
|
||||
if (error === undefined) {
|
||||
return { fields: {} };
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
return this.adapter.count(className)
|
||||
})
|
||||
.then(schema => {
|
||||
return this.collectionExists(className)
|
||||
.then(exist => this.adapter.count(className))
|
||||
.then(count => {
|
||||
if (count > 0) {
|
||||
throw new Parse.Error(255, `Class ${className} is not empty, contains ${count} objects, cannot drop schema.`);
|
||||
}
|
||||
return this.adapter.deleteOneSchema(className);
|
||||
return this.adapter.deleteClass(className);
|
||||
})
|
||||
});
|
||||
.then(wasParseCollection => {
|
||||
if (wasParseCollection) {
|
||||
const relationFieldNames = Object.keys(schema.fields).filter(fieldName => schema.fields[fieldName].type === 'Relation');
|
||||
return Promise.all(relationFieldNames.map(name => this.adapter.deleteClass(joinTableName(className, name))));
|
||||
} else {
|
||||
return Promise.resolve();
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
DatabaseController.prototype.addPointerPermissions = function(schema, className, operation, query, aclGroup = []) {
|
||||
|
||||
@@ -233,13 +233,11 @@ const injectDefaultSchema = schema => ({
|
||||
// Stores the entire schema of the app in a weird hybrid format somewhere between
|
||||
// the mongo format and the Parse format. Soon, this will all be Parse format.
|
||||
class SchemaController {
|
||||
_collection;
|
||||
_dbAdapter;
|
||||
data;
|
||||
perms;
|
||||
|
||||
constructor(collection, databaseAdapter) {
|
||||
this._collection = collection;
|
||||
constructor(databaseAdapter) {
|
||||
this._dbAdapter = databaseAdapter;
|
||||
|
||||
// this.data[className][fieldName] tells you the type of that field, in mongo format
|
||||
@@ -251,7 +249,7 @@ class SchemaController {
|
||||
reloadData() {
|
||||
this.data = {};
|
||||
this.perms = {};
|
||||
return this.getAllSchemas()
|
||||
return this.getAllClasses()
|
||||
.then(allSchemas => {
|
||||
allSchemas.forEach(schema => {
|
||||
this.data[schema.className] = schema.fields;
|
||||
@@ -269,8 +267,8 @@ class SchemaController {
|
||||
});
|
||||
}
|
||||
|
||||
getAllSchemas() {
|
||||
return this._dbAdapter.getAllSchemas()
|
||||
getAllClasses() {
|
||||
return this._dbAdapter.getAllClasses()
|
||||
.then(allSchemas => allSchemas.map(injectDefaultSchema));
|
||||
}
|
||||
|
||||
@@ -278,7 +276,7 @@ class SchemaController {
|
||||
if (allowVolatileClasses && volatileClasses.indexOf(className) > -1) {
|
||||
return Promise.resolve(this.data[className]);
|
||||
}
|
||||
return this._dbAdapter.getOneSchema(className)
|
||||
return this._dbAdapter.getClass(className)
|
||||
.then(injectDefaultSchema);
|
||||
}
|
||||
|
||||
@@ -295,12 +293,12 @@ class SchemaController {
|
||||
return Promise.reject(validationError);
|
||||
}
|
||||
|
||||
return this._collection.addSchema(className, fields, classLevelPermissions)
|
||||
return this._dbAdapter.createClass(className, { fields, classLevelPermissions })
|
||||
.catch(error => {
|
||||
if (error === undefined) {
|
||||
if (error && error.code === Parse.Error.DUPLICATE_VALUE) {
|
||||
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} already exists.`);
|
||||
} else {
|
||||
throw new Parse.Error(Parse.Error.INTERNAL_SERVER_ERROR, 'Database adapter error.');
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -383,7 +381,7 @@ class SchemaController {
|
||||
'schema is frozen, cannot add: ' + className);
|
||||
}
|
||||
// We don't have this class. Update the schema
|
||||
return this.addClassIfNotExists(className, []).then(() => {
|
||||
return this.addClassIfNotExists(className, {}).then(() => {
|
||||
// The schema update succeeded. Reload the schema
|
||||
return this.reloadData();
|
||||
}, () => {
|
||||
@@ -452,16 +450,8 @@ class SchemaController {
|
||||
return Promise.resolve();
|
||||
}
|
||||
validateCLP(perms, newSchema);
|
||||
let update = {
|
||||
_metadata: {
|
||||
class_permissions: perms
|
||||
}
|
||||
};
|
||||
update = {'$set': update};
|
||||
return this._collection.updateSchema(className, update).then(() => {
|
||||
// The update succeeded. Reload the schema
|
||||
return this.reloadData();
|
||||
});
|
||||
return this._dbAdapter.setClassLevelPermissions(className, perms)
|
||||
.then(() => this.reloadData());
|
||||
}
|
||||
|
||||
// Returns a promise that resolves successfully to the new schema
|
||||
@@ -511,7 +501,7 @@ class SchemaController {
|
||||
type = { type };
|
||||
}
|
||||
|
||||
return this._collection.addFieldIfNotExists(className, fieldName, type).then(() => {
|
||||
return this._dbAdapter.addFieldIfNotExists(className, fieldName, type).then(() => {
|
||||
// The update succeeded. Reload the schema
|
||||
return this.reloadData();
|
||||
}, () => {
|
||||
@@ -558,16 +548,16 @@ class SchemaController {
|
||||
if (!this.data[className][fieldName]) {
|
||||
throw new Parse.Error(255, `Field ${fieldName} does not exist, cannot delete.`);
|
||||
}
|
||||
|
||||
})
|
||||
.then(() => this.getOneSchema(className))
|
||||
.then(schema => {
|
||||
if (this.data[className][fieldName].type == 'Relation') {
|
||||
//For relations, drop the _Join table
|
||||
return database.adapter.deleteFields(className, [fieldName], [])
|
||||
.then(() => database.adapter.deleteOneSchema(`_Join:${fieldName}:${className}`));
|
||||
return database.adapter.deleteFields(className, schema, [fieldName])
|
||||
.then(() => database.adapter.deleteClass(`_Join:${fieldName}:${className}`));
|
||||
}
|
||||
|
||||
const fieldNames = [fieldName];
|
||||
const pointerFieldNames = this.data[className][fieldName].type === 'Pointer' ? [fieldName] : [];
|
||||
return database.adapter.deleteFields(className, fieldNames, pointerFieldNames);
|
||||
return database.adapter.deleteFields(className, schema, [fieldName]);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -696,8 +686,8 @@ class SchemaController {
|
||||
}
|
||||
|
||||
// Returns a promise for a new Schema.
|
||||
function load(collection, dbAdapter) {
|
||||
let schema = new SchemaController(collection, dbAdapter);
|
||||
const load = dbAdapter => {
|
||||
let schema = new SchemaController(dbAdapter);
|
||||
return schema.reloadData().then(() => schema);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user