Skip to content

Commit

Permalink
Fix transaction support for migrations (#3084)
Browse files Browse the repository at this point in the history
* Fix transaction support for migrations

* Clarify warning

* Clarify warning message

* Simplify logic, make enabling/disabling more consistent

* Fix tests

* Fix test

* Reduce duplication
  • Loading branch information
kibertoad committed Mar 13, 2019
1 parent de1c934 commit 24fcf27
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
22 changes: 18 additions & 4 deletions src/migrate/Migrator.js
Expand Up @@ -49,12 +49,16 @@ export default class Migrator {
constructor(knex) {
// Clone knex instance and remove post-processing that is unnecessary for internal queries from a cloned config
if (isFunction(knex)) {
this.knex = knex.withUserParams({
...knex.userParams,
});
this.knex.disableProcessing();
if (!knex.isTransaction) {
this.knex = knex.withUserParams({
...knex.userParams,
});
} else {
this.knex = knex;
}
} else {
this.knex = Object.assign({}, knex);
this.knex.userParams = this.knex.userParams || {};
}

this.config = getMergedConfig(this.knex.client.config.migrations);
Expand All @@ -66,6 +70,7 @@ export default class Migrator {

// Migrators to the latest configuration.
latest(config) {
this._disableProcessing();
this.config = getMergedConfig(config, this.config);

return migrationListResolver
Expand Down Expand Up @@ -101,6 +106,7 @@ export default class Migrator {

// Rollback the last "batch", or all, of migrations that were run.
rollback(config, all = false) {
this._disableProcessing();
return Promise.try(() => {
this.config = getMergedConfig(config, this.config);

Expand All @@ -117,6 +123,7 @@ export default class Migrator {
}

status(config) {
this._disableProcessing();
this.config = getMergedConfig(config, this.config);

return Promise.all([
Expand All @@ -130,6 +137,7 @@ export default class Migrator {
// Retrieves and returns the current migration version we're on, as a promise.
// If no migrations have been run yet, return "none".
currentVersion(config) {
this._disableProcessing();
this.config = getMergedConfig(config, this.config);

return migrationListResolver
Expand All @@ -155,6 +163,12 @@ export default class Migrator {
return this.generator.make(name, this.config);
}

_disableProcessing() {
if (this.knex.disableProcessing) {
this.knex.disableProcessing();
}
}

_isLocked(trx) {
const tableName = getLockTableName(this.config.tableName);
return getTable(this.knex, tableName, this.config.schemaName)
Expand Down
3 changes: 2 additions & 1 deletion src/transaction.js
Expand Up @@ -193,7 +193,8 @@ function makeTransactor(trx, connection, trxClient) {
);
};

transactor.userParams = trx.userParams;
transactor.isTransaction = true;
transactor.userParams = trx.userParams || {};

transactor.transaction = function(container, options) {
return trxClient.transaction(container, options, trx);
Expand Down
3 changes: 0 additions & 3 deletions test/unit/knex.js
Expand Up @@ -99,10 +99,7 @@ describe('knex', () => {
const knexWithParams = knex.withUserParams({ userParam: '451' });

expect(knexWithParams.migrate.knex.userParams).to.deep.equal({
isProcessingDisabled: true,
postProcessResponse: undefined,
userParam: '451',
wrapIdentifier: undefined,
});
});

Expand Down
48 changes: 46 additions & 2 deletions test/unit/migrate/Migrator.js
Expand Up @@ -26,7 +26,7 @@ describe('Migrator', () => {

it('latest', (done) => {
expect(() => {
knex.migrate
return knex.migrate
.latest({
directory: 'test/unit/migrate/migrations',
})
Expand All @@ -37,6 +37,49 @@ describe('Migrator', () => {
});
});

describe('supports running migrations in transaction', (done) => {
let migrationSource;
let knex;
let wasProcessed = false;
let wasWrapped = false;
beforeEach(() => {
migrationSource = new FsMigrations('test/unit/migrate/migrations/');
knex = Knex({
...sqliteConfig,
connection: ':memory:',
migrationSource,
postProcessResponse: (response) => {
wasProcessed = true;
return response;
},
wrapIdentifier: (value, wrap) => {
wasWrapped = true;
return wrap(value);
},
});
});

afterEach(() => {
knex.destroy();
});

it('latest', (done) => {
expect(() => {
return knex.transaction((txn) => {
txn.migrate
.latest({
directory: 'test/unit/migrate/migrations',
})
.then(() => {
expect(wasProcessed).to.equal(false);
expect(wasWrapped).to.equal(false);
done();
});
});
}).not.to.throw();
});
});

describe('uses postProcessResponse for migrations', (done) => {
let migrationSource;
let knex;
Expand All @@ -56,8 +99,9 @@ describe('Migrator', () => {
...sqliteConfig,
connection: ':memory:',
migrationSource,
postProcessResponse: () => {
postProcessResponse: (response) => {
wasPostProcessed = true;
return response;
},
});

Expand Down

0 comments on commit 24fcf27

Please sign in to comment.