Skip to content

Commit

Permalink
Added database migration for amp -> comment_id rename
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kirrg001 committed Aug 16, 2018
1 parent 050e026 commit ed0dc19
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
36 changes: 36 additions & 0 deletions core/server/data/migrations/versions/2.0/1-rename-amp-column.js
Original file line number Diff line number Diff line change
@@ -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);
});
}
});
};
32 changes: 32 additions & 0 deletions core/server/data/migrations/versions/2.0/2-update-posts.js
Original file line number Diff line number Diff line change
@@ -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});
});
};

0 comments on commit ed0dc19

Please sign in to comment.