@@ -222,18 +222,30 @@ void main() {
222222 await createTables (db);
223223
224224 var tp = db.writeTransaction ((tx) async {
225- tx.execute ('INSERT INTO test_data(description) VALUES(?)' , ['test1' ]);
226- tx.execute ('INSERT INTO test_data(description) VALUES(?)' , ['test2' ]);
227- ignore (tx.execute ('INSERT INTO test_data(description) VALUES(json(?))' ,
228- ['test3' ])); // Errors
229- // Will not be executed because of the above error
225+ tx.execute (
226+ 'INSERT OR ROLLBACK INTO test_data(id, description) VALUES(?, ?)' ,
227+ [1 , 'test1' ]);
228+ tx.execute (
229+ 'INSERT OR ROLLBACK INTO test_data(id, description) VALUES(?, ?)' ,
230+ [2 , 'test2' ]);
230231 ignore (tx.execute (
231- 'INSERT INTO test_data(description) VALUES(?) RETURNING *' ,
232- ['test4' ]));
232+ 'INSERT OR ROLLBACK INTO test_data(id, description) VALUES(?, ?)' ,
233+ [2 , 'test3' ])); // Errors
234+
235+ // Will not be executed because of the above rollback
236+ ignore (tx.execute (
237+ 'INSERT OR ROLLBACK INTO test_data(id, description) VALUES(?, ?)' ,
238+ [4 , 'test4' ]));
233239 });
234240
235241 // The error propagates up to the transaction
236- await expectLater (tp, throwsA ((e) => e is sqlite.SqliteException ));
242+ await expectLater (
243+ tp,
244+ throwsA ((e) =>
245+ e is sqlite.SqliteException &&
246+ e.message
247+ .contains ('Transaction rolled back by earlier statement' ) &&
248+ e.message.contains ('UNIQUE constraint failed' )));
237249
238250 expect (await db.get ('SELECT count() count FROM test_data' ),
239251 equals ({'count' : 0 }));
@@ -321,6 +333,27 @@ void main() {
321333 });
322334 expect (computed, equals (5 ));
323335 });
336+
337+ test ('should allow resuming transaction after errors' , () async {
338+ final db = await setupDatabase (path: path);
339+ await createTables (db);
340+ await db.writeTransaction ((tx) async {
341+ var caught = false ;
342+ try {
343+ // This error does not rollback the transaction
344+ await tx.execute ('NOT A VALID STATEMENT' );
345+ } catch (e) {
346+ // Ignore
347+ caught = true ;
348+ }
349+ expect (caught, equals (true ));
350+
351+ final rs = await tx.execute (
352+ 'INSERT INTO test_data(description) VALUES(?) RETURNING description' ,
353+ ['Test Data' ]);
354+ expect (rs.rows[0 ], equals (['Test Data' ]));
355+ });
356+ });
324357 });
325358}
326359
0 commit comments