Skip to content

Commit

Permalink
feat(migrations): introduce a silent flag to silent the output
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Nov 22, 2017
1 parent 10023f7 commit c16abb8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 18 deletions.
15 changes: 15 additions & 0 deletions commands/BaseMigration.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ class BaseMigration extends Command {
throw new Error('Cannot run migrations in production. Use --force flag to continue')
}
}

/**
* Executes the function when conditional
* is false
*
* @method execIfNot
*
* @param {Boolean} conditional
* @param {Function} fn
*/
execIfNot (conditional, fn) {
if (!conditional) {
fn()
}
}
}

module.exports = BaseMigration
8 changes: 5 additions & 3 deletions commands/MigrationRefresh.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class MigrationRefresh extends BaseMigration {
return `
migration:refresh
{ -f, --force: Forcefully run migrations in production }
{ -s, --silent: Silent the migrations output }
{ --log: Log SQL queries instead of executing them }
`
}
Expand All @@ -50,13 +51,14 @@ class MigrationRefresh extends BaseMigration {
* @param {Object} args
* @param {Boolean} options.log
* @param {Boolean} options.force
* @param {Boolean} options.silent
*
* @return {void|Array}
*/
async handle (args, { log, force }) {
async handle (args, { log, force, silent }) {
this._validateState(force)
await ace.call('migration:reset', {}, { log, force })
await ace.call('migration:run', {}, { log, force })
await ace.call('migration:reset', {}, { log, force, silent })
await ace.call('migration:run', {}, { log, force, silent })
}
}

Expand Down
12 changes: 7 additions & 5 deletions commands/MigrationReset.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class MigrationReset extends BaseMigration {
return `
migration:reset
{ -f, --force: Forcefully run migrations in production }
{ -s, --silent: Silent the migrations output }
{ --log: Log SQL queries instead of executing them }
`
}
Expand All @@ -50,10 +51,11 @@ class MigrationReset extends BaseMigration {
* @param {Object} args
* @param {Boolean} options.log
* @param {Boolean} options.force
* @param {Boolean} options.silent
*
* @return {void|Array}
*/
async handle (args, { log, force }) {
async handle (args, { log, force, silent }) {
try {
this._validateState(force)

Expand All @@ -64,15 +66,15 @@ class MigrationReset extends BaseMigration {
* Tell user that there is nothing to migrate
*/
if (status === 'skipped') {
this.info('Already at the last batch')
this.execIfNot(silent, () => this.info('Already at the last batch'))
}

/**
* Log files that been migrated successfully
*/
if (status === 'completed' && !queries) {
const endTime = process.hrtime(startTime)
migrated.forEach((name) => this.completed('rollback', `${name}.js`))
migrated.forEach((name) => this.execIfNot(silent, () => this.completed('rollback', `${name}.js`)))
this.success(`Reset completed in ${prettyHrTime(endTime)}`)
}

Expand All @@ -81,8 +83,8 @@ class MigrationReset extends BaseMigration {
*/
if (queries) {
_.each(queries, ({queries, name}) => {
console.log(this.chalk.magenta(`\n Queries for ${name}.js`))
_.each(queries, (query) => console.log(` ${query}`))
this.execIfNot(silent, () => console.log(this.chalk.magenta(`\n Queries for ${name}.js`)))
_.each(queries, (query) => this.execIfNot(silent, () => console.log(` ${query}`)))
console.log('\n')
})
}
Expand Down
12 changes: 7 additions & 5 deletions commands/MigrationRollback.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class MirationRollback extends BaseMigration {
migration:rollback
{ -b, --batch=@value: Rollback upto a specific batch number }
{ -f, --force: Forcefully run migrations in production }
{ -s, --silent: Silent the migrations output }
{ --log: Log SQL queries instead of executing them }
`
}
Expand All @@ -52,10 +53,11 @@ class MirationRollback extends BaseMigration {
* @param {Boolean} options.log
* @param {Boolean} options.force
* @param {Number} options.batch
* @param {Boolean} options.silent
*
* @return {void|Array}
*/
async handle (args, { log, force, batch }) {
async handle (args, { log, force, batch, silent }) {
try {
batch = batch ? Number(batch) : null

Expand All @@ -68,15 +70,15 @@ class MirationRollback extends BaseMigration {
* Tell user that there is nothing to migrate
*/
if (status === 'skipped') {
this.info('Already at the last batch')
this.execIfNot(silent, () => this.info('Already at the last batch'))
}

/**
* Log files that been migrated successfully
*/
if (status === 'completed' && !queries) {
const endTime = process.hrtime(startTime)
migrated.forEach((name) => this.completed('rollback', `${name}.js`))
migrated.forEach((name) => this.execIfNot(silent, () => this.completed('rollback', `${name}.js`)))
this.success(`Rollback completed in ${prettyHrTime(endTime)}`)
}

Expand All @@ -85,8 +87,8 @@ class MirationRollback extends BaseMigration {
*/
if (queries) {
_.each(queries, ({queries, name}) => {
console.log(this.chalk.magenta(`\n Queries for ${name}.js`))
_.each(queries, (query) => console.log(` ${query}`))
this.execIfNot(silent, () => console.log(this.chalk.magenta(`\n Queries for ${name}.js`)))
_.each(queries, (query) => this.execIfNot(silent, () => console.log(` ${query}`)))
console.log('\n')
})
}
Expand Down
14 changes: 9 additions & 5 deletions commands/MigrationRun.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class MigrationRun extends BaseMigration {
return `
migration:run
{ -f, --force: Forcefully run migrations in production }
{ -s, --silent: Silent the migrations output }
{ --log: Log SQL queries instead of executing them }
`
}
Expand All @@ -49,10 +50,12 @@ class MigrationRun extends BaseMigration {
*
* @param {Object} args
* @param {Boolean} options.log
* @param {Boolean} options.force
* @param {Boolean} options.silent
*
* @return {void|Array}
*/
async handle (args, { log, force }) {
async handle (args, { log, force, silent }) {
try {
this._validateState(force)

Expand All @@ -63,15 +66,15 @@ class MigrationRun extends BaseMigration {
* Tell user that there is nothing to migrate
*/
if (status === 'skipped') {
this.info('Nothing to migrate')
this.execIfNot(silent, () => this.info('Nothing to migrate'))
}

/**
* Log files that been migrated successfully
*/
if (status === 'completed' && !queries) {
const endTime = process.hrtime(startTime)
migrated.forEach((name) => this.completed('migrate', `${name}.js`))
migrated.forEach((name) => this.execIfNot(silent, () => this.completed('migrate', `${name}.js`)))
this.success(`Database migrated successfully in ${prettyHrTime(endTime)}`)
}

Expand All @@ -80,8 +83,8 @@ class MigrationRun extends BaseMigration {
*/
if (queries) {
_.each(queries, ({queries, name}) => {
console.log(this.chalk.magenta(`\n Queries for ${name}.js`))
_.each(queries, (query) => console.log(` ${query}`))
this.execIfNot(silent, () => console.log(this.chalk.magenta(`\n Queries for ${name}.js`)))
_.each(queries, (query) => this.execIfNot(silent, () => console.log(` ${query}`)))
console.log('\n')
})
}
Expand All @@ -91,6 +94,7 @@ class MigrationRun extends BaseMigration {
}
} catch (error) {
console.log(error)
process.exit(1)
}
}
}
Expand Down

0 comments on commit c16abb8

Please sign in to comment.