use crate::adapters::dispatch::main::dialect::sql_adapter::sql_adapter;
use crate::column_types::models::TypeFit;
use crate::project::models::{OnSchemaChange, Policy};
use crate::schema_plan::constants::ALLOW_REBUILD_FLAG;
use crate::schema_plan::models::{
ColumnChange, ColumnOrigin, DesiredColumn, LiveColumn, SchemaRules,
};
#[derive(Debug, Default)]
pub(crate) struct ColumnDecision {
pub(crate) change: Option<ColumnChange>,
pub(crate) write: bool,
pub(crate) warning: Option<String>,
pub(crate) problem: Option<String>,
pub(crate) fix_flag: Option<&'static str>,
}
pub(crate) fn missing_column(column: &DesiredColumn, rules: SchemaRules) -> ColumnDecision {
let add = || ColumnChange::Add {
column: column.clone(),
sql_type: sql_adapter(rules.warehouse)
.render_loader_logical_type(&column.column_type)
.sql,
};
let declared = matches!(column.origin, ColumnOrigin::Pinned | ColumnOrigin::Metadata);
match rules.on_schema_change {
_ if declared => ColumnDecision {
change: Some(add()),
write: true,
..ColumnDecision::default()
},
OnSchemaChange::AppendNewColumns => ColumnDecision {
change: Some(add()),
write: true,
..ColumnDecision::default()
},
OnSchemaChange::Ignore => ColumnDecision {
warning: Some(format!(
"new field {} is not loaded (on_schema_change = \"ignore\")",
column.name
)),
..ColumnDecision::default()
},
OnSchemaChange::Fail => ColumnDecision {
problem: Some(format!(
"new field {} is not in the table and on_schema_change = \"fail\"",
column.name
)),
..ColumnDecision::default()
},
}
}
pub(crate) fn existing_column(
column: &DesiredColumn,
live: &LiveColumn,
rules: SchemaRules,
) -> ColumnDecision {
let written = ColumnDecision {
write: true,
..ColumnDecision::default()
};
let adapter = sql_adapter(rules.warehouse);
match (
adapter.type_fit(&column.column_type, &live.column_type),
adapter.runs_sql(),
) {
(TypeFit::Fits, _) => written,
(_, false) => ColumnDecision {
problem: Some(format!(
"{} is {} in the existing files but {} in the source; existing files cannot be altered, so pin the files' type (columns = {{ {} = \"{}\" }}) or rewrite every file with --reload",
live.name, live.column_type, column.column_type, column.name, live.column_type
)),
..ColumnDecision::default()
},
(TypeFit::WidenInPlace(sql_type), _) => ColumnDecision {
change: Some(ColumnChange::Widen {
column: live.name.clone(),
from: live.column_type.clone(),
to: column.column_type.clone(),
sql_type,
}),
..written
},
(TypeFit::NeedsRebuild(sql_type), _) => rebuild(column, live, rules, sql_type),
(TypeFit::Incompatible, _) => ColumnDecision {
problem: Some(mismatch(column, live)),
..ColumnDecision::default()
},
}
}
fn rebuild(
column: &DesiredColumn,
live: &LiveColumn,
rules: SchemaRules,
sql_type: String,
) -> ColumnDecision {
let change = ColumnChange::Rebuild {
column: live.name.clone(),
from: live.column_type.clone(),
to: column.column_type.clone(),
sql_type,
};
let description = format!(
"{} {} -> {} needs a table rebuild",
live.name, live.column_type, column.column_type
);
match (rules.type_change_rebuild, rules.rebuild_allowed) {
(Policy::Allow, _) | (Policy::RequireConfirmation, true) => ColumnDecision {
change: Some(change),
write: true,
..ColumnDecision::default()
},
(Policy::RequireConfirmation, false) => ColumnDecision {
problem: Some(format!(
"{description} (type_change_rebuild = \"require_confirmation\")"
)),
fix_flag: Some(ALLOW_REBUILD_FLAG),
..ColumnDecision::default()
},
(Policy::Deny, _) => ColumnDecision {
problem: Some(format!("{description}, and type_change_rebuild = \"deny\"")),
..ColumnDecision::default()
},
}
}
fn mismatch(column: &DesiredColumn, live: &LiveColumn) -> String {
let fix = match column.origin {
ColumnOrigin::Pinned => "change the pinned type, or migrate the column by hand".to_owned(),
_ => format!(
"pin a type the table can hold, for example columns = {{ {} = \"{}\" }}",
column.name, live.column_type
),
};
format!(
"{} is {} in the table but {} in the source; {fix}",
live.name, live.column_type, column.column_type
)
}