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

Resolve connect timeout in schema discovery #2461

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions sea-orm-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,13 @@ pub enum GenerateSubcommands {
)]
max_connections: u32,

#[arg(
long,
default_value = "30",
long_help = "Acquire timeout in seconds of the connection used for schema discovery"
)]
acquire_timeout: u64,

#[arg(
short = 'o',
long,
Expand Down
31 changes: 24 additions & 7 deletions sea-orm-cli/src/commands/generate.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::time;
use sea_orm_codegen::{
DateTimeCrate as CodegenDateTimeCrate, EntityTransformer, EntityWriterContext, OutputFile,
WithSerde,
Expand All @@ -20,6 +21,7 @@ pub async fn run_generate_command(
tables,
ignore_tables,
max_connections,
acquire_timeout,
output_dir,
database_schema,
database_url,
Expand Down Expand Up @@ -114,7 +116,9 @@ pub async fn run_generate_command(

println!("Connecting to MySQL ...");
let connection =
sqlx_connect::<MySql>(max_connections, url.as_str(), None).await?;
sqlx_connect::<MySql>(max_connections, acquire_timeout, url.as_str(), None)
.await?;

println!("Discovering schema ...");
let schema_discovery = SchemaDiscovery::new(connection, database_name);
let schema = schema_discovery.discover().await?;
Expand All @@ -133,8 +137,14 @@ pub async fn run_generate_command(
use sqlx::Sqlite;

println!("Connecting to SQLite ...");
let connection =
sqlx_connect::<Sqlite>(max_connections, url.as_str(), None).await?;
let connection = sqlx_connect::<Sqlite>(
max_connections,
acquire_timeout,
url.as_str(),
None,
)
.await?;

println!("Discovering schema ...");
let schema_discovery = SchemaDiscovery::new(connection);
let schema = schema_discovery
Expand All @@ -157,9 +167,13 @@ pub async fn run_generate_command(

println!("Connecting to Postgres ...");
let schema = database_schema.as_deref().unwrap_or("public");
let connection =
sqlx_connect::<Postgres>(max_connections, url.as_str(), Some(schema))
.await?;
let connection = sqlx_connect::<Postgres>(
max_connections,
acquire_timeout,
url.as_str(),
Some(schema),
)
.await?;
println!("Discovering schema ...");
let schema_discovery = SchemaDiscovery::new(connection, schema);
let schema = schema_discovery.discover().await?;
Expand Down Expand Up @@ -222,14 +236,17 @@ pub async fn run_generate_command(

async fn sqlx_connect<DB>(
max_connections: u32,
acquire_timeout: u64,
url: &str,
schema: Option<&str>,
) -> Result<sqlx::Pool<DB>, Box<dyn Error>>
where
DB: sqlx::Database,
for<'a> &'a mut <DB as sqlx::Database>::Connection: sqlx::Executor<'a>,
{
let mut pool_options = sqlx::pool::PoolOptions::<DB>::new().max_connections(max_connections);
let mut pool_options = sqlx::pool::PoolOptions::<DB>::new()
.max_connections(max_connections)
.acquire_timeout(time::Duration::from_secs(acquire_timeout));
// Set search_path for Postgres, E.g. Some("public") by default
// MySQL & SQLite connection initialize with schema `None`
if let Some(schema) = schema {
Expand Down