use crate::adapters::dispatch::main::dialect::sql_adapter::sql_adapter;
use crate::column_types::models::Warehouse;
use crate::project::models::{Deletes, SourceKind, WriteStrategy};
pub fn deletes_problems(
deletes: Option<Deletes>,
(strategy, kind): (WriteStrategy, SourceKind),
(warehouse, motherduck): (Warehouse, bool),
unnests: bool,
) -> Vec<String> {
if deletes.is_none() || kind == SourceKind::PostgresCdc {
return Vec::new();
}
let mut problems: Vec<String> = Vec::new();
if strategy != WriteStrategy::Merge {
problems.push(format!(
"deletes marks the rows whose unique_key the source no longer holds, which needs write_strategy = \"merge\"; {strategy} keeps no key to match. Fix: remove deletes, or merge on a unique_key"
));
}
if !matches!(
kind,
SourceKind::Postgres | SourceKind::Odbc | SourceKind::Snowflake | SourceKind::BigQuery
) {
problems.push(format!(
"deletes reads every key of the source table to find the ones it no longer holds, which only database and warehouse sources list; a {} source can't. Fix: remove deletes",
kind.name()
));
}
if motherduck || !sql_adapter(warehouse).marks_deleted_rows() {
let target = match motherduck {
true => "MotherDuck".to_owned(),
false => warehouse.to_string(),
};
problems.push(format!(
"deletes marks rows of the table in place with statements SQLLoad has run only on DuckDB and Postgres so far, so it refuses them on {target} rather than run them untested. Fix: remove deletes, or load into DuckDB or Postgres"
));
}
if unnests {
problems.push("deletes would leave the child tables' rows of each row it removes or marks. Fix: drop unnest, or remove deletes".to_owned());
}
problems
}