diff --git a/backend/windmill-api/src/settings.rs b/backend/windmill-api/src/settings.rs index 051bb4a176..4f762f19cd 100644 --- a/backend/windmill-api/src/settings.rs +++ b/backend/windmill-api/src/settings.rs @@ -701,7 +701,7 @@ async fn setup_ducklake_catalog_db_inner( sslmode = ssl_mode ); - let (client, connection) = if ssl_mode == "require" { + if ssl_mode == "require" { use native_tls::TlsConnector; use postgres_native_tls::MakeTlsConnector; @@ -709,7 +709,7 @@ async fn setup_ducklake_catalog_db_inner( connector.danger_accept_invalid_certs(true); connector.danger_accept_invalid_hostnames(true); - tokio::time::timeout( + let (client, connection) = tokio::time::timeout( std::time::Duration::from_secs(20), tokio_postgres::connect( &conn_str, @@ -718,43 +718,72 @@ async fn setup_ducklake_catalog_db_inner( ) .await .map_err(|e| error::Error::ExecutionErr(format!("timeout: {}", e.to_string())))? - .map_err(|e| error::Error::ExecutionErr(format!("error: {}", e.to_string())))? + .map_err(|e| error::Error::ExecutionErr(format!("error: {}", e.to_string())))?; + + let join_handle = tokio::spawn(async move { connection.await }); + logs.db_connect = "OK".to_string(); + + client + .batch_execute(&format!( + "GRANT CONNECT ON DATABASE \"{dbname}\" TO ducklake_user; + GRANT USAGE ON SCHEMA public TO ducklake_user; + GRANT CREATE ON SCHEMA public TO ducklake_user; + ALTER DEFAULT PRIVILEGES IN SCHEMA public + GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO ducklake_user;" + )) + .await + .map_err(|e| { + error::Error::ExecutionErr(format!( + "Failed to grant permissions to ducklake_user: {}", + e.to_string(), + )) + })?; + logs.grant_permissions = "OK".to_string(); + + drop(client); // /!\ Drop before joining to avoid deadlock + join_handle + .await + .map_err(|e| error::Error::ExecutionErr(format!("join error: {}", e.to_string())))? + .map_err(|e| { + error::Error::ExecutionErr(format!("tokio_postgres error: {}", e.to_string())) + })?; } else { - tokio::time::timeout( + let (client, connection) = tokio::time::timeout( std::time::Duration::from_secs(20), tokio_postgres::connect(&conn_str, tokio_postgres::NoTls), ) .await .map_err(|e| error::Error::ExecutionErr(format!("timeout: {}", e.to_string())))? - .map_err(|e| error::Error::ExecutionErr(format!("error: {}", e.to_string())))? - }; - let join_handle = tokio::spawn(async move { connection.await }); - logs.db_connect = "OK".to_string(); + .map_err(|e| error::Error::ExecutionErr(format!("error: {}", e.to_string())))?; - client - .batch_execute(&format!( - "GRANT CONNECT ON DATABASE \"{dbname}\" TO ducklake_user; - GRANT USAGE ON SCHEMA public TO ducklake_user; - GRANT CREATE ON SCHEMA public TO ducklake_user; - ALTER DEFAULT PRIVILEGES IN SCHEMA public - GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO ducklake_user;" - )) - .await - .map_err(|e| { - error::Error::ExecutionErr(format!( - "Failed to grant permissions to ducklake_user: {}", - e.to_string(), + let join_handle = tokio::spawn(async move { connection.await }); + logs.db_connect = "OK".to_string(); + + client + .batch_execute(&format!( + "GRANT CONNECT ON DATABASE \"{dbname}\" TO ducklake_user; + GRANT USAGE ON SCHEMA public TO ducklake_user; + GRANT CREATE ON SCHEMA public TO ducklake_user; + ALTER DEFAULT PRIVILEGES IN SCHEMA public + GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO ducklake_user;" )) - })?; - logs.grant_permissions = "OK".to_string(); + .await + .map_err(|e| { + error::Error::ExecutionErr(format!( + "Failed to grant permissions to ducklake_user: {}", + e.to_string(), + )) + })?; + logs.grant_permissions = "OK".to_string(); - drop(client); // /!\ Drop before joining to avoid deadlock - join_handle - .await - .map_err(|e| error::Error::ExecutionErr(format!("join error: {}", e.to_string())))? - .map_err(|e| { - error::Error::ExecutionErr(format!("tokio_postgres error: {}", e.to_string())) - })?; + drop(client); // /!\ Drop before joining to avoid deadlock + join_handle + .await + .map_err(|e| error::Error::ExecutionErr(format!("join error: {}", e.to_string())))? + .map_err(|e| { + error::Error::ExecutionErr(format!("tokio_postgres error: {}", e.to_string())) + })?; + } Ok(()) }