1+ use std:: future:: Future ;
2+
13use clap:: Parser ;
24use dotenvy:: dotenv;
35use std:: { error:: Error , fmt:: Display , process:: exit} ;
46use tracing_subscriber:: { prelude:: * , EnvFilter } ;
57
6- use sea_orm:: { ConnectOptions , Database , DbConn } ;
8+ use sea_orm:: { ConnectOptions , Database , DbConn , DbErr } ;
79use sea_orm_cli:: { run_migrate_generate, run_migrate_init, MigrateSubcommands } ;
810
911use super :: MigratorTrait ;
@@ -13,6 +15,20 @@ const MIGRATION_DIR: &str = "./";
1315pub async fn run_cli < M > ( migrator : M )
1416where
1517 M : MigratorTrait ,
18+ {
19+ run_cli_with_connection ( migrator, Database :: connect) . await ;
20+ }
21+
22+ /// Same as [`run_cli`] where you provide the function to create the [`DbConn`].
23+ ///
24+ /// This allows configuring the database connection as you see fit.
25+ /// E.g. you can change settings in [`ConnectOptions`] or you can load sqlite
26+ /// extensions.
27+ pub async fn run_cli_with_connection < M , F , Fut > ( migrator : M , make_connection : F )
28+ where
29+ M : MigratorTrait ,
30+ F : FnOnce ( ConnectOptions ) -> Fut ,
31+ Fut : Future < Output = Result < DbConn , DbErr > > ,
1632{
1733 dotenv ( ) . ok ( ) ;
1834 let cli = Cli :: parse ( ) ;
@@ -25,11 +41,12 @@ where
2541 let connect_options = ConnectOptions :: new ( url)
2642 . set_schema_search_path ( schema)
2743 . to_owned ( ) ;
28- let db = & Database :: connect ( connect_options)
44+
45+ let db = make_connection ( connect_options)
2946 . await
3047 . expect ( "Fail to acquire database connection" ) ;
3148
32- run_migrate ( migrator, db, cli. command , cli. verbose )
49+ run_migrate ( migrator, & db, cli. command , cli. verbose )
3350 . await
3451 . unwrap_or_else ( handle_error) ;
3552}
0 commit comments