0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

🐛 Added logic to avoid updating the updated_at and updated_by field when migrating (#9814)

no issue

- we have to explicitly reset the previous `updated_at` field, because Bookshelf auto-updates this field on each update
- we have to extend the condition to avoid updating the `updated_by` field
- detect and respect `options.migrating`
This commit is contained in:
Katharina Irrgang 2018-08-22 13:57:12 +02:00 committed by GitHub
parent 91950cfea0
commit 4b2ebee67b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 2 deletions

View file

@ -283,7 +283,7 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
*/
onUpdating: function onUpdating(newObj, attr, options) {
if (schema.tables[this.tableName].hasOwnProperty('updated_by')) {
if (!options.importing) {
if (!options.importing && !options.migrating) {
this.set('updated_by', this.contextUser(options));
}
}
@ -304,7 +304,9 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
// CASE: do not allow setting only the `updated_at` field, exception: importing
if (schema.tables[this.tableName].hasOwnProperty('updated_at') && !options.importing) {
if (newObj.hasChanged() && Object.keys(newObj.changed).length === 1 && newObj.changed.updated_at) {
if (options.migrating) {
newObj.set('updated_at', newObj.previous('updated_at'));
} else if (newObj.hasChanged() && Object.keys(newObj.changed).length === 1 && newObj.changed.updated_at) {
newObj.set('updated_at', newObj.previous('updated_at'));
}
}

View file

@ -268,6 +268,45 @@ describe('Unit: models/post', function () {
});
describe('edit', function () {
it('update post with options.migrating', function () {
const events = {
post: [],
tag: []
};
sandbox.stub(models.Post.prototype, 'emitChange').callsFake(function (event) {
events.post.push(event);
});
sandbox.stub(models.Tag.prototype, 'emitChange').callsFake(function (event) {
events.tag.push(event);
});
let originalUpdatedAt;
let originalUpdatedBy;
return models.Post.findOne({
id: testUtils.DataGenerator.forKnex.posts[3].id,
status: 'draft'
}, {withRelated: ['tags']})
.then((post) => {
originalUpdatedAt = post.get('updated_at');
originalUpdatedBy = post.get('updated_by');
// post will be updated, tags relation not
return models.Post.edit({
html: 'changed html'
}, _.merge({id: testUtils.DataGenerator.forKnex.posts[3].id, migrating: true}, testUtils.context.editor));
})
.then((post) => {
post.get('updated_at').should.eql(originalUpdatedAt);
post.get('updated_by').should.eql(originalUpdatedBy);
events.post.should.eql(['edited']);
events.tag.should.eql([]);
});
});
it('update post, relation has not changed', function () {
const events = {
post: [],