Skip to content

Commit

Permalink
feat(migrations): add status method
Browse files Browse the repository at this point in the history
status method will return the current state of migrations
  • Loading branch information
thetutlage committed Mar 16, 2016
1 parent 7bdde0e commit ec68f1c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,25 @@ class Migrations {
this.database.close()
return {migrated: migrate, status}
}

/**
* returns current status of migrations
*
* @param {Array} files
* @return {Object}
*
* @public
*/
* status (files) {
const migrate = yield this._diff(files, 'up')
return _.transform(files, function (result, file, name) {
if (migrate.indexOf(name) > -1) {
result[name] = 'N'
} else {
result[name] = 'Y'
}
})
}
}

class ExtendedMigrations extends mixin(Migrations, Lock, Migrate, Batch) {}
Expand Down
34 changes: 34 additions & 0 deletions test/unit/migrations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,40 @@ describe('Migrations', function () {
yield runner.database.schema.dropTable('adonis_migrations')
})

it('should return migration status', function * () {
const runner = new Migrations(Database, Config)
class Users extends Schema {
up () {
this.create('users', function (table) {
table.increments()
table.string('username')
})
}
down () {
this.drop('users')
}
}

class Accounts extends Schema {
up () {
this.create('accounts', function (table) {
table.increments()
table.string('account_name')
})
}
}

const batch1 = {'2015-01-20': Users}
const batch2 = {'2016-03-13': Accounts}
const all = {}
_.merge(all, batch1, batch2)
yield runner.up(batch1)
const status = yield runner.status(all)
expect(status).deep.equal({'2015-01-20': 'Y', '2016-03-13': 'N'})
yield runner.database.schema.dropTable('users')
yield runner.database.schema.dropTable('adonis_migrations')
})

it('should migrate the database by calling the up method', function * () {
const runner = new Migrations(Database, Config)
class Users extends Schema {
Expand Down

0 comments on commit ec68f1c

Please sign in to comment.