use super::SchemaSyncChange;
use crate::table::{ForeignKeyConfig, SchemaColumn};
use std::collections::{BTreeMap, BTreeSet};
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ExistingIndex {
pub(super) name: String,
pub(super) unique: bool,
pub(super) columns: Vec<String>,
pub(super) nulls_not_distinct: bool,
pub(super) valid: bool,
pub(super) predicate: Option<String>,
pub(super) expressions: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct ExistingCheck {
pub(super) name: String,
pub(super) expression: String,
pub(super) validated: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct ExistingForeignKey {
pub(super) name: String,
pub(super) columns: Vec<String>,
pub(super) referenced_table: String,
pub(super) referenced_columns: Vec<String>,
pub(super) update_rule: String,
pub(super) delete_rule: String,
pub(super) validated: bool,
}
impl ExistingIndex {
pub(crate) fn new(name: impl Into<String>, unique: bool, columns: Vec<String>) -> Self {
Self {
name: name.into(),
unique,
columns,
nulls_not_distinct: false,
valid: true,
predicate: None,
expressions: Vec::new(),
}
}
#[cfg(feature = "postgres")]
pub(crate) fn with_nulls_not_distinct(mut self, nulls_not_distinct: bool) -> Self {
self.nulls_not_distinct = nulls_not_distinct;
self
}
#[cfg(feature = "postgres")]
pub(crate) fn with_valid(mut self, valid: bool) -> Self {
self.valid = valid;
self
}
#[cfg(feature = "postgres")]
pub(crate) fn with_predicate(mut self, predicate: String) -> Self {
self.predicate = Some(predicate);
self
}
#[cfg(feature = "postgres")]
pub(crate) fn with_expressions(mut self, expressions: Vec<String>) -> Self {
self.expressions = expressions;
self
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ExistingTableSchema {
pub(super) exists: bool,
pub(super) columns: Vec<SchemaColumn>,
pub(super) primary_key: Vec<String>,
pub(super) indexes: Vec<ExistingIndex>,
pub(super) checks: Vec<ExistingCheck>,
pub(super) foreign_keys: Vec<ExistingForeignKey>,
pub(super) has_rows: bool,
pub(super) comments: BTreeMap<String, String>,
pub(super) identity_columns: BTreeSet<String>,
pub(super) generated_columns: BTreeMap<String, String>,
pub(super) collations: BTreeMap<String, String>,
pub(super) enum_types: BTreeMap<String, Vec<String>>,
}
impl ExistingTableSchema {
pub(crate) fn missing() -> Self {
Self {
exists: false,
columns: Vec::new(),
primary_key: Vec::new(),
indexes: Vec::new(),
checks: Vec::new(),
foreign_keys: Vec::new(),
has_rows: false,
comments: BTreeMap::new(),
identity_columns: BTreeSet::new(),
generated_columns: BTreeMap::new(),
collations: BTreeMap::new(),
enum_types: BTreeMap::new(),
}
}
pub(crate) fn existing(
columns: Vec<SchemaColumn>,
primary_key: Vec<String>,
indexes: Vec<ExistingIndex>,
) -> Self {
Self {
exists: true,
columns,
primary_key,
indexes,
checks: Vec::new(),
foreign_keys: Vec::new(),
has_rows: false,
comments: BTreeMap::new(),
identity_columns: BTreeSet::new(),
generated_columns: BTreeMap::new(),
collations: BTreeMap::new(),
enum_types: BTreeMap::new(),
}
}
pub(crate) fn with_rows(mut self, has_rows: bool) -> Self {
self.has_rows = has_rows;
self
}
pub(super) fn with_constraints(
mut self,
checks: Vec<ExistingCheck>,
foreign_keys: Vec<ExistingForeignKey>,
) -> Self {
self.checks = checks;
self.foreign_keys = foreign_keys;
self
}
#[cfg(feature = "postgres")]
pub(super) fn with_comments(mut self, comments: BTreeMap<String, String>) -> Self {
self.comments = comments;
self
}
#[cfg(feature = "postgres")]
pub(super) fn with_column_kinds(
mut self,
identity_columns: BTreeSet<String>,
generated_columns: BTreeMap<String, String>,
) -> Self {
self.identity_columns = identity_columns;
self.generated_columns = generated_columns;
self
}
#[cfg(feature = "postgres")]
pub(super) fn with_collations(mut self, collations: BTreeMap<String, String>) -> Self {
self.collations = collations;
self
}
#[cfg(feature = "postgres")]
pub(super) fn with_enum_types(mut self, enum_types: BTreeMap<String, Vec<String>>) -> Self {
self.enum_types = enum_types;
self
}
pub(super) fn with_renamed_columns(&self, renamed_columns: &BTreeMap<String, String>) -> Self {
let rename = |name: &str| {
renamed_columns
.get(name)
.cloned()
.unwrap_or_else(|| name.to_string())
};
let mut effective = self.clone();
for column in &mut effective.columns {
column.name = rename(&column.name);
}
effective.primary_key = self.primary_key.iter().map(|name| rename(name)).collect();
for index in &mut effective.indexes {
index.columns = index.columns.iter().map(|name| rename(name)).collect();
}
effective.identity_columns = self
.identity_columns
.iter()
.map(|name| rename(name))
.collect();
effective.generated_columns = self
.generated_columns
.iter()
.map(|(name, expression)| (rename(name), expression.clone()))
.collect();
effective.collations = self
.collations
.iter()
.map(|(name, collation)| (rename(name), collation.clone()))
.collect();
effective.comments = self
.comments
.iter()
.map(|(name, comment)| (rename(name), comment.clone()))
.collect();
effective
}
}
#[derive(Debug)]
pub(crate) struct TableSyncPlan {
#[cfg_attr(not(test), allow(dead_code))]
pub(crate) statements: Vec<String>,
pub(crate) changes: Vec<SchemaSyncChange>,
pub(crate) steps: Vec<PlanStep>,
pub(crate) preflight: Vec<SchemaPreflightCheck>,
}
#[derive(Debug)]
pub(crate) struct PlanStep {
pub(crate) statements: Vec<String>,
pub(crate) deferred_statements: Vec<String>,
pub(crate) change: SchemaSyncChange,
pub(crate) concurrent: bool,
}
#[derive(Debug)]
pub(crate) enum SchemaPreflightCheck {
ColumnPredicate {
object: String,
predicate: String,
},
Check {
object: String,
expression: String,
},
ForeignKey {
object: String,
columns: Vec<String>,
referenced_table: String,
referenced_columns: Vec<String>,
},
UniqueIndex {
object: String,
columns: Vec<String>,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct DesiredIndex {
pub(super) name: String,
pub(super) unique: bool,
pub(super) columns: Vec<String>,
pub(super) expressions: Vec<String>,
pub(super) predicate: Option<String>,
pub(super) nulls_not_distinct: bool,
}
pub(super) fn existing_column_name(
column: &str,
renamed_columns: &BTreeMap<String, String>,
) -> String {
renamed_columns
.iter()
.find_map(|(legacy, current)| (current == column).then(|| legacy.clone()))
.unwrap_or_else(|| column.to_string())
}
pub(super) fn foreign_key_matches(
existing: &ExistingForeignKey,
desired: &ForeignKeyConfig,
renamed_columns: &BTreeMap<String, String>,
) -> bool {
let columns = existing
.columns
.iter()
.map(|column| {
renamed_columns
.get(column)
.cloned()
.unwrap_or_else(|| column.clone())
})
.collect::<Vec<_>>();
columns == desired.columns
&& existing.referenced_table == desired.referenced_table
&& existing.referenced_columns == desired.referenced_columns
&& matches!(existing.update_rule.as_str(), "RESTRICT" | "NO ACTION")
&& matches!(existing.delete_rule.as_str(), "RESTRICT" | "NO ACTION")
}