Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ForUpdate array parameter #4882

Merged
merged 2 commits into from Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/query/querybuilder.js
Expand Up @@ -1233,7 +1233,11 @@ class Builder extends EventEmitter {
// Set a lock for update constraint.
forUpdate(...tables) {
this._single.lock = lockMode.forUpdate;
this._single.lockTables = tables;
if (tables.length === 1 && Array.isArray(tables[0])) {
this._single.lockTables = tables[0];
} else {
this._single.lockTables = tables;
}
return this;
}

Expand Down
28 changes: 28 additions & 0 deletions test/unit/query/builder.js
Expand Up @@ -6717,6 +6717,34 @@ describe('QueryBuilder', () => {
);
});

it('lock only some tables for update (with array #4878)', () => {
testsql(
qb()
.select('*')
.from('foo')
.where('bar', '=', 'baz')
.forUpdate(['lo', 'rem']),
{
mysql: {
sql: 'select * from `foo` where `bar` = ? for update',
bindings: ['baz'],
},
pg: {
sql: 'select * from "foo" where "bar" = ? for update of "lo", "rem"',
bindings: ['baz'],
},
mssql: {
sql: 'select * from [foo] with (UPDLOCK) where [bar] = ?',
bindings: ['baz'],
},
oracledb: {
sql: 'select * from "foo" where "bar" = ? for update',
bindings: ['baz'],
},
}
);
});

it('lock for update with skip locked #1937', () => {
testsql(qb().select('*').from('foo').first().forUpdate().skipLocked(), {
mysql: {
Expand Down