diff --git a/core/server/data/migrations/versions/2.0/1-rename-amp-column.js b/core/server/data/migrations/versions/2.0/1-rename-amp-column.js new file mode 100644 index 0000000000..9096628eb1 --- /dev/null +++ b/core/server/data/migrations/versions/2.0/1-rename-amp-column.js @@ -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); + }); + } + }); +}; diff --git a/core/server/data/migrations/versions/2.0/2-update-posts.js b/core/server/data/migrations/versions/2.0/2-update-posts.js new file mode 100644 index 0000000000..04e7f88c75 --- /dev/null +++ b/core/server/data/migrations/versions/2.0/2-update-posts.js @@ -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}); + }); +};