-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
core/server/data/migrations/versions/2.0/1-rename-amp-column.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
core/server/data/migrations/versions/2.0/2-update-posts.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | ||
}); | ||
}; |