@@ -31,7 +31,7 @@ const getById = (blobId) => ({ maybeOne }) =>
3131 maybeOne ( sql `select * from blobs where id=${ blobId } ` )
3232 . then ( map ( construct ( Blob ) ) ) ;
3333
34- const s3CountByStatus = ( status ) => ( { oneFirst , run } ) => {
34+ const s3CountByStatus = ( status ) => ( db ) => {
3535 // Note: This takes out rowlocks on all rows in `blobs`, so
3636 // if this is run in a transaction, then those locks aren't
3737 // released until the end of the transaction.
@@ -51,31 +51,25 @@ const s3CountByStatus = (status) => ({ oneFirst, run }) => {
5151 )` ;
5252 // in_progress is an implicit status
5353 if ( status === 'in_progress' ) {
54- return oneFirst ( sql `
55- SAVEPOINT peeklocked;
56- WITH ${ lockCTEs }
57- SELECT count FROM locked
58- ` ) . then ( cnt =>
59- run (
60- sql `ROLLBACK TO SAVEPOINT peeklocked`
61- ) . then ( ( ) => cnt )
54+ return db . transaction ( tx =>
55+ tx . oneFirst ( sql `
56+ WITH ${ lockCTEs }
57+ SELECT count FROM locked
58+ ` )
6259 ) ;
6360 } else if ( status === 'pending' ) {
64- return oneFirst ( sql `
65- SAVEPOINT peeklocked;
66- WITH
67- allpending AS (
68- SELECT COUNT(*) FROM blobs WHERE s3_status='pending'
69- ),
70- ${ lockCTEs }
71- SELECT allpending.count-locked.count FROM allpending, locked
72- ` ) . then ( cnt =>
73- run (
74- sql `ROLLBACK TO SAVEPOINT peeklocked`
75- ) . then ( ( ) => cnt )
61+ return db . transaction ( tx =>
62+ tx . oneFirst ( sql `
63+ WITH
64+ allpending AS (
65+ SELECT COUNT(*) FROM blobs WHERE s3_status='pending'
66+ ),
67+ ${ lockCTEs }
68+ SELECT allpending.count-locked.count FROM allpending, locked
69+ ` )
7670 ) ;
7771 } else {
78- return oneFirst ( sql `SELECT COUNT(*) FROM blobs WHERE s3_status=${ status } ` ) ;
72+ return db . oneFirst ( sql `SELECT COUNT(*) FROM blobs WHERE s3_status=${ status } ` ) ;
7973 }
8074} ;
8175
0 commit comments