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

Commit 838d596c by Mick Hansen

Merge pull request #4911 from EToreo/master

Added Transaction Type
2 parents 17739641 f9554221
......@@ -38,6 +38,9 @@ AbstractDialect.prototype.supports = {
updateOnDuplicate: false,
schemas: false,
transactions: true,
transactionOptions: {
type: false
},
migrations: true,
upserts: true,
constraints: {
......
......@@ -25,6 +25,9 @@ SqliteDialect.prototype.supports = _.merge(_.cloneDeep(Abstract.prototype.suppor
index: {
using: false
},
transactionOptions: {
type: true
},
joinTableDependent: false,
groupedLimit: false,
ignoreDuplicates: ' OR IGNORE'
......
......@@ -365,7 +365,7 @@ var QueryGenerator = {
return 'SAVEPOINT ' + this.quoteIdentifier(transaction.name) + ';';
}
return 'BEGIN TRANSACTION;';
return 'BEGIN ' + transaction.options.type + ' TRANSACTION;';
},
setAutocommitQuery: function() {
......
......@@ -79,6 +79,7 @@ var url = require('url')
* @param {Integer} [options.pool.maxIdleTime] The maximum time, in milliseconds, that a connection can be idle before being released
* @param {Function} [options.pool.validateConnection] A function that validates a connection. Called with client. The default function checks that client is an object, and that its state is not disconnected
* @param {Boolean} [options.quoteIdentifiers=true] Set to `false` to make table names and attributes case-insensitive on Postgres and skip double quoting of them.
* @param {String} [options.transactionType='DEFERRED'] Set the default transaction type. See `Sequelize.Transaction.TYPES` for possible options. Sqlite only.
* @param {String} [options.isolationLevel='REPEATABLE_READ'] Set the default transaction isolation level. See `Sequelize.Transaction.ISOLATION_LEVELS` for possible options.
* @param {Boolean} [options.typeValidation=false] Run built in type validators on insert and update, e.g. validate that arguments passed to integer fields are integer-like
*/
......@@ -143,6 +144,7 @@ var Sequelize = function(database, username, password, options) {
pool: {},
quoteIdentifiers: true,
hooks: {},
transactionType: Transaction.TYPES.DEFERRED,
isolationLevel: Transaction.ISOLATION_LEVELS.REPEATABLE_READ,
databaseVersion: 0,
typeValidation: false
......@@ -285,7 +287,7 @@ Object.defineProperty(Sequelize.prototype, 'connectorManager', {
});
/**
* A reference to the sequelize transaction class. Use this to access isolationLevels when creating a transaction
* A reference to the sequelize transaction class. Use this to access isolationLevels and types when creating a transaction
* @property Transaction
* @see {Transaction}
* @see {Sequelize#transaction}
......@@ -1204,6 +1206,7 @@ Sequelize.where = Sequelize.condition = Sequelize.prototype.condition = Sequeliz
* @param {Object} [options={}]
* @param {Boolean} [options.autocommit=true]
* @param {String} [options.type='DEFERRED'] See `Sequelize.Transaction.TYPES` for possible options. Sqlite only.
* @param {String} [options.isolationLevel='REPEATABLE_READ'] See `Sequelize.Transaction.ISOLATION_LEVELS` for possible options
* @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql.
* @return {Promise}
......
......@@ -13,6 +13,7 @@ var Utils = require('./utils')
* @param {Sequelize} sequelize A configured sequelize Instance
* @param {Object} options An object with options
* @param {Boolean} options.autocommit=true Sets the autocommit property of the transaction.
* @param {String} options.type=true Sets the type of the transaction.
* @param {String} options.isolationLevel=true Sets the isolation level of the transaction.
* @param {String} options.deferrable Sets the constraints to be deferred or immediately checked.
*/
......@@ -21,6 +22,7 @@ var Transaction = module.exports = function(sequelize, options) {
this.savepoints = [];
this.options = Utils._.extend({
autocommit: true,
type: sequelize.options.transactionType,
isolationLevel: sequelize.options.isolationLevel
}, options || {});
......@@ -39,6 +41,46 @@ var Transaction = module.exports = function(sequelize, options) {
};
/**
* Types can be set per-transaction by passing `options.type` to `sequelize.transaction`.
* Default to `DEFERRED` but you can override the default type by passing `options.transactionType` in `new Sequelize`.
* Sqlite only.
*
* The possible types to use when starting a transaction:
*
* ```js
* {
* DEFERRED: "DEFERRED",
* IMMEDIATE: "IMMEDIATE",
* EXCLUSIVE: "EXCLUSIVE"
* }
* ```
*
* Pass in the desired level as the first argument:
*
* ```js
* return sequelize.transaction({
* type: Sequelize.Transaction.EXCLUSIVE
* }, function (t) {
*
* // your transactions
*
* }).then(function(result) {
* // transaction has been committed. Do something after the commit if required.
* }).catch(function(err) {
* // do something with the err.
* });
* ```
*
* @property TYPES
*/
Transaction.TYPES = {
DEFERRED: 'DEFERRED',
IMMEDIATE: 'IMMEDIATE',
EXCLUSIVE: 'EXCLUSIVE'
};
/**
* Isolations levels can be set per-transaction by passing `options.isolationLevel` to `sequelize.transaction`.
* Default to `REPEATABLE_READ` but you can override the default isolation level by passing `options.isolationLevel` in `new Sequelize`.
*
......
......@@ -239,6 +239,32 @@ describe(Support.getTestDialectTeaser('Transaction'), function() {
});
});
}
if (current.dialect.supports.transactionOptions.type) {
describe('transaction types', function() {
it('should support default transaction type DEFERRED', function() {
return this.sequelize.transaction({
}).bind(this).then(function (t) {
return t.rollback().bind(this).then(function() {
expect(t.options.type).to.equal('DEFERRED');
});
});
});
Object.keys(Transaction.TYPES).forEach(function(key) {
it('should allow specification of ' + key + ' type', function() {
return this.sequelize.transaction({
type: key
}).bind(this).then(function (t) {
return t.rollback().bind(this).then(function() {
expect(t.options.type).to.equal(Transaction.TYPES[key]);
});
});
});
});
});
}
if (current.dialect.supports.lock) {
describe('row locking', function () {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!