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

Added database migration for amp -> comment_id rename

refs #9742

- rename column from amp -> comment_id
- iterate over all posts and ensure we use the resource id or the original amp value
- provide down hook to undo this change
This commit is contained in:
kirrg001 2018-07-19 13:15:04 +02:00 committed by Katharina Irrgang
parent 050e026f8b
commit ed0dc191b2
2 changed files with 68 additions and 0 deletions

View file

@ -0,0 +1,36 @@
const common = require('../../../../lib/common'),
table = 'posts',
columnNameOld = 'amp',
columnNameNew = 'comment_id',
message1 = `Renaming column ${columnNameOld} to ${columnNameNew}`,
message2 = `Renaming column ${columnNameNew} to ${columnNameOld}`;
module.exports.up = function renameAmpColumn(options) {
const connection = options.connection;
common.logging.info(message1);
return connection.schema.hasColumn(table, columnNameOld)
.then((exists) => {
if (exists) {
return connection.schema.table(table, function (t) {
t.renameColumn(columnNameOld, columnNameNew);
});
}
});
};
module.exports.down = function renameCommentIdColumn(options) {
let connection = options.connection;
common.logging.info(message2);
return connection.schema.hasColumn(table, columnNameNew)
.then((exists) => {
if (exists) {
return connection.schema.table(table, function (t) {
t.renameColumn(columnNameNew, columnNameOld);
});
}
});
};

View file

@ -0,0 +1,32 @@
const _ = require('lodash'),
Promise = require('bluebird'),
common = require('../../../../lib/common'),
models = require('../../../../models'),
message1 = 'Updating post data (comment_id)';
module.exports.config = {
transaction: true
};
module.exports.up = function updatePosts(options) {
const postAllColumns = ['id', 'comment_id'];
let localOptions = _.merge({
context: {internal: true}
}, options);
common.logging.info(message1);
return models.Post.findAll(_.merge({columns: postAllColumns}, localOptions))
.then(function (posts) {
return Promise.map(posts.models, function (post) {
if (post.get('comment_id')) {
return Promise.resolve();
}
return models.Post.edit({
comment_id: post.id
}, _.merge({id: post.id}, localOptions));
}, {concurrency: 100});
});
};