Skip to content

Commit

Permalink
fix(migrations): allow multiple actions inside a single up/down method
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jul 16, 2017
1 parent a989fed commit 9105c35
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Migrations/Mixins/Migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ Migrate._migrateClass = function (schemaInstance, method) {
Migrate._executeSchema = function (schemaInstance, direction) {
schemaInstance[direction]()
_.each(schemaInstance.store, (schemas, method) => {
const connection = this.database.connection(schemaInstance.constructor.connection)
this._callSchemaActions(schemas, connection.schema, method)
this._callSchemaActions(schemas, schemaInstance.constructor.connection, method)
})
}

Expand All @@ -64,13 +63,14 @@ Migrate._executeSchema = function (schemaInstance, direction) {
* @method _callSchemaActions
*
* @param {Array} actions
* @param {Object} builder
* @param {Object} connection
* @param {String} method
*
* @private
*/
Migrate._callSchemaActions = function (actions, builder, method) {
Migrate._callSchemaActions = function (actions, connection, method) {
_.each(actions, (defination) => {
const builder = this.database.connection(connection).schema
const migration = builder[method](defination.key, this._schemaCallback(defination.callback))
this.migrations.push(migration)
})
Expand Down
57 changes: 57 additions & 0 deletions test/unit/migrations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,4 +493,61 @@ describe('Migrations', function () {
yield runner.database.schema.dropTable('users')
yield runner.database.schema.dropTable('adonis_migrations')
})

it('should be able to run multiple commands inside a single up method', function * () {
const runner = new Migrations(Database, Config)
class Users extends Schema {
up () {
this.create('users', (table) => {
table.increments()
})

this.create('accounts', (table) => {
table.increments()
})
}
}
const migrations = {'2015-01-20': Users}
yield runner.up(migrations)
const usersInfo = yield runner.database.table('users').columnInfo()
const accountsInfo = yield runner.database.table('accounts').columnInfo()
expect(usersInfo.id).to.be.an('object')
expect(accountsInfo.id).to.be.an('object')
yield runner.database.schema.dropTable('users')
yield runner.database.schema.dropTable('accounts')
yield runner.database.schema.dropTable('adonis_migrations')
})

it('should be able to run multiple commands inside a single down method', function * () {
const runner = new Migrations(Database, Config)
class Users extends Schema {
up () {
this.create('users', (table) => {
table.increments()
})

this.create('accounts', (table) => {
table.increments()
})
}

down () {
this.drop('users')
this.drop('accounts')
}
}
const migrations = {'2015-01-20': Users}
yield runner.up(migrations)
const usersInfo = yield runner.database.table('users').columnInfo()
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)
yield runner1.down(migrations)
const usersTable = yield runner1.database.table('users').columnInfo()
const accountsTable = yield runner1.database.table('accounts').columnInfo()
expect(usersTable).deep.equal({})
expect(accountsTable).deep.equal({})
yield runner.database.schema.dropTable('adonis_migrations')
})
})

0 comments on commit 9105c35

Please sign in to comment.