0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Allow forUpdate for any model

no issue

- this was only supported for the Post Model until now
- locking should be possible for every resource depending on the use case
This commit is contained in:
kirrg001 2017-11-14 00:18:59 +01:00 committed by Kevin Ansfield
parent 90d6ac5a0e
commit 4b21fc1d59
2 changed files with 22 additions and 25 deletions

View file

@ -125,6 +125,25 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
return validation.validateSchema(this.tableName, this.toJSON()); return validation.validateSchema(this.tableName, this.toJSON());
}, },
/**
* http://knexjs.org/#Builder-forUpdate
* https://dev.mysql.com/doc/refman/5.7/en/innodb-locking-reads.html
*
* Lock target collection/model for further update operations.
* This avoids collisions and possible content override cases.
*/
onFetching: function onFetching(model, columns, options) {
if (options.forUpdate && options.transacting) {
options.query.forUpdate();
}
},
onFetchingCollection: function onFetchingCollection(model, columns, options) {
if (options.forUpdate && options.transacting) {
options.query.forUpdate();
}
},
/** /**
* Adding resources implies setting these properties on the server side * Adding resources implies setting these properties on the server side
* - set `created_by` based on the context * - set `created_by` based on the context
@ -369,7 +388,7 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
*/ */
permittedOptions: function permittedOptions() { permittedOptions: function permittedOptions() {
// terms to whitelist for all methods. // terms to whitelist for all methods.
return ['context', 'include', 'transacting', 'importing']; return ['context', 'include', 'transacting', 'importing', 'forUpdate'];
}, },
/** /**

View file

@ -56,27 +56,6 @@ Post = ghostBookshelf.Model.extend({
return this.updateTags(model, response, options); return this.updateTags(model, response, options);
}, },
/**
* http://knexjs.org/#Builder-forUpdate
* https://dev.mysql.com/doc/refman/5.7/en/innodb-locking-reads.html
*
* Lock target collection/model for further update operations.
* This avoids collisions and possible content override cases.
*
* `forUpdate` is only supported for posts right now
*/
onFetching: function onFetching(model, columns, options) {
if (options.forUpdate && options.transacting) {
options.query.forUpdate();
}
},
onFetchingCollection: function onFetchingCollection(model, columns, options) {
if (options.forUpdate && options.transacting) {
options.query.forUpdate();
}
},
onUpdated: function onUpdated(model) { onUpdated: function onUpdated(model) {
model.statusChanging = model.get('status') !== model.updated('status'); model.statusChanging = model.get('status') !== model.updated('status');
model.isPublished = model.get('status') === 'published'; model.isPublished = model.get('status') === 'published';
@ -624,10 +603,9 @@ Post = ghostBookshelf.Model.extend({
// whitelists for the `options` hash argument on methods, by method name. // whitelists for the `options` hash argument on methods, by method name.
// these are the only options that can be passed to Bookshelf / Knex. // these are the only options that can be passed to Bookshelf / Knex.
validOptions = { validOptions = {
findOne: ['columns', 'importing', 'withRelated', 'require', 'forUpdate'], findOne: ['columns', 'importing', 'withRelated', 'require'],
findPage: ['page', 'limit', 'columns', 'filter', 'order', 'status', 'staticPages'], findPage: ['page', 'limit', 'columns', 'filter', 'order', 'status', 'staticPages'],
findAll: ['columns', 'filter', 'forUpdate'], findAll: ['columns', 'filter']
edit: ['forUpdate']
}; };
// The post model additionally supports having a formats option // The post model additionally supports having a formats option