-
Notifications
You must be signed in to change notification settings - Fork 182
Add custom migration file option to up #251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,7 +58,8 @@ program | |
program | ||
.command("up") | ||
.description("run all pending database migrations") | ||
.option("-f --file <file>", "use a custom config file") | ||
.option("-c --custom <custom>", "use a custom config file") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The description of this option is the same as the above, maybe it was a mistake when copying the line above to add a new option... The correct description wouldn't be "migration file to run"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A typo. Fixed. |
||
.option("-f --file <file>", "use a file") | ||
.action(options => { | ||
global.options = options; | ||
migrateMongo.database | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,32 @@ | ||
const _ = require("lodash"); | ||
const pEachSeries = require("p-each-series"); | ||
const { promisify } = require("util"); | ||
const fnArgs = require('fn-args'); | ||
const fnArgs = require("fn-args"); | ||
|
||
const status = require("./status"); | ||
const config = require("../env/config"); | ||
const migrationsDir = require("../env/migrationsDir"); | ||
const hasCallback = require('../utils/has-callback'); | ||
const hasCallback = require("../utils/has-callback"); | ||
|
||
module.exports = async (db, client) => { | ||
const statusItems = await status(db); | ||
const pendingItems = _.filter(statusItems, { appliedAt: "PENDING" }); | ||
const migrated = []; | ||
|
||
const migrateItem = async item => { | ||
// console.log(pendingItems); | ||
const migrateItem = async (item) => { | ||
try { | ||
const migration = await migrationsDir.loadMigration(item.fileName); | ||
const up = hasCallback(migration.up) ? promisify(migration.up) : migration.up; | ||
const up = hasCallback(migration.up) | ||
? promisify(migration.up) | ||
: migration.up; | ||
|
||
if (hasCallback(migration.up) && fnArgs(migration.up).length < 3) { | ||
// support old callback-based migrations prior to migrate-mongo 7.x.x | ||
await up(db); | ||
} else { | ||
await up(db, client); | ||
} | ||
|
||
} catch (err) { | ||
const error = new Error( | ||
`Could not migrate up ${item.fileName}: ${err.message}` | ||
|
@@ -33,20 +35,35 @@ module.exports = async (db, client) => { | |
throw error; | ||
} | ||
|
||
const { changelogCollectionName } = await config.read(); | ||
const changelogCollection = db.collection(changelogCollectionName); | ||
if (item.appliedAt != "CUSTOM") | ||
{ | ||
// console.log("writing", item.appliedAt); | ||
const { changelogCollectionName } = await config.read(); | ||
const changelogCollection = db.collection(changelogCollectionName); | ||
|
||
const { fileName } = item; | ||
const appliedAt = new Date(); | ||
const { fileName } = item; | ||
const appliedAt = new Date(); | ||
|
||
try { | ||
await changelogCollection.insertOne({ fileName, appliedAt }); | ||
} catch (err) { | ||
throw new Error(`Could not update changelog: ${err.message}`); | ||
try { | ||
await changelogCollection.insertOne({ fileName, appliedAt }); | ||
} catch (err) { | ||
throw new Error(`Could not update changelog: ${err.message}`); | ||
} | ||
} | ||
|
||
migrated.push(item.fileName); | ||
}; | ||
|
||
await pEachSeries(pendingItems, migrateItem); | ||
// console.log("some", global.options); | ||
if (global.options.custom) { | ||
await migrateItem( | ||
{ fileName: global.options.custom, appliedAt: "CUSTOM" }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bdcorps IMO it would be better if we stored the date in appliedAt field. It will be useful to check when this particular migration was applied. Perhaps a boolean field will be better to differentiate between the regular and custom migration. |
||
false | ||
); | ||
} else { | ||
// console.log("all"); | ||
await pEachSeries(pendingItems, migrateItem); | ||
} | ||
|
||
return migrated; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module.exports = { | ||
async up(db, client) { | ||
// TODO write your migration here. | ||
// See https://github.com/seppevs/migrate-mongo/#creating-a-new-migration-script | ||
// Example: | ||
// await db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: true}}); | ||
}, | ||
|
||
async down(db, client) { | ||
// TODO write the statements to rollback your migration (if possible) | ||
// Example: | ||
// await db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: false}}); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module.exports = { | ||
async up(db, client) { | ||
// TODO write your migration here. | ||
// See https://github.com/seppevs/migrate-mongo/#creating-a-new-migration-script | ||
// Example: | ||
// await db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: true}}); | ||
}, | ||
|
||
async down(db, client) { | ||
// TODO write the statements to rollback your migration (if possible) | ||
// Example: | ||
// await db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: false}}); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bdcorps having a down custom migration also will be super useful. Specially for some quick hot fixes. See if you can consider a down migration support for custom migration.