Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit aa0dee2f
authored
Jul 06, 2014
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2000 from janmeier/multipleSavepoints
Support for multiple savepoints
2 parents
6b257be0
b63d4357
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
83 additions
and
11 deletions
lib/dialects/abstract/query-generator.js
lib/dialects/sqlite/query-generator.js
lib/query-interface.js
lib/sequelize.js
lib/transaction.js
test/sequelize.test.js
lib/dialects/abstract/query-generator.js
View file @
aa0dee2
...
...
@@ -1032,12 +1032,13 @@ module.exports = (function() {
/**
* Returns a query that starts a transaction.
*
* @param {Transaction} transaction
* @param {Object} options An object with options.
* @return {String} The generated sql query.
*/
startTransactionQuery
:
function
(
options
)
{
startTransactionQuery
:
function
(
transaction
,
options
)
{
if
(
options
.
parent
)
{
return
'SAVEPOINT '
+
this
.
quoteIdentifier
(
options
.
parent
.
id
)
+
';'
;
return
'SAVEPOINT '
+
this
.
quoteIdentifier
(
transaction
.
name
)
+
';'
;
}
return
'START TRANSACTION;'
;
...
...
@@ -1060,12 +1061,13 @@ module.exports = (function() {
/**
* Returns a query that rollbacks a transaction.
*
* @param {Transaction} transaction
* @param {Object} options An object with options.
* @return {String} The generated sql query.
*/
rollbackTransactionQuery
:
function
(
options
)
{
rollbackTransactionQuery
:
function
(
transaction
,
options
)
{
if
(
options
.
parent
)
{
return
'ROLLBACK TO SAVEPOINT '
+
this
.
quoteIdentifier
(
options
.
parent
.
id
)
+
';'
;
return
'ROLLBACK TO SAVEPOINT '
+
this
.
quoteIdentifier
(
transaction
.
name
)
+
';'
;
}
return
'ROLLBACK;'
;
...
...
lib/dialects/sqlite/query-generator.js
View file @
aa0dee2
...
...
@@ -374,9 +374,9 @@ module.exports = (function() {
});
},
startTransactionQuery
:
function
(
options
)
{
startTransactionQuery
:
function
(
transaction
,
options
)
{
if
(
options
.
parent
)
{
return
'SAVEPOINT '
+
this
.
quoteIdentifier
(
options
.
parent
.
id
)
+
';'
;
return
'SAVEPOINT '
+
this
.
quoteIdentifier
(
transaction
.
name
)
+
';'
;
}
return
"BEGIN TRANSACTION;"
;
...
...
lib/query-interface.js
View file @
aa0dee2
...
...
@@ -729,7 +729,7 @@ module.exports = (function() {
parent
:
options
.
transaction
},
options
||
{});
var
sql
=
this
.
QueryGenerator
.
startTransactionQuery
(
options
);
var
sql
=
this
.
QueryGenerator
.
startTransactionQuery
(
transaction
,
options
);
return
this
.
sequelize
.
query
(
sql
,
null
,
options
);
};
...
...
@@ -762,7 +762,7 @@ module.exports = (function() {
parent
:
options
.
transaction
},
options
||
{});
var
sql
=
this
.
QueryGenerator
.
rollbackTransactionQuery
(
options
);
var
sql
=
this
.
QueryGenerator
.
rollbackTransactionQuery
(
transaction
,
options
);
return
this
.
sequelize
.
query
(
sql
,
null
,
options
);
};
...
...
lib/sequelize.js
View file @
aa0dee2
...
...
@@ -771,9 +771,7 @@ module.exports = (function() {
var
transaction
=
new
Transaction
(
this
,
options
);
return
transaction
.
prepareEnvironment
().
then
(
function
()
{
return
transaction
;
});
return
transaction
.
prepareEnvironment
().
return
(
transaction
);
};
Sequelize
.
prototype
.
log
=
function
()
{
...
...
lib/transaction.js
View file @
aa0dee2
...
...
@@ -11,11 +11,20 @@ var Utils = require('./utils')
*/
var
Transaction
=
module
.
exports
=
function
(
sequelize
,
options
)
{
this
.
sequelize
=
sequelize
;
this
.
savepoints
=
[];
this
.
options
=
Utils
.
_
.
extend
({
autocommit
:
true
,
isolationLevel
:
Transaction
.
ISOLATION_LEVELS
.
REPEATABLE_READ
},
options
||
{});
this
.
id
=
this
.
options
.
transaction
?
this
.
options
.
transaction
.
id
:
Utils
.
generateUUID
();
if
(
this
.
options
.
transaction
)
{
this
.
id
=
this
.
options
.
transaction
.
id
;
this
.
options
.
transaction
.
savepoints
.
push
(
this
);
this
.
name
=
this
.
id
+
'-savepoint-'
+
this
.
options
.
transaction
.
savepoints
.
length
;
}
else
{
this
.
id
=
this
.
name
=
Utils
.
generateUUID
();
}
};
/**
...
...
test/sequelize.test.js
View file @
aa0dee2
...
...
@@ -929,6 +929,69 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () {
})
})
describe
(
'supports rolling back to savepoints'
,
function
()
{
beforeEach
(
function
()
{
this
.
User
=
this
.
sequelizeWithTransaction
.
define
(
'user'
,
{});
return
this
.
sequelizeWithTransaction
.
sync
({
force
:
true
});
})
it
(
'rolls back to the first savepoint, undoing everything'
,
function
()
{
return
this
.
sequelizeWithTransaction
.
transaction
().
bind
(
this
).
then
(
function
(
transaction
)
{
this
.
transaction
=
transaction
;
return
this
.
sequelizeWithTransaction
.
transaction
({
transaction
:
transaction
});
}).
then
(
function
(
sp1
)
{
this
.
sp1
=
sp1
;
return
this
.
User
.
create
({},
{
transaction
:
this
.
transaction
});
}).
then
(
function
()
{
return
this
.
sequelizeWithTransaction
.
transaction
({
transaction
:
this
.
transaction
});
}).
then
(
function
(
sp2
)
{
this
.
sp2
=
sp2
;
return
this
.
User
.
create
({},
{
transaction
:
this
.
transaction
});
}).
then
(
function
()
{
return
this
.
User
.
findAll
({},
{
transaction
:
this
.
transaction
});
}).
then
(
function
(
users
)
{
expect
(
users
).
to
.
have
.
length
(
2
);
return
this
.
sp1
.
rollback
();
}).
then
(
function
()
{
return
this
.
User
.
findAll
({},
{
transaction
:
this
.
transaction
});
}).
then
(
function
(
users
)
{
expect
(
users
).
to
.
have
.
length
(
0
);
return
this
.
transaction
.
rollback
();
});
});
it
(
'rolls back to the most recent savepoint, only undoing recent changes'
,
function
()
{
return
this
.
sequelizeWithTransaction
.
transaction
().
bind
(
this
).
then
(
function
(
transaction
)
{
this
.
transaction
=
transaction
;
return
this
.
sequelizeWithTransaction
.
transaction
({
transaction
:
transaction
});
}).
then
(
function
(
sp1
)
{
this
.
sp1
=
sp1
;
return
this
.
User
.
create
({},
{
transaction
:
this
.
transaction
});
}).
then
(
function
()
{
return
this
.
sequelizeWithTransaction
.
transaction
({
transaction
:
this
.
transaction
});
}).
then
(
function
(
sp2
)
{
this
.
sp2
=
sp2
;
return
this
.
User
.
create
({},
{
transaction
:
this
.
transaction
});
}).
then
(
function
()
{
return
this
.
User
.
findAll
({},
{
transaction
:
this
.
transaction
});
}).
then
(
function
(
users
)
{
expect
(
users
).
to
.
have
.
length
(
2
);
return
this
.
sp2
.
rollback
();
}).
then
(
function
()
{
return
this
.
User
.
findAll
({},
{
transaction
:
this
.
transaction
});
}).
then
(
function
(
users
)
{
expect
(
users
).
to
.
have
.
length
(
1
);
return
this
.
transaction
.
rollback
();
});
});
});
it
(
'supports rolling back a nested transaction'
,
function
(
done
)
{
var
self
=
this
var
User
=
this
.
sequelize
.
define
(
'Users'
,
{
username
:
DataTypes
.
STRING
})
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment