@@ -222,18 +222,37 @@ void main() {
222
222
await createTables (db);
223
223
224
224
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
+ await tx.execute (
226
+ 'INSERT OR ROLLBACK INTO test_data(id, description) VALUES(?, ?)' ,
227
+ [1 , 'test1' ]);
228
+ await tx.execute (
229
+ 'INSERT OR ROLLBACK INTO test_data(id, description) VALUES(?, ?)' ,
230
+ [2 , 'test2' ]);
231
+ expect (await tx.getAutoCommit (), equals (false ));
232
+ try {
233
+ await tx.execute (
234
+ 'INSERT OR ROLLBACK INTO test_data(id, description) VALUES(?, ?)' ,
235
+ [2 , 'test3' ]);
236
+ } catch (e) {
237
+ // Ignore
238
+ }
239
+ expect (await tx.getAutoCommit (), equals (true ));
240
+ expect (tx.closed, equals (false ));
241
+
242
+ // Will not be executed because of the above rollback
230
243
ignore (tx.execute (
231
- 'INSERT INTO test_data(description) VALUES(?) RETURNING * ' ,
232
- ['test4' ]));
244
+ 'INSERT OR ROLLBACK INTO test_data(id, description) VALUES(?, ?) ' ,
245
+ [4 , 'test4' ]));
233
246
});
234
247
235
248
// The error propagates up to the transaction
236
- await expectLater (tp, throwsA ((e) => e is sqlite.SqliteException ));
249
+ await expectLater (
250
+ tp,
251
+ throwsA ((e) =>
252
+ e is sqlite.SqliteException &&
253
+ e.message
254
+ .contains ('Transaction rolled back by earlier statement' ) &&
255
+ e.message.contains ('UNIQUE constraint failed' )));
237
256
238
257
expect (await db.get ('SELECT count() count FROM test_data' ),
239
258
equals ({'count' : 0 }));
@@ -321,6 +340,34 @@ void main() {
321
340
});
322
341
expect (computed, equals (5 ));
323
342
});
343
+
344
+ test ('should allow resuming transaction after errors' , () async {
345
+ final db = await setupDatabase (path: path);
346
+ await createTables (db);
347
+ SqliteWriteContext ? savedTx;
348
+ await db.writeTransaction ((tx) async {
349
+ savedTx = tx;
350
+ var caught = false ;
351
+ try {
352
+ // This error does not rollback the transaction
353
+ await tx.execute ('NOT A VALID STATEMENT' );
354
+ } catch (e) {
355
+ // Ignore
356
+ caught = true ;
357
+ }
358
+ expect (caught, equals (true ));
359
+
360
+ expect (await tx.getAutoCommit (), equals (false ));
361
+ expect (tx.closed, equals (false ));
362
+
363
+ final rs = await tx.execute (
364
+ 'INSERT INTO test_data(description) VALUES(?) RETURNING description' ,
365
+ ['Test Data' ]);
366
+ expect (rs.rows[0 ], equals (['Test Data' ]));
367
+ });
368
+ expect (await savedTx! .getAutoCommit (), equals (true ));
369
+ expect (savedTx! .closed, equals (true ));
370
+ });
324
371
});
325
372
}
326
373
0 commit comments