Skip to content

Commit

Permalink
feat(migrations): add keep-alive flag
Browse files Browse the repository at this point in the history
* Add a keep-alive option to all migration commands.

This prevents the migration commands from closing the database connection which allows for using an in-memory database during testing. Without this the connection is closed after the migrations are run and the next connection starts with an empty database.

* Fix lint errors.
  • Loading branch information
nrvalliere authored and thetutlage committed Mar 3, 2018
1 parent 29b9161 commit 88423f9
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 8 deletions.
9 changes: 8 additions & 1 deletion commands/MigrationRefresh.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class MigrationRefresh extends BaseMigration {
{ -f, --force: Forcefully run migrations in production }
{ -s, --silent: Silent the migrations output }
{ --log: Log SQL queries instead of executing them }
{ -a, --keep-alive: Do not close the database connection }
`
}

Expand All @@ -52,11 +53,17 @@ class MigrationRefresh extends BaseMigration {
* @param {Boolean} options.log
* @param {Boolean} options.force
* @param {Boolean} options.silent
* @param {Boolean} options.keepAlive
*
* @return {void|Array}
*/
async handle (args, { log, force, silent }) {
async handle (args, { log, force, silent, keepAlive }) {
this._validateState(force)

if (keepAlive) {
this.migration.keepAlive()
}

await ace.call('migration:reset', {}, { log, force, silent })
await ace.call('migration:run', {}, { log, force, silent })
}
Expand Down
8 changes: 7 additions & 1 deletion commands/MigrationReset.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class MigrationReset extends BaseMigration {
{ -f, --force: Forcefully run migrations in production }
{ -s, --silent: Silent the migrations output }
{ --log: Log SQL queries instead of executing them }
{ -a, --keep-alive: Do not close the database connection }
`
}

Expand All @@ -52,13 +53,18 @@ class MigrationReset extends BaseMigration {
* @param {Boolean} options.log
* @param {Boolean} options.force
* @param {Boolean} options.silent
* @param {Boolean} options.keepAlive
*
* @return {void|Array}
*/
async handle (args, { log, force, silent }) {
async handle (args, { log, force, silent, keepAlive }) {
try {
this._validateState(force)

if (keepAlive) {
this.migration.keepAlive()
}

const startTime = process.hrtime()
const { migrated, status, queries } = await this.migration.down(this._getSchemaFiles(), 0, log)

Expand Down
8 changes: 7 additions & 1 deletion commands/MigrationRollback.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class MirationRollback extends BaseMigration {
{ -f, --force: Forcefully run migrations in production }
{ -s, --silent: Silent the migrations output }
{ --log: Log SQL queries instead of executing them }
{ -a, --keep-alive: Do not close the database connection }
`
}

Expand All @@ -54,15 +55,20 @@ class MirationRollback extends BaseMigration {
* @param {Boolean} options.force
* @param {Number} options.batch
* @param {Boolean} options.silent
* @param {Boolean} options.keepAlive
*
* @return {void|Array}
*/
async handle (args, { log, force, batch, silent }) {
async handle (args, { log, force, batch, silent, keepAlive }) {
try {
batch = batch ? Number(batch) : null

this._validateState(force)

if (keepAlive) {
this.migration.keepAlive()
}

const startTime = process.hrtime()
const { migrated, status, queries } = await this.migration.down(this._getSchemaFiles(), batch, log)

Expand Down
8 changes: 7 additions & 1 deletion commands/MigrationRun.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class MigrationRun extends BaseMigration {
{ -f, --force: Forcefully run migrations in production }
{ -s, --silent: Silent the migrations output }
{ --log: Log SQL queries instead of executing them }
{ -a, --keep-alive: Do not close the database connection }
`
}

Expand All @@ -52,13 +53,18 @@ class MigrationRun extends BaseMigration {
* @param {Boolean} options.log
* @param {Boolean} options.force
* @param {Boolean} options.silent
* @param {Boolean} options.keepAlive
*
* @return {void|Array}
*/
async handle (args, { log, force, silent }) {
async handle (args, { log, force, silent, keepAlive }) {
try {
this._validateState(force)

if (keepAlive) {
this.migration.keepAlive()
}

const startTime = process.hrtime()
const { migrated, status, queries } = await this.migration.up(this._getSchemaFiles(), log)

Expand Down
14 changes: 12 additions & 2 deletions commands/MigrationStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ class MigrationStatus extends BaseMigration {
* @return {String}
*/
static get signature () {
return 'migration:status'
return `
migration:status
{ -a, --keep-alive: Do not close the database connection }
`
}

/**
Expand All @@ -40,9 +43,16 @@ class MigrationStatus extends BaseMigration {
*
* @method handle
*
* @param {Object} args
* @param {Boolean} options.keepAlive
*
* @return {void|Array}
*/
async handle () {
async handle (args, {keepAlive}) {
if (keepAlive) {
this.migration.keepAlive()
}

try {
const migrations = await this.migration.status(this._getSchemaFiles())
const head = ['File name', 'Migrated', 'Batch']
Expand Down
24 changes: 22 additions & 2 deletions src/Migration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Migration {
this.db = Database
this._migrationsTable = Config.get('database.migrationsTable', 'adonis_schema')
this._lockTable = `${this._migrationsTable}_lock`
this.isKeepAliveEnabled = false
}

/**
Expand Down Expand Up @@ -301,7 +302,23 @@ class Migration {
*/
async _cleanup () {
await this._removeLock()
this.db.close()

if (!this.isKeepAliveEnabled) {
this.db.close()
}
}

/**
* Enable or disable keepAlive, which prevents the database connection from being closed.
*
* @method keepAlive
*
* @param {boolean}enabled
*
* @return {void}
*/
keepAlive (enabled = true) {
this.isKeepAliveEnabled = enabled
}

/**
Expand Down Expand Up @@ -456,7 +473,10 @@ class Migration {
.table(this._migrationsTable)
.orderBy('name')

this.db.close()
if (!this.isKeepAliveEnabled) {
this.db.close()
}

return _.map(schemas, (schema, name) => {
const migration = _.find(migrated, (mig) => mig.name === name)
return {
Expand Down

0 comments on commit 88423f9

Please sign in to comment.