@@ -77,6 +77,28 @@ describe("query builder > locking", () => {
77
77
} ) ;
78
78
} ) ) ) ;
79
79
80
+ 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
+
80
102
it ( "should attach pessimistic read lock statement on query if locking enabled" , ( ) => Promise . all ( connections . map ( async connection => {
81
103
if ( connection . driver instanceof AbstractSqliteDriver || connection . driver instanceof CockroachDriver || connection . driver instanceof SapDriver )
82
104
return ;
@@ -141,6 +163,30 @@ describe("query builder > locking", () => {
141
163
142
164
} ) ) ) ;
143
165
166
+ 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
+
144
190
it ( "should throw error if optimistic lock used with getMany method" , ( ) => Promise . all ( connections . map ( async connection => {
145
191
146
192
return connection . createQueryBuilder ( PostWithVersion , "post" )
@@ -291,4 +337,19 @@ describe("query builder > locking", () => {
291
337
return ;
292
338
} ) ) ) ;
293
339
340
+ 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
+
294
355
} ) ;
0 commit comments