Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow modifying the connection in migrations #2397

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions sea-orm-migration/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::future::Future;

use clap::Parser;
use dotenvy::dotenv;
use std::{error::Error, fmt::Display, process::exit};
use tracing_subscriber::{prelude::*, EnvFilter};

use sea_orm::{ConnectOptions, Database, DbConn};
use sea_orm::{ConnectOptions, Database, DbConn, DbErr};
use sea_orm_cli::{run_migrate_generate, run_migrate_init, MigrateSubcommands};

use super::MigratorTrait;
Expand All @@ -13,6 +15,20 @@ const MIGRATION_DIR: &str = "./";
pub async fn run_cli<M>(migrator: M)
where
M: MigratorTrait,
{
run_cli_with_connection(migrator, Database::connect).await;
}

/// Same as [`run_cli`] where you provide the function to create the [`DbConn`].
///
/// This allows configuring the database connection as you see fit.
/// E.g. you can change settings in [`ConnectOptions`] or you can load sqlite
/// extensions.
pub async fn run_cli_with_connection<M, F, Fut>(migrator: M, make_connection: F)
where
M: MigratorTrait,
F: FnOnce(ConnectOptions) -> Fut,
Fut: Future<Output = Result<DbConn, DbErr>>,
{
dotenv().ok();
let cli = Cli::parse();
Expand All @@ -25,11 +41,12 @@ where
let connect_options = ConnectOptions::new(url)
.set_schema_search_path(schema)
.to_owned();
let db = &Database::connect(connect_options)

let db = make_connection(connect_options)
.await
.expect("Fail to acquire database connection");

run_migrate(migrator, db, cli.command, cli.verbose)
run_migrate(migrator, &db, cli.command, cli.verbose)
.await
.unwrap_or_else(handle_error);
}
Expand Down
Loading