feat: add FOR NO KEY UPDATE lock mode for postgresql (#5971) · typeorm/typeorm@360122f
@@ -77,6 +77,28 @@ describe("query builder > locking", () => {
7777});
7878})));
797980+it("should throw error if for no key update lock used without transaction", () => Promise.all(connections.map(async connection => {
81+if (connection.driver instanceof PostgresDriver) {
82+return connection.createQueryBuilder(PostWithVersion, "post")
83+.setLock("for_no_key_update")
84+.where("post.id = :id", { id: 1 })
85+.getOne().should.be.rejectedWith(PessimisticLockTransactionRequiredError);
86+}
87+return;
88+})));
89+90+it("should not throw error if for no key update lock used with transaction", () => Promise.all(connections.map(async connection => {
91+if (connection.driver instanceof PostgresDriver) {
92+return connection.manager.transaction(entityManager => {
93+return Promise.all([entityManager.createQueryBuilder(PostWithVersion, "post")
94+.setLock("for_no_key_update")
95+.where("post.id = :id", { id: 1})
96+.getOne().should.not.be.rejected]);
97+});
98+}
99+return;
100+})));
101+80102it("should attach pessimistic read lock statement on query if locking enabled", () => Promise.all(connections.map(async connection => {
81103if (connection.driver instanceof AbstractSqliteDriver || connection.driver instanceof CockroachDriver || connection.driver instanceof SapDriver)
82104return;
@@ -141,6 +163,30 @@ describe("query builder > locking", () => {
141163142164})));
143165166+it("should not attach for no key update lock statement on query if locking is not used", () => Promise.all(connections.map(async connection => {
167+if (connection.driver instanceof PostgresDriver) {
168+const sql = connection.createQueryBuilder(PostWithVersion, "post")
169+.where("post.id = :id", { id: 1 })
170+.getSql();
171+172+expect(sql.indexOf("FOR NO KEY UPDATE") === -1).to.be.true;
173+}
174+return;
175+})));
176+177+it("should attach for no key update lock statement on query if locking enabled", () => Promise.all(connections.map(async connection => {
178+if (connection.driver instanceof PostgresDriver) {
179+const sql = connection.createQueryBuilder(PostWithVersion, "post")
180+.setLock("for_no_key_update")
181+.where("post.id = :id", { id: 1 })
182+.getSql();
183+184+expect(sql.indexOf("FOR NO KEY UPDATE") !== -1).to.be.true;
185+}
186+return;
187+188+})));
189+144190it("should throw error if optimistic lock used with getMany method", () => Promise.all(connections.map(async connection => {
145191146192return connection.createQueryBuilder(PostWithVersion, "post")
@@ -291,4 +337,19 @@ describe("query builder > locking", () => {
291337return;
292338})));
293339340+it("should throw error if for no key update locking not supported by given driver", () => Promise.all(connections.map(async connection => {
341+if (!(connection.driver instanceof PostgresDriver)) {
342+return connection.manager.transaction(entityManager => {
343+return Promise.all([
344+entityManager.createQueryBuilder(PostWithVersion, "post")
345+.setLock("for_no_key_update")
346+.where("post.id = :id", { id: 1 })
347+.getOne().should.be.rejectedWith(LockNotSupportedOnGivenDriverError),
348+]);
349+});
350+}
351+352+return;
353+})));
354+294355});