Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(migrations): return migrations class body instead of instance
Browse files Browse the repository at this point in the history
migrations export returns the class body instead of instance, now consumer can create a new instance
for each migrations operation
thetutlage committed Jun 26, 2016
1 parent 7b840dc commit 358aeb6
Showing 9 changed files with 77 additions and 52 deletions.
7 changes: 3 additions & 4 deletions src/Commands/Refresh.js
Original file line number Diff line number Diff line change
@@ -48,15 +48,14 @@ class Refresh extends Command {
try {
this.checkEnv(flags.force)
const migrationsFiles = this.loadFiles(this.helpers.migrationsPath())
yield this.migrations.down(migrationsFiles, 0)
const response = yield this.migrations.up(migrationsFiles)
const MigrationsRunner = this.migrations
yield new MigrationsRunner().down(migrationsFiles, 0)
const response = yield new MigrationsRunner().up(migrationsFiles)
const successMessage = 'Migrations successfully refreshed.'
const infoMessage = 'Already at the latest batch.'
this.log(response.status, successMessage, infoMessage)
} catch (e) {
this.error(e)
} finally {
this.migrations.database.close()
}
}
}
5 changes: 2 additions & 3 deletions src/Commands/Reset.js
Original file line number Diff line number Diff line change
@@ -48,15 +48,14 @@ class Reset extends Command {
this.checkEnv(flags.force)

const migrationsFiles = this.loadFiles(this.helpers.migrationsPath())
const response = yield this.migrations.down(migrationsFiles, 0)
const MigrationsRunner = this.migrations
const response = yield new MigrationsRunner().down(migrationsFiles, 0)

const successMessage = 'Rolled back to latest batch.'
const infoMessage = 'Already at the latest batch.'
this.log(response.status, successMessage, infoMessage)
} catch (e) {
this.error(e)
} finally {
this.migrations.database.close()
}
}
}
5 changes: 2 additions & 3 deletions src/Commands/Rollback.js
Original file line number Diff line number Diff line change
@@ -49,15 +49,14 @@ class Rollback extends Command {
this.checkEnv(flags.force)

const migrationsFiles = this.loadFiles(this.helpers.migrationsPath())
const response = yield this.migrations.down(migrationsFiles, flags.batch)
const MigrationsRunner = this.migrations
const response = yield new MigrationsRunner().down(migrationsFiles, flags.batch)

const successMessage = flags.batch ? `Rolled back to ${flags.batch} batch.` : 'Rolled back to previous batch.'
const infoMessage = 'Already at the latest batch.'
this.log(response.status, successMessage, infoMessage)
} catch (e) {
this.error(e)
} finally {
this.migrations.database.close()
}
}
}
6 changes: 2 additions & 4 deletions src/Commands/Run.js
Original file line number Diff line number Diff line change
@@ -49,16 +49,14 @@ class Run extends Command {

const selectedFiles = flags.files ? flags.files.split(',') : null
const migrationsFiles = this.loadFiles(this.helpers.migrationsPath(), selectedFiles)

const response = yield this.migrations.up(migrationsFiles)
const MigrationsRunner = this.migrations
const response = yield new MigrationsRunner().up(migrationsFiles)

const successMessage = 'Database migrated successfully.'
const infoMessage = 'Nothing to migrate.'
this.log(response.status, successMessage, infoMessage)
} catch (e) {
this.error(e)
} finally {
this.migrations.database.close()
}
}
}
2 changes: 0 additions & 2 deletions src/Commands/Seed.js
Original file line number Diff line number Diff line change
@@ -56,8 +56,6 @@ class Seed extends Command {
this.success(`${this.icon('success')} seeded database successfully`)
} catch (e) {
this.error(e)
} finally {
this.migrations.database.close()
}
}
}
6 changes: 4 additions & 2 deletions src/Commands/Status.js
Original file line number Diff line number Diff line change
@@ -44,14 +44,16 @@ class Status extends Command {
* @public
*/
* handle (options, flags) {
const MigrationsRunner = this.migrations
const migrationsRunner = new MigrationsRunner()
try {
const migrationsFiles = this.loadFiles(this.helpers.migrationsPath())
const response = yield this.migrations.status(migrationsFiles)
const response = yield migrationsRunner.status(migrationsFiles)
this.table(['Migration', 'Status'], response)
} catch (e) {
this.error(e)
} finally {
this.migrations.database.close()
migrationsRunner.database.close()
}
}
}
18 changes: 14 additions & 4 deletions src/Migrations/index.js
Original file line number Diff line number Diff line change
@@ -14,12 +14,14 @@ const mixin = require('es6-class-mixin')
const Lock = require('./Mixins/Lock')
const Migrate = require('./Mixins/Migrate')
const Batch = require('./Mixins/Batch')
let ConfigReference = null
let DatabaseReference = null

