不要怂,就是干,撸起袖子干!

Commit fbe47d1a by Harshith Kashyap Committed by Jan Aagaard Meier

[MSSQL] Format isolation level as tedious isolation level (#7297)

* [MSSQL] Format isolation level as tedious isolation level

* [ci skip] Added changelog entry

* Added unit test that stubs beginTransaction method

* Fixes jshint error

* Explicitly pass tedious lib instance
1 parent 7ab3ba45
# Future # Future
- [FIXED] Map isolation level strings to tedious isolation level [MSSQL] [#7296] (https://github.com/sequelize/sequelize/issues/7296)
- [FEATURE] `addConstraint`, `removeConstraint`, `showConstraint` [#7108](https://github.com/sequelize/sequelize/pull/7108) - [FEATURE] `addConstraint`, `removeConstraint`, `showConstraint` [#7108](https://github.com/sequelize/sequelize/pull/7108)
- [FIXED] `changeColumn` generates incorrect query with ENUM type [#7455](https://github.com/sequelize/sequelize/pull/7455) - [FIXED] `changeColumn` generates incorrect query with ENUM type [#7455](https://github.com/sequelize/sequelize/pull/7455)
- [ADDED] `options.alter` to sequelize.sync() to alter existing tables.[#537](https://github.com/sequelize/sequelize/issues/537) - [ADDED] `options.alter` to sequelize.sync() to alter existing tables.[#537](https://github.com/sequelize/sequelize/issues/537)
......
...@@ -51,7 +51,7 @@ class Query extends AbstractQuery { ...@@ -51,7 +51,7 @@ class Query extends AbstractQuery {
} else { } else {
resolve(this.formatResults()); resolve(this.formatResults());
} }
}, this.options.transaction.name, this.options.isolationLevel); }, this.options.transaction.name, Utils.mapIsolationLevelStringToTedious(this.options.isolationLevel, connection.lib));
} else if (_.startsWith(this.sql, 'COMMIT TRANSACTION')) { } else if (_.startsWith(this.sql, 'COMMIT TRANSACTION')) {
connection.commitTransaction(err => { connection.commitTransaction(err => {
if (err) { if (err) {
......
...@@ -496,3 +496,23 @@ class Where extends SequelizeMethod { ...@@ -496,3 +496,23 @@ class Where extends SequelizeMethod {
exports.Where = Where; exports.Where = Where;
exports.validateParameter = parameterValidator; exports.validateParameter = parameterValidator;
exports.mapIsolationLevelStringToTedious = (isolationLevel, tedious) => {
if (!tedious) {
throw new Error('An instance of tedious lib should be passed to this function');
}
const tediousIsolationLevel = tedious.ISOLATION_LEVEL;
switch (isolationLevel) {
case 'READ_UNCOMMITTED':
return tediousIsolationLevel.READ_UNCOMMITTED;
case 'READ_COMMITTED':
return tediousIsolationLevel.READ_COMMITTED;
case 'REPEATABLE_READ':
return tediousIsolationLevel.REPEATABLE_READ;
case 'SERIALIZABLE':
return tediousIsolationLevel.SERIALIZABLE;
case 'SNAPSHOT':
return tediousIsolationLevel.SNAPSHOT;
}
};
\ No newline at end of file
'use strict';
const path = require('path');
const Query = require(path.resolve('./lib/dialects/mssql/query.js'));
const Support = require(path.resolve('./test/support'));
const sequelize = Support.sequelize;
const sinon = require('sinon');
const expect = require('chai').expect;
const tedious = require('tedious');
const tediousIsolationLevel = tedious.ISOLATION_LEVEL;
const connectionStub = { beginTransaction: () => {}, lib: tedious };
let sandbox, query;
describe('[MSSQL]', () => {
describe('beginTransaction', () => {
beforeEach(() => {
sandbox = sinon.sandbox.create();
const options = {
transaction: { name: 'transactionName' },
isolationLevel: 'REPEATABLE_READ',
logging: false
};
sandbox.stub(connectionStub, 'beginTransaction', cb => {
cb();
});
query = new Query(connectionStub, sequelize, options);
});
it('should call beginTransaction with correct arguments', () => {
return query._run(connectionStub, 'BEGIN TRANSACTION')
.then(() => {
expect(connectionStub.beginTransaction.called).to.equal(true);
expect(connectionStub.beginTransaction.args[0][1]).to.equal('transactionName');
expect(connectionStub.beginTransaction.args[0][2]).to.equal(tediousIsolationLevel.REPEATABLE_READ);
});
});
afterEach(() => {
sandbox.restore();
});
});
});
...@@ -5,6 +5,8 @@ const expect = chai.expect; ...@@ -5,6 +5,8 @@ const expect = chai.expect;
const Support = require(__dirname + '/support'); const Support = require(__dirname + '/support');
const DataTypes = require(__dirname + '/../../lib/data-types'); const DataTypes = require(__dirname + '/../../lib/data-types');
const Utils = require(__dirname + '/../../lib/utils'); const Utils = require(__dirname + '/../../lib/utils');
const tedious = require('tedious');
const tediousIsolationLevel = tedious.ISOLATION_LEVEL;
suite(Support.getTestDialectTeaser('Utils'), () => { suite(Support.getTestDialectTeaser('Utils'), () => {
suite('merge', () => { suite('merge', () => {
...@@ -265,4 +267,33 @@ suite(Support.getTestDialectTeaser('Utils'), () => { ...@@ -265,4 +267,33 @@ suite(Support.getTestDialectTeaser('Utils'), () => {
expect(testLogger.namespace).to.be.eql('sequelize:test'); expect(testLogger.namespace).to.be.eql('sequelize:test');
}); });
}); });
if (Support.getTestDialect() === 'mssql') {
suite('mapIsolationLevelStringToTedious', () => {
test('READ_UNCOMMITTED', () => {
expect(Utils.mapIsolationLevelStringToTedious('READ_UNCOMMITTED', tedious)).to.equal(tediousIsolationLevel.READ_UNCOMMITTED);
});
test('READ_COMMITTED', () => {
expect(Utils.mapIsolationLevelStringToTedious('READ_COMMITTED', tedious)).to.equal(tediousIsolationLevel.READ_COMMITTED);
});
test('REPEATABLE_READ', () => {
expect(Utils.mapIsolationLevelStringToTedious('REPEATABLE_READ', tedious)).to.equal(tediousIsolationLevel.REPEATABLE_READ);
});
test('SERIALIZABLE', () => {
expect(Utils.mapIsolationLevelStringToTedious('SERIALIZABLE', tedious)).to.equal(tediousIsolationLevel.SERIALIZABLE);
});
test('SNAPSHOT', () => {
expect(Utils.mapIsolationLevelStringToTedious('SNAPSHOT', tedious)).to.equal(tediousIsolationLevel.SNAPSHOT);
});
test('should throw error if tedious lib is not passed as a parameter', () => {
expect(Utils.mapIsolationLevelStringToTedious.bind(Utils, 'SNAPSHOT')).to.throw('An instance of tedious lib should be passed to this function');
});
});
}
}); });
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!