@@ -26,6 +26,7 @@ interface SqlExecuteOption {
2626 onStart ?: ( ) => void ;
2727 skipProtection ?: boolean ;
2828 disableAnalyze ?: boolean ;
29+ insideTransaction ?: boolean ;
2930}
3031
3132export class SqlRunnerManager {
@@ -44,6 +45,12 @@ export class SqlRunnerManager {
4445 const result : SqlStatementResult [ ] = [ ] ;
4546 const parser = new Parser ( ) ;
4647
48+ // We only wrap transaction if it is multiple statement and
49+ // insideTransactin is specified. Single statement, by itself, is
50+ // transactional already.
51+ const shouldStartTransaction =
52+ ! ! options ?. insideTransaction && statements . length > 1 ;
53+
4754 const finalStatements : SqlStatementWithAnalyze [ ] = options ?. disableAnalyze
4855 ? statements
4956 : statements . map ( ( statement ) => {
@@ -65,33 +72,40 @@ export class SqlRunnerManager {
6572 }
6673 }
6774
68- for ( const statement of finalStatements ) {
69- for ( const cb of this . beforeEachCallbacks ) {
70- if ( ! ( await cb ( statement , options ?. skipProtection ) ) ) {
71- throw 'Cancel' ;
75+ try {
76+ if ( shouldStartTransaction ) await this . executor ( 'START TRANSACTION;' ) ;
77+ for ( const statement of finalStatements ) {
78+ for ( const cb of this . beforeEachCallbacks ) {
79+ if ( ! ( await cb ( statement , options ?. skipProtection ) ) ) {
80+ throw 'Cancel' ;
81+ }
7282 }
73- }
7483
75- if ( options ?. onStart ) options . onStart ( ) ;
84+ if ( options ?. onStart ) options . onStart ( ) ;
7685
77- console . log ( statement . sql ) ;
86+ console . log ( statement . sql ) ;
7887
79- const returnedResult = await this . executor (
80- statement . sql ,
81- statement . params
82- ) ;
88+ const returnedResult = await this . executor (
89+ statement . sql ,
90+ statement . params
91+ ) ;
8392
84- if ( ! returnedResult ?. error ) {
85- result . push ( {
86- statement,
87- result : returnedResult ,
88- } ) ;
89- } else {
90- throw returnedResult . error ;
93+ if ( ! returnedResult ?. error ) {
94+ result . push ( {
95+ statement,
96+ result : returnedResult ,
97+ } ) ;
98+ } else {
99+ throw returnedResult . error ;
100+ }
91101 }
92- }
93102
94- return result ;
103+ if ( shouldStartTransaction ) await this . executor ( 'COMMIT;' ) ;
104+ return result ;
105+ } catch ( e ) {
106+ if ( shouldStartTransaction ) await this . executor ( 'ROLLBACK;' ) ;
107+ throw e ;
108+ }
95109 }
96110
97111 registerBeforeAll ( cb : BeforeAllEventCallback ) {
0 commit comments