class Migrations {

constructor (Database, Config) {
this.database = Database
this.migrationsTable = Config.get('database.migrationsTable', 'adonis_schema')
constructor () {
this.database = DatabaseReference
this.migrationsTable = ConfigReference.get('database.migrationsTable', 'adonis_schema')
this.lockTable = `${this.migrationsTable}_lock`
this.migrations = []
}
@@ -168,4 +170,12 @@ class Migrations {

class ExtendedMigrations extends mixin(Migrations, Lock, Migrate, Batch) {}

module.exports = ExtendedMigrations
class MigrationsManager {
constructor (Database, Config) {
DatabaseReference = Database
ConfigReference = Config
return ExtendedMigrations
}
}

module.exports = MigrationsManager
4 changes: 2 additions & 2 deletions test/acceptance/setup.js
Original file line number Diff line number Diff line change
@@ -73,9 +73,9 @@ setup.end = function * () {

setup.migrate = function * (schemas, direction) {
const Migrations = Ioc.use('Adonis/Src/Migrations')
yield Migrations[direction](schemas)
yield new Migrations()[direction](schemas)
if (direction === 'down') {
yield Migrations.database.schema.dropTable('adonis_migrations')
yield new Migrations().database.schema.dropTable('adonis_migrations')
}
}

76 changes: 48 additions & 28 deletions test/unit/migrations.spec.js
Original file line number Diff line number Diff line change
@@ -45,7 +45,8 @@ describe('Migrations', function () {
})

it('should make migrations table', function * () {
const runner = new Migrations(Database, Config)
const Runner = new Migrations(Database, Config)
const runner = new Runner()
yield runner._makeMigrationsTable()
const columns = yield runner.database.table('adonis_migrations').columnInfo()
expect(columns).to.be.an('object')
@@ -54,7 +55,8 @@ describe('Migrations', function () {
})

it('should make lock table', function * () {
const runner = new Migrations(Database, Config)
const Runner = new Migrations(Database, Config)
const runner = new Runner()
yield runner._makeLockTable()
const columns = yield runner.database.table('adonis_migrations_lock').columnInfo()
expect(columns).to.be.an('object')
@@ -63,15 +65,17 @@ describe('Migrations', function () {
})

it('should return false when there is no lock', function * () {
const runner = new Migrations(Database, Config)
const Runner = new Migrations(Database, Config)
const runner = new Runner()
yield runner._makeLockTable()
const isLocked = yield runner._checkLock()
expect(isLocked).to.equal(false)
yield runner.database.schema.dropTable('adonis_migrations_lock')
})

it('should add a lock to the lock table', function * () {
const runner = new Migrations(Database, Config)
const Runner = new Migrations(Database, Config)
const runner = new Runner()
yield runner._makeLockTable()
yield runner._addLock()
const lock = yield runner.database.table('adonis_migrations_lock').where('is_locked', 1)
@@ -80,7 +84,8 @@ describe('Migrations', function () {
})

it('should throw an error when a table has been locked', function * () {
const runner = new Migrations(Database, Config)
const Runner = new Migrations(Database, Config)
const runner = new Runner()
yield runner._makeLockTable()
yield runner._addLock()
try {
@@ -93,7 +98,8 @@ describe('Migrations', function () {
})

it('should free an added lock by deleting the lock table', function * () {
const runner = new Migrations(Database, Config)
const Runner = new Migrations(Database, Config)
const runner = new Runner()
yield runner._makeLockTable()
yield runner._addLock()
yield runner._freeLock()
@@ -106,7 +112,8 @@ describe('Migrations', function () {
})

it('should return diff of migrations to be executed', function * () {
const runner = new Migrations(Database, Config)
const Runner = new Migrations(Database, Config)
const runner = new Runner()
class Users extends Schema {
up () {
this.table('users', function (table) {
@@ -122,7 +129,8 @@ describe('Migrations', function () {
})

it('should return diff of migrations to be rollback', function * () {
const runner = new Migrations(Database, Config)
const Runner = new Migrations(Database, Config)
const runner = new Runner()
class Users extends Schema {
up () {
this.table('users', function (table) {
@@ -141,7 +149,8 @@ describe('Migrations', function () {
})

it('should return migration status', function * () {
const runner = new Migrations(Database, Config)
const Runner = new Migrations(Database, Config)
const runner = new Runner()
class Users extends Schema {
up () {
this.create('users', function (table) {
@@ -175,7 +184,8 @@ describe('Migrations', function () {
})

it('should migrate the database by calling the up method', function * () {
const runner = new Migrations(Database, Config)
const Runner = new Migrations(Database, Config)
const runner = new Runner()
class Users extends Schema {
up () {
this.create('users', function (table) {
@@ -196,8 +206,9 @@ describe('Migrations', function () {
})

it('should rollback the recently executed migrations', function * () {
const runner = new Migrations(Database, Config)
const rollbackRunner = new Migrations(Database, Config)
const Runner = new Migrations(Database, Config)
const runner = new Runner()
const rollbackRunner = new Runner()
class Users extends Schema {
up () {
this.create('users', function (table) {
@@ -230,7 +241,8 @@ describe('Migrations', function () {
})

it('should be able to use a different connection for a given schema', function * () {
const runner = new Migrations(Database, Config)
const Runner = new Migrations(Database, Config)
const runner = new Runner()
class Accounts extends Schema {
static get connection () {
return 'alternateConnection'
@@ -262,8 +274,9 @@ describe('Migrations', function () {
})

it('should be able to rollback migrations when schema is using a different connection', function * () {
const runner = new Migrations(Database, Config)
const rollbackRunner = new Migrations(Database, Config)
const Runner = new Migrations(Database, Config)
const runner = new Runner()
const rollbackRunner = new Runner()
class Accounts extends Schema {
static get connection () {
return 'alternateConnection'
@@ -341,19 +354,20 @@ describe('Migrations', function () {
const migrationsB2 = {'2016-01-30_create_accouts_table': Account}
let allMigs = {}
_.merge(allMigs, migrationsB1, migrationsB2)
let runner, result, rollback
const Runner = new Migrations(Database, Config)

runner = new Migrations(Database, Config)
let runner, result, rollback
runner = new Runner()
result = yield runner.up(migrationsB1)
expect(result.status).to.equal('completed')
expect(result.migrated).deep.equal(_.keys(migrationsB1))

runner = new Migrations(Database, Config)
runner = new Runner()
result = yield runner.up(migrationsB2)
expect(result.status).to.equal('completed')
expect(result.migrated).deep.equal(_.keys(migrationsB2))

runner = new Migrations(Database, Config)
runner = new Runner()
rollback = yield runner.down(allMigs)
expect(rollback.status).to.equal('completed')
expect(rollback.migrated).deep.equal(_.keys(migrationsB2))
@@ -407,19 +421,20 @@ describe('Migrations', function () {
const migrationsB2 = {'2016-01-30_create_accouts_table': Account}
let allMigs = {}
_.merge(allMigs, migrationsB1, migrationsB2)
const Runner = new Migrations(Database, Config)
let runner, result, rollback

runner = new Migrations(Database, Config)
runner = new Runner()
result = yield runner.up(migrationsB1)
expect(result.status).to.equal('completed')
expect(result.migrated).deep.equal(_.keys(migrationsB1))

runner = new Migrations(Database, Config)
runner = new Runner()
result = yield runner.up(migrationsB2)
expect(result.status).to.equal('completed')
expect(result.migrated).deep.equal(_.keys(migrationsB2))

runner = new Migrations(Database, Config)
runner = new Runner()
rollback = yield runner.down(allMigs, 0)
expect(rollback.status).to.equal('completed')
expect(rollback.migrated).deep.equal(_.reverse(_.keys(allMigs)))
@@ -435,7 +450,8 @@ describe('Migrations', function () {
})

it('should have access to knex fn inside the schema class', function * () {
const runner = new Migrations(Database, Config)
const Runner = new Migrations(Database, Config)
const runner = new Runner()
let fn = null
class Users extends Schema {
up () {
@@ -452,7 +468,8 @@ describe('Migrations', function () {
})

it('should be able to define soft delete field inside migrations', function * () {
const runner = new Migrations(Database, Config)
const Runner = new Migrations(Database, Config)
const runner = new Runner()
class Users extends Schema {
up () {
this.create('users', (table) => {
@@ -472,7 +489,8 @@ describe('Migrations', function () {
})

it('should be able to define nullableTimestamps inside migrations', function * () {
const runner = new Migrations(Database, Config)
const Runner = new Migrations(Database, Config)
const runner = new Runner()
class Users extends Schema {
up () {
this.create('users', (table) => {
@@ -495,7 +513,8 @@ describe('Migrations', function () {
})

it('should be able to run multiple commands inside a single up method', function * () {
const runner = new Migrations(Database, Config)
const Runner = new Migrations(Database, Config)
const runner = new Runner()
class Users extends Schema {
up () {
this.create('users', (table) => {
@@ -519,7 +538,8 @@ describe('Migrations', function () {
})

it('should be able to run multiple commands inside a single down method', function * () {
const runner = new Migrations(Database, Config)
const Runner = new Migrations(Database, Config)
const runner = new Runner()
class Users extends Schema {
up () {
this.create('users', (table) => {
@@ -542,7 +562,7 @@ describe('Migrations', function () {
const accountsInfo = yield runner.database.table('accounts').columnInfo()
expect(usersInfo.id).to.be.an('object')
expect(accountsInfo.id).to.be.an('object')
const runner1 = new Migrations(Database, Config)
const runner1 = new Runner()
yield runner1.down(migrations)
const usersTable = yield runner1.database.table('users').columnInfo()
const accountsTable = yield runner1.database.table('accounts').columnInfo()

0 comments on commit 358aeb6

Please sign in to comment.