use super::super::model::AddColumn;
use super::super::quote_ident;
use super::super::trigger::{Direction, Trigger};
use super::super::Plan;
pub(super) fn plan(op: &AddColumn, latest_schema: &str) -> Plan {
let mut sql = format!(
"ALTER TABLE {} ADD COLUMN IF NOT EXISTS {} {}",
quote_ident(&op.table),
quote_ident(&op.column),
op.column_type
);
if let Some(default) = &op.default {
sql.push_str(&format!(" DEFAULT {default}"));
}
let mut plan = Plan {
statements: vec![sql],
..Plan::default()
};
if let Some(up) = &op.up {
plan.statements.extend(
Trigger {
table: &op.table,
physical_column: &op.column,
direction: Direction::Up,
expression: up,
latest_schema,
}
.install(),
);
plan.backfill_tables.push(op.table.clone());
}
if !op.nullable {
plan.deferred_not_null
.push((op.table.clone(), op.column.clone()));
}
plan
}