From 88423f99eccb7a1f2d3051226a88a05eb77509db Mon Sep 17 00:00:00 2001 From: nrvalliere <4960075+nrvalliere@users.noreply.github.com> Date: Sat, 3 Mar 2018 03:26:57 -0500 Subject: [PATCH] feat(migrations): add keep-alive flag * 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. --- commands/MigrationRefresh.js | 9 ++++++++- commands/MigrationReset.js | 8 +++++++- commands/MigrationRollback.js | 8 +++++++- commands/MigrationRun.js | 8 +++++++- commands/MigrationStatus.js | 14 ++++++++++++-- src/Migration/index.js | 24 ++++++++++++++++++++++-- 6 files changed, 63 insertions(+), 8 deletions(-) diff --git a/commands/MigrationRefresh.js b/commands/MigrationRefresh.js index e8ed52db..e3aa009b 100644 --- a/commands/MigrationRefresh.js +++ b/commands/MigrationRefresh.js @@ -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 } ` } @@ -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 }) } diff --git a/commands/MigrationReset.js b/commands/MigrationReset.js index d71e37a8..95ba1fdb 100644 --- a/commands/MigrationReset.js +++ b/commands/MigrationReset.js @@ -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 } ` } @@ -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) diff --git a/commands/MigrationRollback.js b/commands/MigrationRollback.js index c8408442..d5a891f2 100644 --- a/commands/MigrationRollback.js +++ b/commands/MigrationRollback.js @@ -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 } ` } @@ -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) diff --git a/commands/MigrationRun.js b/commands/MigrationRun.js index 6b039d5f..89cf39be 100644 --- a/commands/MigrationRun.js +++ b/commands/MigrationRun.js @@ -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 } ` } @@ -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) diff --git a/commands/MigrationStatus.js b/commands/MigrationStatus.js index 3f949e7c..4d84e63b 100644 --- a/commands/MigrationStatus.js +++ b/commands/MigrationStatus.js @@ -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 } + ` } /** @@ -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'] diff --git a/src/Migration/index.js b/src/Migration/index.js index b526b6e7..4fc82b98 100644 --- a/src/Migration/index.js +++ b/src/Migration/index.js @@ -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 } /** @@ -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 } /** @@ -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 {