use crate::column_types::models::{ColumnType, Warehouse};
use crate::project::models::{Contract, OnSchemaChange, Policy};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColumnOrigin {
Pinned,
Source,
Inferred,
Metadata,
Constant,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DesiredColumn {
pub name: String,
pub column_type: ColumnType,
pub origin: ColumnOrigin,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LiveColumn {
pub name: String,
pub column_type: ColumnType,
pub fraction_digits: Option<i64>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SchemaRules {
pub warehouse: Warehouse,
pub contract: Contract,
pub on_schema_change: OnSchemaChange,
pub auto_widen: bool,
pub type_change_rebuild: Policy,
pub rebuild_allowed: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ColumnChange {
Add {
column: DesiredColumn,
sql_type: String,
},
Widen {
column: String,
from: ColumnType,
to: ColumnType,
sql_type: String,
},
Rebuild {
column: String,
from: ColumnType,
to: ColumnType,
sql_type: String,
},
}
impl std::fmt::Display for ColumnChange {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Add { column, sql_type } => write!(
formatter,
"add column {} {} -> {sql_type}",
column.name, column.column_type
),
Self::Widen {
column,
from,
to,
sql_type,
} => write!(
formatter,
"widen {column} {from} -> {to} ({sql_type}) in place"
),
Self::Rebuild {
column,
from,
to,
sql_type,
} => write!(formatter, "rebuild {column} {from} -> {to} ({sql_type})"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TableAction {
Create,
Recreate,
Alter(Vec<ColumnChange>),
Unchanged,
}
impl TableAction {
pub fn summary(&self) -> Vec<String> {
match self {
Self::Create => vec!["create".to_owned()],
Self::Recreate => vec!["recreate".to_owned()],
Self::Unchanged => vec!["unchanged".to_owned()],
Self::Alter(changes) => changes.iter().map(ToString::to_string).collect(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Blocked {
pub reasons: Vec<String>,
pub fix_flag: Option<&'static str>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SchemaPlan {
pub action: TableAction,
pub write_columns: Vec<DesiredColumn>,
pub warnings: Vec<String>,
pub blocked: Option<Blocked>,
}