From 8315c05c6ad683ab07c77fc2dafdcca93832d9be Mon Sep 17 00:00:00 2001 From: Matthias Seiffert Date: Sat, 19 Oct 2024 15:50:31 +0200 Subject: [PATCH 1/3] Add function run_cli_with_connection --- sea-orm-migration/src/cli.rs | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/sea-orm-migration/src/cli.rs b/sea-orm-migration/src/cli.rs index aa0d3668bc..70fb312ae7 100644 --- a/sea-orm-migration/src/cli.rs +++ b/sea-orm-migration/src/cli.rs @@ -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; @@ -34,6 +36,38 @@ where .unwrap_or_else(handle_error); } +/// Same as [`run_cli`] but you crate the [`DbConn`] yourself. +/// +/// 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(migrator: M, make_connection: F) +where + M: MigratorTrait, + F: FnOnce(ConnectOptions) -> Fut, + Fut: Future>, +{ + dotenv().ok(); + let cli = Cli::parse(); + + let url = cli + .database_url + .expect("Environment variable 'DATABASE_URL' not set"); + let schema = cli.database_schema.unwrap_or_else(|| "public".to_owned()); + + let connect_options = ConnectOptions::new(url) + .set_schema_search_path(schema) + .to_owned(); + + let db = make_connection(connect_options) + .await + .expect("Fail to acquire database connection"); + + run_migrate(migrator, &db, cli.command, cli.verbose) + .await + .unwrap_or_else(handle_error); +} + pub async fn run_migrate( _: M, db: &DbConn, From 171f20684c85f3cb52df54d4a1017fa67d777777 Mon Sep 17 00:00:00 2001 From: Matthias Seiffert Date: Tue, 24 Dec 2024 11:36:25 +0100 Subject: [PATCH 2/3] Delegate run_cli to run_cli_with_connection --- sea-orm-migration/src/cli.rs | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/sea-orm-migration/src/cli.rs b/sea-orm-migration/src/cli.rs index 70fb312ae7..bd44eddfe1 100644 --- a/sea-orm-migration/src/cli.rs +++ b/sea-orm-migration/src/cli.rs @@ -16,24 +16,7 @@ pub async fn run_cli(migrator: M) where M: MigratorTrait, { - dotenv().ok(); - let cli = Cli::parse(); - - let url = cli - .database_url - .expect("Environment variable 'DATABASE_URL' not set"); - let schema = cli.database_schema.unwrap_or_else(|| "public".to_owned()); - - let connect_options = ConnectOptions::new(url) - .set_schema_search_path(schema) - .to_owned(); - let db = &Database::connect(connect_options) - .await - .expect("Fail to acquire database connection"); - - run_migrate(migrator, db, cli.command, cli.verbose) - .await - .unwrap_or_else(handle_error); + run_cli_with_connection(migrator, Database::connect).await; } /// Same as [`run_cli`] but you crate the [`DbConn`] yourself. From 6257539a1dea8b7384f03120a05a4c501cc9bfd1 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Fri, 27 Dec 2024 09:57:32 +0000 Subject: [PATCH 3/3] Update sea-orm-migration/src/cli.rs --- sea-orm-migration/src/cli.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sea-orm-migration/src/cli.rs b/sea-orm-migration/src/cli.rs index bd44eddfe1..6716fb0119 100644 --- a/sea-orm-migration/src/cli.rs +++ b/sea-orm-migration/src/cli.rs @@ -19,7 +19,7 @@ where run_cli_with_connection(migrator, Database::connect).await; } -/// Same as [`run_cli`] but you crate the [`DbConn`] yourself. +/// 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