Skip to content

Commit

Permalink
fix(migrator): use the correct batch number when no batch is defined …
Browse files Browse the repository at this point in the history
…during rollback
  • Loading branch information
thetutlage committed Jan 12, 2020
1 parent 85755bb commit 7e8532a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Migrator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,10 @@ export class Migrator extends EventEmitter implements MigratorContract {
* Migrate down (aka rollback)
*/
private async _runDown (batch?: number) {
batch = batch === undefined ? await this._getLatestBatch() : batch
if (batch === undefined) {
batch = await this._getLatestBatch() - 1
}

const existing = await this._getMigratedFilesTillBatch(batch)
const collected = await this._migrationSource.getMigrations()

Expand Down
63 changes: 63 additions & 0 deletions test/migrations/migrator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,69 @@ test.group('Migrator', (group) => {
}])
})

test('rollback database to the latest batch', async (assert) => {
const app = new Application(fs.basePath, {} as any, {} as any, {})

await fs.add('database/migrations/users.ts', `
import { Schema } from '../../../../../src/Schema'
module.exports = class User extends Schema {
public async up () {
this.schema.createTable('schema_users', (table) => {
table.increments()
})
}
public async down () {
this.schema.dropTable('schema_users')
}
}
`)

const migrator = getMigrator(db, app, { direction: 'up', connectionName: 'primary' })
await migrator.run()

await fs.add('database/migrations/accounts.ts', `
import { Schema } from '../../../../../src/Schema'
module.exports = class User extends Schema {
public async up () {
this.schema.createTable('schema_accounts', (table) => {
table.increments()
})
}
public async down () {
this.schema.dropTable('schema_accounts')
}
}
`)

const migrator1 = getMigrator(db, app, { direction: 'up', connectionName: 'primary' })
await migrator1.run()

const migrator2 = getMigrator(db, app, { direction: 'down', connectionName: 'primary' })
await migrator2.run()

const migrated = await db.connection().from('adonis_schema').select('*')
const hasUsersTable = await db.connection().schema.hasTable('schema_users')
const hasAccountsTable = await db.connection().schema.hasTable('schema_accounts')
const migratedFiles = Object.keys(migrator2.migratedFiles).map((file) => {
return {
status: migrator2.migratedFiles[file].status,
file: file,
queries: migrator2.migratedFiles[file].queries,
}
})

assert.lengthOf(migrated, 1)
assert.isTrue(hasUsersTable)
assert.isFalse(hasAccountsTable)
assert.deepEqual(migratedFiles, [{
status: 'completed',
file: 'database/migrations/accounts',
queries: [],
}])
})

test('rollback all down to batch 0', async (assert) => {
const app = new Application(fs.basePath, {} as any, {} as any, {})

Expand Down

0 comments on commit 7e8532a

Please sign in to comment.