Skip to content

Commit 5db8cb0

Browse files
Lythenastyt2y3
andauthored
Allow modifying the connection in migrations (#2397)
* Add function run_cli_with_connection * Delegate run_cli to run_cli_with_connection * Update sea-orm-migration/src/cli.rs --------- Co-authored-by: Chris Tsang <[email protected]>
1 parent bead32a commit 5db8cb0

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

sea-orm-migration/src/cli.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
use std::future::Future;
2+
13
use clap::Parser;
24
use dotenvy::dotenv;
35
use std::{error::Error, fmt::Display, process::exit};
46
use tracing_subscriber::{prelude::*, EnvFilter};
57

6-
use sea_orm::{ConnectOptions, Database, DbConn};
8+
use sea_orm::{ConnectOptions, Database, DbConn, DbErr};
79
use sea_orm_cli::{run_migrate_generate, run_migrate_init, MigrateSubcommands};
810

911
use super::MigratorTrait;
@@ -13,6 +15,20 @@ const MIGRATION_DIR: &str = "./";
1315
pub async fn run_cli<M>(migrator: M)
1416
where
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

Comments
 (0)