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 7b163084
authored
May 02, 2018
by
joshuacullen
Committed by
Sushant
May 02, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(postgres): skip locked support (#9197)
1 parent
2752bf7a
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
82 additions
and
0 deletions
docs/transactions.md
lib/dialects/abstract/query-generator.js
lib/dialects/postgres/index.js
lib/transaction.js
test/integration/transaction.test.js
docs/transactions.md
View file @
7b16308
...
...
@@ -261,5 +261,26 @@ model.afterSave((instance, options) => {
}
// Save done outside a transaction, safe for callers to fetch the updated model
// Notify
##
Locks
Queries
within
a
`transaction`
can
be
performed
with
locks
```
return User.findAll({
limit: 1,
lock: true,
transaction: t1
})
```
Queries within a transaction can skip locked rows
```
return User.findAll({
limit: 1,
lock: true,
skipLocked: true,
transaction: t2
})
```
lib/dialects/abstract/query-generator.js
View file @
7b16308
...
...
@@ -1327,6 +1327,9 @@ class QueryGenerator {
if
(
this
.
_dialect
.
supports
.
lockOf
&&
options
.
lock
.
of
&&
options
.
lock
.
of
.
prototype
instanceof
Model
)
{
query
+=
' OF '
+
this
.
quoteTable
(
options
.
lock
.
of
.
name
);
}
if
(
this
.
_dialect
.
supports
.
skipLocked
&&
options
.
skipLocked
)
{
query
+=
' SKIP LOCKED'
;
}
}
return
`
${
query
}
;`
;
...
...
lib/dialects/postgres/index.js
View file @
7b16308
...
...
@@ -33,6 +33,7 @@ PostgresDialect.prototype.supports = _.merge(_.cloneDeep(AbstractDialect.prototy
lockOf
:
true
,
lockKey
:
true
,
lockOuterJoinFailure
:
true
,
skipLocked
:
true
,
forShare
:
'FOR SHARE'
,
index
:
{
concurrently
:
true
,
...
...
lib/transaction.js
View file @
7b16308
...
...
@@ -290,6 +290,19 @@ class Transaction {
* ```
* UserModel will be locked but TaskModel won't!
*
* You can also skip locked rows:
*
*```js
* t1 // is a transaction
* Model.findAll({
* where: ...,
* transaction: t1,
* lock: true,
* skipLocked: true
* });
* ```
* The query will now return any rows that aren't locked by another transaction
*
* @return {Object}
* @property UPDATE
* @property SHARE
...
...
test/integration/transaction.test.js
View file @
7b16308
...
...
@@ -528,6 +528,50 @@ if (current.dialect.supports.transactions) {
});
});
if
(
current
.
dialect
.
supports
.
skipLocked
)
{
it
(
'supports for update with skip locked'
,
function
()
{
const
User
=
this
.
sequelize
.
define
(
'user'
,
{
username
:
Support
.
Sequelize
.
STRING
,
awesome
:
Support
.
Sequelize
.
BOOLEAN
});
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(()
=>
{
return
Promise
.
all
([
User
.
create
(
{
username
:
'jan'
}
),
User
.
create
(
{
username
:
'joe'
}
)
]);
}).
then
(()
=>
{
return
this
.
sequelize
.
transaction
().
then
(
t1
=>
{
return
User
.
findAll
({
limit
:
1
,
lock
:
true
,
transaction
:
t1
}).
then
(
results
=>
{
const
firstUserId
=
results
[
0
].
id
;
return
this
.
sequelize
.
transaction
().
then
(
t2
=>
{
return
User
.
findAll
({
limit
:
1
,
lock
:
true
,
skipLocked
:
true
,
transaction
:
t2
}).
then
(
secondResults
=>
{
expect
(
secondResults
[
0
].
id
).
to
.
not
.
equal
(
firstUserId
);
return
Promise
.
all
([
t1
.
commit
(),
t2
.
commit
()
]);
});
});
});
});
});
});
}
it
(
'fail locking with outer joins'
,
function
()
{
const
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
Support
.
Sequelize
.
STRING
}),
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
Support
.
Sequelize
.
STRING
,
active
:
Support
.
Sequelize
.
BOOLEAN
}),
...
...
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