use crate::adapters::files::models::FolderClaim;
use crate::store_paths::models::{Location, StoreKind};
pub fn folder_conflict(claim: &FolderClaim, claims: &[FolderClaim]) -> Option<String> {
let key = store_key(&claim.folder);
let other = claims.iter().find(|other| {
let other_key = store_key(&other.folder);
other.id != claim.id && (key.starts_with(&other_key) || other_key.starts_with(&key))
})?;
let hint = match key == store_key(&other.folder)
&& !claim.template.contains("{schema}")
&& claim.schema != other.schema
{
true => format!(
"; both come from tables named alike in schemas {} and {}, so add {{schema}} to the path",
claim.schema, other.schema
),
false => String::new(),
};
Some(format!(
"{} writes to {}, which overlaps {}'s folder {}; give each table a folder of its own{hint}",
claim.name, claim.folder, other.name, other.folder
))
}
fn store_key(folder: &Location) -> String {
match &folder.store {
StoreKind::S3 { bucket, .. } => format!("s3://{bucket}/{}", folder.prefix),
StoreKind::Filesystem { root } => {
let resolved = match std::fs::canonicalize(root) {
Ok(path) => path.display().to_string(),
Err(_) => root.clone(),
};
format!("{}/{}", resolved.trim_end_matches('/'), folder.prefix)
}
}
}