@@ -36,11 +36,9 @@ use crate::modelsv2::get_geometry_layer_table;
36
36
use crate :: modelsv2:: get_table;
37
37
use crate :: modelsv2:: prelude:: * ;
38
38
use crate :: modelsv2:: railjson:: persist_railjson;
39
- use crate :: modelsv2:: railjson:: persist_railjson_v2;
40
39
use crate :: modelsv2:: Create ;
41
40
use crate :: tables:: infra:: dsl;
42
41
use editoast_models:: DbConnection ;
43
- use editoast_models:: DbConnectionPool ;
44
42
use editoast_models:: DbConnectionPoolV2 ;
45
43
use editoast_schemas:: infra:: RailJson ;
46
44
use editoast_schemas:: infra:: RAILJSON_VERSION ;
@@ -77,16 +75,11 @@ pub struct Infra {
77
75
}
78
76
79
77
impl InfraChangeset {
80
- pub async fn persist (
81
- self ,
82
- railjson : RailJson ,
83
- db_pool : Arc < DbConnectionPool > ,
84
- ) -> Result < Infra > {
85
- let conn = & mut db_pool. get ( ) . await ?;
78
+ pub async fn persist ( self , railjson : RailJson , conn : & mut DbConnection ) -> Result < Infra > {
86
79
let infra = self . create ( conn) . await ?;
87
80
// TODO: lock infra for update
88
81
debug ! ( "🛤 Begin importing all railjson objects" ) ;
89
- if let Err ( e) = persist_railjson ( db_pool , infra. id , railjson) . await {
82
+ if let Err ( e) = persist_railjson ( conn , infra. id , railjson) . await {
90
83
error ! ( "Could not import infrastructure {}. Rolling back" , infra. id) ;
91
84
infra. delete ( conn) . await ?;
92
85
return Err ( e) ;
@@ -95,23 +88,6 @@ impl InfraChangeset {
95
88
Ok ( infra)
96
89
}
97
90
98
- pub async fn persist_v2 (
99
- self ,
100
- railjson : RailJson ,
101
- db_pool : Arc < DbConnectionPoolV2 > ,
102
- ) -> Result < Infra > {
103
- let infra = self . create ( db_pool. get ( ) . await ?. deref_mut ( ) ) . await ?;
104
- // TODO: lock infra for update
105
- debug ! ( "🛤 Begin importing all railjson objects" ) ;
106
- if let Err ( e) = persist_railjson_v2 ( db_pool. clone ( ) , infra. id , railjson) . await {
107
- error ! ( "Could not import infrastructure {}. Rolling back" , infra. id) ;
108
- infra. delete ( db_pool. get ( ) . await ?. deref_mut ( ) ) . await ?;
109
- return Err ( e) ;
110
- } ;
111
- debug ! ( "🛤 Import finished successfully" ) ;
112
- Ok ( infra)
113
- }
114
-
115
91
#[ must_use = "builder methods are intended to be chained" ]
116
92
pub fn last_railjson_version ( self ) -> Self {
117
93
self . railjson_version ( RAILJSON_VERSION . to_owned ( ) )
@@ -337,8 +313,6 @@ pub mod tests {
337
313
338
314
use super :: Infra ;
339
315
use crate :: error:: EditoastError ;
340
- use crate :: fixtures:: tests:: db_pool;
341
- use crate :: fixtures:: tests:: IntoFixture ;
342
316
use crate :: modelsv2:: fixtures:: create_empty_infra;
343
317
use crate :: modelsv2:: infra:: DEFAULT_INFRA_VERSION ;
344
318
use crate :: modelsv2:: prelude:: * ;
@@ -359,6 +333,8 @@ pub mod tests {
359
333
}
360
334
361
335
#[ rstest]
336
+ // PostgreSQL deadlock can happen in this test, see section `Deadlock` of [DbConnectionPoolV2::get] for more information
337
+ #[ serial_test:: serial]
362
338
async fn clone_infra_with_new_name_returns_new_cloned_infra ( ) {
363
339
// GIVEN
364
340
let db_pool = DbConnectionPoolV2 :: for_tests ( ) ;
@@ -378,15 +354,15 @@ pub mod tests {
378
354
#[ rstest]
379
355
#[ serial_test:: serial]
380
356
async fn persists_railjson_ko_version ( ) {
381
- let pool = db_pool ( ) ;
357
+ let db_pool = DbConnectionPoolV2 :: for_tests ( ) ;
382
358
let railjson_with_invalid_version = RailJson {
383
359
version : "0" . to_string ( ) ,
384
360
..Default :: default ( )
385
361
} ;
386
362
let res = Infra :: changeset ( )
387
363
. name ( "test" . to_owned ( ) )
388
364
. last_railjson_version ( )
389
- . persist ( railjson_with_invalid_version, pool )
365
+ . persist ( railjson_with_invalid_version, db_pool . get_ok ( ) . deref_mut ( ) )
390
366
. await ;
391
367
assert ! ( res. is_err( ) ) ;
392
368
let expected_error = RailJsonError :: UnsupportedVersion {
@@ -418,14 +394,13 @@ pub mod tests {
418
394
version : RAILJSON_VERSION . to_string ( ) ,
419
395
} ;
420
396
421
- let pool = db_pool ( ) ;
397
+ let db_pool = DbConnectionPoolV2 :: for_tests ( ) ;
422
398
let infra = Infra :: changeset ( )
423
399
. name ( "persist_railjson_ok_infra" . to_owned ( ) )
424
400
. last_railjson_version ( )
425
- . persist ( railjson. clone ( ) , pool . clone ( ) )
401
+ . persist ( railjson. clone ( ) , db_pool . get_ok ( ) . deref_mut ( ) )
426
402
. await
427
- . expect ( "could not persist infra" )
428
- . into_fixture ( pool. clone ( ) ) ;
403
+ . expect ( "could not persist infra" ) ;
429
404
430
405
// THEN
431
406
assert_eq ! ( infra. railjson_version, railjson. version) ;
@@ -435,51 +410,94 @@ pub mod tests {
435
410
objects
436
411
}
437
412
438
- let conn = & mut pool. get ( ) . await . unwrap ( ) ;
439
413
let id = infra. id ;
440
414
441
415
assert_eq ! (
442
- sort:: <BufferStop >( find_all_schemas( conn, id) . await . unwrap( ) ) ,
416
+ sort:: <BufferStop >(
417
+ find_all_schemas( db_pool. get_ok( ) . deref_mut( ) , id)
418
+ . await
419
+ . unwrap( )
420
+ ) ,
443
421
sort( railjson. buffer_stops)
444
422
) ;
445
423
assert_eq ! (
446
- sort:: <Route >( find_all_schemas( conn, id) . await . unwrap( ) ) ,
424
+ sort:: <Route >(
425
+ find_all_schemas( db_pool. get_ok( ) . deref_mut( ) , id)
426
+ . await
427
+ . unwrap( )
428
+ ) ,
447
429
sort( railjson. routes)
448
430
) ;
449
431
assert_eq ! (
450
- sort:: <SwitchType >( find_all_schemas( conn, id) . await . unwrap( ) ) ,
432
+ sort:: <SwitchType >(
433
+ find_all_schemas( db_pool. get_ok( ) . deref_mut( ) , id)
434
+ . await
435
+ . unwrap( )
436
+ ) ,
451
437
sort( railjson. extended_switch_types)
452
438
) ;
453
439
assert_eq ! (
454
- sort:: <Switch >( find_all_schemas( conn, id) . await . unwrap( ) ) ,
440
+ sort:: <Switch >(
441
+ find_all_schemas( db_pool. get_ok( ) . deref_mut( ) , id)
442
+ . await
443
+ . unwrap( )
444
+ ) ,
455
445
sort( railjson. switches)
456
446
) ;
457
447
assert_eq ! (
458
- sort:: <TrackSection >( find_all_schemas( conn, id) . await . unwrap( ) ) ,
448
+ sort:: <TrackSection >(
449
+ find_all_schemas( db_pool. get_ok( ) . deref_mut( ) , id)
450
+ . await
451
+ . unwrap( )
452
+ ) ,
459
453
sort( railjson. track_sections)
460
454
) ;
461
455
assert_eq ! (
462
- sort:: <SpeedSection >( find_all_schemas( conn, id) . await . unwrap( ) ) ,
456
+ sort:: <SpeedSection >(
457
+ find_all_schemas( db_pool. get_ok( ) . deref_mut( ) , id)
458
+ . await
459
+ . unwrap( )
460
+ ) ,
463
461
sort( railjson. speed_sections)
464
462
) ;
465
463
assert_eq ! (
466
- sort:: <NeutralSection >( find_all_schemas( conn, id) . await . unwrap( ) ) ,
464
+ sort:: <NeutralSection >(
465
+ find_all_schemas( db_pool. get_ok( ) . deref_mut( ) , id)
466
+ . await
467
+ . unwrap( )
468
+ ) ,
467
469
sort( railjson. neutral_sections)
468
470
) ;
469
471
assert_eq ! (
470
- sort:: <Electrification >( find_all_schemas( conn, id) . await . unwrap( ) ) ,
472
+ sort:: <Electrification >(
473
+ find_all_schemas( db_pool. get_ok( ) . deref_mut( ) , id)
474
+ . await
475
+ . unwrap( )
476
+ ) ,
471
477
sort( railjson. electrifications)
472
478
) ;
473
479
assert_eq ! (
474
- sort:: <Signal >( find_all_schemas( conn, id) . await . unwrap( ) ) ,
480
+ sort:: <Signal >(
481
+ find_all_schemas( db_pool. get_ok( ) . deref_mut( ) , id)
482
+ . await
483
+ . unwrap( )
484
+ ) ,
475
485
sort( railjson. signals)
476
486
) ;
477
487
assert_eq ! (
478
- sort:: <Detector >( find_all_schemas( conn, id) . await . unwrap( ) ) ,
488
+ sort:: <Detector >(
489
+ find_all_schemas( db_pool. get_ok( ) . deref_mut( ) , id)
490
+ . await
491
+ . unwrap( )
492
+ ) ,
479
493
sort( railjson. detectors)
480
494
) ;
481
495
assert_eq ! (
482
- sort:: <OperationalPoint >( find_all_schemas( conn, id) . await . unwrap( ) ) ,
496
+ sort:: <OperationalPoint >(
497
+ find_all_schemas( db_pool. get_ok( ) . deref_mut( ) , id)
498
+ . await
499
+ . unwrap( )
500
+ ) ,
483
501
sort( railjson. operational_points)
484
502
) ;
485
503
}
0 commit comments