@@ -3,12 +3,8 @@ use barrel::backend::Pg as Sql;
33#[ cfg( feature = "tokio-postgres" ) ]
44mod tokio_postgres {
55 use futures:: FutureExt ;
6- use refinery:: {
7- config:: { Config , ConfigDbType } ,
8- embed_migrations,
9- error:: Kind ,
10- AsyncMigrate , Migration , Runner , Target ,
11- } ;
6+ use refinery_core:: config:: Config ;
7+ use refinery:: { embed_migrations, error:: Kind , AsyncMigrate , Migration , Runner , Target } ;
128 use refinery_core:: tokio_postgres;
139 use refinery_core:: tokio_postgres:: NoTls ;
1410 use std:: panic:: AssertUnwindSafe ;
@@ -17,6 +13,23 @@ mod tokio_postgres {
1713
1814 const DEFAULT_TABLE_NAME : & str = "refinery_schema_history" ;
1915
16+ fn db_url ( db : & str ) -> String {
17+ let base = std:: env:: var ( "POSTGRES_URL" )
18+ . unwrap_or_else ( |_| "postgres://postgres@localhost:5432" . to_string ( ) ) ;
19+ // Skip past "://" then find the first '/' which separates authority from path
20+ let after_scheme = base. find ( "://" ) . map ( |i| i + 3 ) . unwrap_or ( 0 ) ;
21+ let base = if let Some ( pos) = base[ after_scheme..] . find ( '/' ) {
22+ base[ ..after_scheme + pos] . to_string ( )
23+ } else {
24+ base
25+ } ;
26+ format ! ( "{base}/{db}" )
27+ }
28+
29+ fn db_config ( db : & str ) -> Config {
30+ Config :: from_str ( & db_url ( db) ) . unwrap ( )
31+ }
32+
2033 fn get_migrations ( ) -> Vec < Migration > {
2134 embed_migrations ! ( "./tests/migrations" ) ;
2235
@@ -72,10 +85,9 @@ mod tokio_postgres {
7285 }
7386
7487 async fn clean_database ( ) {
75- let ( client, connection) =
76- tokio_postgres:: connect ( "postgres://postgres@localhost:5432/template1" , NoTls )
77- . await
78- . unwrap ( ) ;
88+ let ( client, connection) = tokio_postgres:: connect ( db_url ( "template1" ) . as_str ( ) , NoTls )
89+ . await
90+ . unwrap ( ) ;
7991
8092 tokio:: spawn ( async move {
8193 connection. await . unwrap ( ) ;
@@ -100,7 +112,7 @@ mod tokio_postgres {
100112 async fn report_contains_applied_migrations ( ) {
101113 run_test ( async {
102114 let ( mut client, connection) =
103- tokio_postgres:: connect ( "postgres://postgres@localhost:5432/postgres" , NoTls )
115+ tokio_postgres:: connect ( db_url ( "postgres" ) . as_str ( ) , NoTls )
104116 . await
105117 . unwrap ( ) ;
106118
@@ -140,7 +152,7 @@ mod tokio_postgres {
140152 async fn creates_migration_table ( ) {
141153 run_test ( async {
142154 let ( mut client, connection) =
143- tokio_postgres:: connect ( "postgres://postgres@localhost:5432/postgres" , NoTls )
155+ tokio_postgres:: connect ( db_url ( "postgres" ) . as_str ( ) , NoTls )
144156 . await
145157 . unwrap ( ) ;
146158
@@ -175,7 +187,7 @@ mod tokio_postgres {
175187 async fn creates_migration_table_grouped_migrations ( ) {
176188 run_test ( async {
177189 let ( mut client, connection) =
178- tokio_postgres:: connect ( "postgres://postgres@localhost:5432/postgres" , NoTls )
190+ tokio_postgres:: connect ( db_url ( "postgres" ) . as_str ( ) , NoTls )
179191 . await
180192 . unwrap ( ) ;
181193
@@ -211,7 +223,7 @@ mod tokio_postgres {
211223 async fn applies_migration ( ) {
212224 run_test ( async {
213225 let ( mut client, connection) =
214- tokio_postgres:: connect ( "postgres://postgres@localhost:5432/postgres" , NoTls )
226+ tokio_postgres:: connect ( db_url ( "postgres" ) . as_str ( ) , NoTls )
215227 . await
216228 . unwrap ( ) ;
217229
@@ -250,7 +262,7 @@ mod tokio_postgres {
250262 async fn applies_migration_grouped ( ) {
251263 run_test ( async {
252264 let ( mut client, connection) =
253- tokio_postgres:: connect ( "postgres://postgres@localhost:5432/postgres" , NoTls )
265+ tokio_postgres:: connect ( db_url ( "postgres" ) . as_str ( ) , NoTls )
254266 . await
255267 . unwrap ( ) ;
256268
@@ -290,7 +302,7 @@ mod tokio_postgres {
290302 async fn updates_schema_history ( ) {
291303 run_test ( async {
292304 let ( mut client, connection) =
293- tokio_postgres:: connect ( "postgres://postgres@localhost:5432/postgres" , NoTls )
305+ tokio_postgres:: connect ( db_url ( "postgres" ) . as_str ( ) , NoTls )
294306 . await
295307 . unwrap ( ) ;
296308
@@ -322,7 +334,7 @@ mod tokio_postgres {
322334 async fn updates_schema_history_grouped ( ) {
323335 run_test ( async {
324336 let ( mut client, connection) =
325- tokio_postgres:: connect ( "postgres://postgres@localhost:5432/postgres" , NoTls )
337+ tokio_postgres:: connect ( db_url ( "postgres" ) . as_str ( ) , NoTls )
326338 . await
327339 . unwrap ( ) ;
328340
@@ -355,7 +367,7 @@ mod tokio_postgres {
355367 async fn updates_to_last_working_if_not_grouped ( ) {
356368 run_test ( async {
357369 let ( mut client, connection) =
358- tokio_postgres:: connect ( "postgres://postgres@localhost:5432/postgres" , NoTls )
370+ tokio_postgres:: connect ( db_url ( "postgres" ) . as_str ( ) , NoTls )
359371 . await
360372 . unwrap ( ) ;
361373
@@ -399,7 +411,7 @@ mod tokio_postgres {
399411 async fn doesnt_update_to_last_working_if_grouped ( ) {
400412 run_test ( async {
401413 let ( mut client, connection) =
402- tokio_postgres:: connect ( "postgres://postgres@localhost:5432/postgres" , NoTls )
414+ tokio_postgres:: connect ( db_url ( "postgres" ) . as_str ( ) , NoTls )
403415 . await
404416 . unwrap ( ) ;
405417
@@ -428,7 +440,7 @@ mod tokio_postgres {
428440 async fn gets_applied_migrations ( ) {
429441 run_test ( async {
430442 let ( mut client, connection) =
431- tokio_postgres:: connect ( "postgres://postgres@localhost:5432/postgres" , NoTls )
443+ tokio_postgres:: connect ( db_url ( "postgres" ) . as_str ( ) , NoTls )
432444 . await
433445 . unwrap ( ) ;
434446
@@ -470,7 +482,7 @@ mod tokio_postgres {
470482 async fn applies_new_migration ( ) {
471483 run_test ( async {
472484 let ( mut client, connection) =
473- tokio_postgres:: connect ( "postgres://postgres@localhost:5432/postgres" , NoTls )
485+ tokio_postgres:: connect ( db_url ( "postgres" ) . as_str ( ) , NoTls )
474486 . await
475487 . unwrap ( ) ;
476488
@@ -513,7 +525,7 @@ mod tokio_postgres {
513525 async fn migrates_to_target_migration ( ) {
514526 run_test ( async {
515527 let ( mut client, connection) =
516- tokio_postgres:: connect ( "postgres://postgres@localhost:5432/postgres" , NoTls )
528+ tokio_postgres:: connect ( db_url ( "postgres" ) . as_str ( ) , NoTls )
517529 . await
518530 . unwrap ( ) ;
519531
@@ -558,7 +570,7 @@ mod tokio_postgres {
558570 async fn migrates_to_target_migration_grouped ( ) {
559571 run_test ( async {
560572 let ( mut client, connection) =
561- tokio_postgres:: connect ( "postgres://postgres@localhost:5432/postgres" , NoTls )
573+ tokio_postgres:: connect ( db_url ( "postgres" ) . as_str ( ) , NoTls )
562574 . await
563575 . unwrap ( ) ;
564576
@@ -604,7 +616,7 @@ mod tokio_postgres {
604616 async fn aborts_on_missing_migration_on_filesystem ( ) {
605617 run_test ( async {
606618 let ( mut client, connection) =
607- tokio_postgres:: connect ( "postgres://postgres@localhost:5432/postgres" , NoTls )
619+ tokio_postgres:: connect ( db_url ( "postgres" ) . as_str ( ) , NoTls )
608620 . await
609621 . unwrap ( ) ;
610622
@@ -649,7 +661,7 @@ mod tokio_postgres {
649661 async fn aborts_on_divergent_migration ( ) {
650662 run_test ( async {
651663 let ( mut client, connection) =
652- tokio_postgres:: connect ( "postgres://postgres@localhost:5432/postgres" , NoTls )
664+ tokio_postgres:: connect ( db_url ( "postgres" ) . as_str ( ) , NoTls )
653665 . await
654666 . unwrap ( ) ;
655667
@@ -696,7 +708,7 @@ mod tokio_postgres {
696708 async fn aborts_on_missing_migration_on_database ( ) {
697709 run_test ( async {
698710 let ( mut client, connection) =
699- tokio_postgres:: connect ( "postgres://postgres@localhost:5432/postgres" , NoTls )
711+ tokio_postgres:: connect ( db_url ( "postgres" ) . as_str ( ) , NoTls )
700712 . await
701713 . unwrap ( ) ;
702714
@@ -752,11 +764,7 @@ mod tokio_postgres {
752764 #[ tokio:: test]
753765 async fn migrates_from_config ( ) {
754766 run_test ( async {
755- let mut config = Config :: new ( ConfigDbType :: Postgres )
756- . set_db_name ( "postgres" )
757- . set_db_user ( "postgres" )
758- . set_db_host ( "localhost" )
759- . set_db_port ( "5432" ) ;
767+ let mut config = db_config ( "postgres" ) ;
760768
761769 let migrations = get_migrations ( ) ;
762770 let runner = Runner :: new ( & migrations)
@@ -796,11 +804,7 @@ mod tokio_postgres {
796804 #[ tokio:: test]
797805 async fn migrate_from_config_report_contains_migrations ( ) {
798806 run_test ( async {
799- let mut config = Config :: new ( ConfigDbType :: Postgres )
800- . set_db_name ( "postgres" )
801- . set_db_user ( "postgres" )
802- . set_db_host ( "localhost" )
803- . set_db_port ( "5432" ) ;
807+ let mut config = db_config ( "postgres" ) ;
804808
805809 let migrations = get_migrations ( ) ;
806810 let runner = Runner :: new ( & migrations)
@@ -837,11 +841,7 @@ mod tokio_postgres {
837841 #[ tokio:: test]
838842 async fn migrate_from_config_report_returns_last_applied_migration ( ) {
839843 run_test ( async {
840- let mut config = Config :: new ( ConfigDbType :: Postgres )
841- . set_db_name ( "postgres" )
842- . set_db_user ( "postgres" )
843- . set_db_host ( "localhost" )
844- . set_db_port ( "5432" ) ;
844+ let mut config = db_config ( "postgres" ) ;
845845
846846 let migrations = get_migrations ( ) ;
847847 let runner = Runner :: new ( & migrations)
@@ -869,7 +869,7 @@ mod tokio_postgres {
869869 async fn doesnt_run_migrations_if_fake ( ) {
870870 run_test ( async {
871871 let ( mut client, connection) =
872- tokio_postgres:: connect ( "postgres://postgres@localhost:5432/postgres" , NoTls )
872+ tokio_postgres:: connect ( db_url ( "postgres" ) . as_str ( ) , NoTls )
873873 . await
874874 . unwrap ( ) ;
875875
@@ -913,7 +913,7 @@ mod tokio_postgres {
913913 async fn doesnt_run_migrations_if_fake_version ( ) {
914914 run_test ( async {
915915 let ( mut client, connection) =
916- tokio_postgres:: connect ( "postgres://postgres@localhost:5432/postgres" , NoTls )
916+ tokio_postgres:: connect ( db_url ( "postgres" ) . as_str ( ) , NoTls )
917917 . await
918918 . unwrap ( ) ;
919919
@@ -953,12 +953,45 @@ mod tokio_postgres {
953953 . await ;
954954 }
955955
956+ #[ cfg( feature = "tokio-postgres-tls" ) ]
956957 #[ tokio:: test]
957958 async fn migrates_with_tls_enabled ( ) {
958959 run_test ( async {
959960 let mut config =
960- Config :: from_str ( "postgres://postgres@localhost:5432/postgres?sslmode=require" )
961- . unwrap ( ) ;
961+ Config :: from_str ( & format ! ( "{}?sslmode=require" , db_url( "postgres" ) ) ) . unwrap ( ) ;
962+
963+ let migrations = get_migrations ( ) ;
964+ let runner = Runner :: new ( & migrations)
965+ . set_grouped ( false )
966+ . set_abort_divergent ( true )
967+ . set_abort_missing ( true ) ;
968+
969+ let report = runner. run_async ( & mut config) . await . unwrap ( ) ;
970+
971+ let applied_migrations = report. applied_migrations ( ) ;
972+ assert_eq ! ( 5 , applied_migrations. len( ) ) ;
973+
974+ let last_migration = runner
975+ . get_last_applied_migration_async ( & mut config)
976+ . await
977+ . unwrap ( )
978+ . unwrap ( ) ;
979+
980+ assert_eq ! ( 5 , last_migration. version( ) ) ;
981+ assert_eq ! ( migrations[ 4 ] . name( ) , last_migration. name( ) ) ;
982+ assert_eq ! ( migrations[ 4 ] . checksum( ) , last_migration. checksum( ) ) ;
983+
984+ assert ! ( config. use_tls( ) ) ;
985+ } )
986+ . await ;
987+ }
988+
989+ #[ cfg( feature = "tokio-postgres-rustls" ) ]
990+ #[ tokio:: test]
991+ async fn migrates_with_rustls_enabled ( ) {
992+ run_test ( async {
993+ let mut config =
994+ Config :: from_str ( & format ! ( "{}?sslmode=require" , db_url( "postgres" ) ) ) . unwrap ( ) ;
962995
963996 let migrations = get_migrations ( ) ;
964997 let runner = Runner :: new ( & migrations)
0 commit comments