use camino::Utf8Path;
use postgresql_embedded::Settings;
use super::{SettingsPaths, cluster_slot};
use crate::{error::BootstrapResult, observability::LOG_TARGET};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum DataLayout {
PerCluster,
Persistent,
}
pub(super) fn claim_derived_slot(
settings: &mut Settings,
paths: &mut SettingsPaths,
layout: DataLayout,
) -> BootstrapResult<bool> {
if layout == DataLayout::Persistent || !paths.data_default {
return Ok(false);
}
warn_on_old_layout(&paths.data_dir);
let slot = cluster_slot::claim_slot(&paths.data_dir)?;
settings.data_dir = slot.data_dir.clone().into_std_path_buf();
settings.password_file = slot.password_file.clone().into_std_path_buf();
paths.data_dir = slot.data_dir;
paths.password_file = slot.password_file;
Ok(true)
}
fn warn_on_old_layout(data_parent: &Utf8Path) {
if data_parent.join("PG_VERSION").is_file() {
tracing::warn!(
target: LOG_TARGET,
data_parent = %data_parent,
"a cluster from the 0.6.0 layout, or one initialized by `run`, remains here; \
test clusters now use per-cluster directories beneath it, so it is not used by \
them (set PG_DATA_DIR to share it)"
);
}
}