use crate::adapters::databricks::main::stage::_check_volume::check_volume;
use crate::adapters::dispatch::main::session::single_text::single_text;
use crate::adapters::dispatch::models::Destination;
use crate::adapters::postgres::main::session::_check_session::check_session;
use crate::adapters::snowflake::main::stage::_check_stage::check_stage;
use crate::adapters::snowflake::models::ForcedStage;
use crate::file_store::main::open_folder::open_folder;
use crate::file_store::main::probe_folder::probe_folder;
use crate::loader::errors::LoaderError;
use crate::store_paths::models::Location;
pub async fn check_destination(destination: Destination) -> Result<String, LoaderError> {
match destination {
Destination::Files(files) => {
let folder = open_folder(&Location {
store: files.store.clone(),
prefix: String::new(),
})?;
let entries = probe_folder(&folder).await?;
Ok(format!(
"{} lists ({entries} entries at the top)",
folder.location
))
}
Destination::Databricks(databricks) => {
let query = "SELECT current_user() || ' on catalog ' || current_catalog()";
let destination = Destination::Databricks(databricks.clone());
let answer = single_text(&destination, query.to_owned())
.await?
.unwrap_or_default();
let checked = check_volume(&databricks).await?;
Ok(format!("{answer}; {checked}"))
}
Destination::BigQuery(bigquery) => {
let query = "SELECT SESSION_USER() || ' in project ' || @@project_id";
let location = bigquery.api.location().to_owned();
let destination = Destination::BigQuery(bigquery);
let answer = single_text(&destination, query.to_owned())
.await?
.unwrap_or_default();
Ok(format!("{answer}, jobs in {location}"))
}
Destination::ClickHouse(clickhouse) => {
let query = "SELECT currentUser() || ' on ClickHouse ' || version() || ', time zone ' || timezone()";
let destination = Destination::ClickHouse(clickhouse);
let answer = single_text(&destination, query.to_owned()).await?;
Ok(answer.unwrap_or_default())
}
Destination::Snowflake(snowflake) => {
let query = "SELECT CURRENT_USER() || ' as ' || CURRENT_ROLE() || ', warehouse ' || COALESCE(CURRENT_WAREHOUSE(), '-')";
let destination = Destination::Snowflake(snowflake.clone());
let answer = single_text(&destination, query.to_owned())
.await?
.unwrap_or_default();
let answer = format!("{answer}, signed in with {}", snowflake.auth.describe());
let ForcedStage::External(stage) = &snowflake.stage else {
return Ok(answer);
};
let checked = check_stage(&snowflake, stage).await?;
Ok(format!("{answer}; {checked}"))
}
Destination::Postgres(postgres) => check_session(&postgres).await,
#[cfg(feature = "duckdb")]
destination @ Destination::DuckDb(_) => {
let answer = single_text(&destination, version_query(&destination).to_owned()).await?;
Ok(answer.unwrap_or_default())
}
}
}
#[cfg(feature = "duckdb")]
fn version_query(destination: &Destination) -> &'static str {
match destination {
Destination::DuckDb(duckdb) if duckdb.motherduck => {
"SELECT 'MotherDuck ' || current_database() || ', DuckDB ' || version()"
}
_ => "SELECT 'DuckDB ' || version()",
}
}