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

fix: attach FOR NO KEY UPDATE lock to query if required #8008

Merged
merged 1 commit into from
Nov 8, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/find-options/FindOptionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,14 @@ export class FindOptionsUtils {
if (options.lock) {
if (options.lock.mode === "optimistic") {
qb.setLock(options.lock.mode, options.lock.version);
} else if (options.lock.mode === "pessimistic_read" || options.lock.mode === "pessimistic_write" || options.lock.mode === "dirty_read" || options.lock.mode === "pessimistic_partial_write" || options.lock.mode === "pessimistic_write_or_fail") {
} else if (
options.lock.mode === "pessimistic_read" ||

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it better to store all possible locks in const array and call array.includes() instead of chaining equality checks.

Copy link
Contributor Author

@das-mensch das-mensch Aug 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely. The locking code base seems to have grown a lot in the past and needs some refactoring. I would like to create a separate PR for cleaning up. This is to be understood as a "hot fix" as the refactoring will need a bit of time.

options.lock.mode === "pessimistic_write" ||
options.lock.mode === "dirty_read" ||
options.lock.mode === "pessimistic_partial_write" ||
options.lock.mode === "pessimistic_write_or_fail" ||
options.lock.mode === "for_no_key_update"
) {
const tableNames = options.lock.tables ? options.lock.tables.map((table) => {
const tableAlias = qb.expressionMap.aliases.find((alias) => {
return alias.metadata.tableNameWithoutPrefix === table;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,27 @@ describe("repository > find options > locking", () => {

})));

it("should attach for no key update lock statement on query if locking enabled", () => Promise.all(connections.map(async connection => {
if (!(connection.driver instanceof PostgresDriver))
return;

const executedSql: string[] = [];

await connection.manager.transaction(entityManager => {
const originalQuery = entityManager.queryRunner!.query.bind(entityManager.queryRunner);
entityManager.queryRunner!.query = (...args: any[]) => {
executedSql.push(args[0]);
return originalQuery(...args);
};

return entityManager
.getRepository(PostWithVersion)
.findOne(1, { lock: { mode: "for_no_key_update" } });
});

expect(executedSql.join(" ").includes("FOR NO KEY UPDATE")).to.be.true;
})));

it("should attach pessimistic write lock statement on query if locking enabled", () => Promise.all(connections.map(async connection => {
if (connection.driver instanceof AbstractSqliteDriver || connection.driver instanceof CockroachDriver || connection.driver instanceof SapDriver)
return;
Expand Down