File tree 2 files changed +45
-3
lines changed
2 files changed +45
-3
lines changed Original file line number Diff line number Diff line change @@ -244,7 +244,7 @@ impl<'conn> Transaction<'conn> {
244
244
///
245
245
/// Panics if there is an active nested transaction.
246
246
pub fn transaction < ' a > ( & ' a self ) -> Result < Transaction < ' a > > {
247
- self . savepoint ( "sp" )
247
+ self . savepoint ( format ! ( "sp_{}" , self . depth ( ) ) )
248
248
}
249
249
250
250
/// Like `Connection::transaction`, but creates a nested transaction via
@@ -253,7 +253,14 @@ impl<'conn> Transaction<'conn> {
253
253
/// # Panics
254
254
///
255
255
/// Panics if there is an active nested transaction.
256
- pub fn savepoint < ' a > ( & ' a self , name : & str ) -> Result < Transaction < ' a > > {
256
+ #[ inline]
257
+ pub fn savepoint < ' a , I > ( & ' a self , name : I ) -> Result < Transaction < ' a > >
258
+ where I : Into < String >
259
+ {
260
+ self . _savepoint ( name. into ( ) )
261
+ }
262
+
263
+ fn _savepoint < ' a > ( & ' a self , name : String ) -> Result < Transaction < ' a > > {
257
264
let mut conn = self . conn . 0 . borrow_mut ( ) ;
258
265
check_desync ! ( conn) ;
259
266
assert ! (
@@ -265,7 +272,7 @@ impl<'conn> Transaction<'conn> {
265
272
Ok ( Transaction {
266
273
conn : self . conn ,
267
274
depth : self . depth + 1 ,
268
- savepoint_name : Some ( name. to_owned ( ) ) ,
275
+ savepoint_name : Some ( name) ,
269
276
commit : Cell :: new ( false ) ,
270
277
finished : false ,
271
278
} )
Original file line number Diff line number Diff line change @@ -331,6 +331,41 @@ fn test_nested_transactions_finish() {
331
331
) ;
332
332
}
333
333
334
+ #[ test]
335
+ fn test_nested_transactions_partial_rollback ( ) {
336
+ let conn = or_panic ! ( Connection :: connect(
337
+ "postgres://postgres@localhost:5433" ,
338
+ TlsMode :: None ,
339
+ ) ) ;
340
+ or_panic ! ( conn. execute( "CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)" , & [ ] ) ) ;
341
+
342
+ or_panic ! ( conn. execute( "INSERT INTO foo (id) VALUES ($1)" , & [ & 1i32 ] ) ) ;
343
+
344
+ {
345
+ let trans = or_panic ! ( conn. transaction( ) ) ;
346
+ or_panic ! ( trans. execute( "INSERT INTO foo (id) VALUES ($1)" , & [ & 2i32 ] ) ) ;
347
+ {
348
+ let trans = or_panic ! ( trans. transaction( ) ) ;
349
+ or_panic ! ( trans. execute( "INSERT INTO foo (id) VALUES ($1)" , & [ & 3i32 ] ) ) ;
350
+ {
351
+ let trans = or_panic ! ( trans. transaction( ) ) ;
352
+ or_panic ! ( trans. execute( "INSERT INTO foo (id) VALUES ($1)" , & [ & 4i32 ] ) ) ;
353
+ drop ( trans) ;
354
+ }
355
+ drop ( trans) ;
356
+ }
357
+ or_panic ! ( trans. commit( ) ) ;
358
+ }
359
+
360
+ let stmt = or_panic ! ( conn. prepare( "SELECT * FROM foo ORDER BY id" ) ) ;
361
+ let result = or_panic ! ( stmt. query( & [ ] ) ) ;
362
+
363
+ assert_eq ! (
364
+ vec![ 1i32 , 2 ] ,
365
+ result. iter( ) . map( |row| row. get( 0 ) ) . collect:: <Vec <i32 >>( )
366
+ ) ;
367
+ }
368
+
334
369
#[ test]
335
370
#[ should_panic( expected = "active transaction" ) ]
336
371
fn test_conn_trans_when_nested ( ) {
You can’t perform that action at this time.
0 commit comments