use serde::{Deserialize, Serialize};
use crate::schema::types::{Column, DataType, DatabaseSchema};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DriftReport {
pub has_drift: bool,
pub new_tables: Vec<String>,
pub removed_tables: Vec<String>,
pub new_columns: Vec<ColumnRef>,
pub removed_columns: Vec<ColumnRef>,
pub changed_columns: Vec<ColumnChange>,
}
impl DriftReport {
pub fn summary(&self) -> String {
if !self.has_drift {
return "No schema drift detected.".to_string();
}
let mut lines = vec!["Schema drift detected:".to_string()];
for t in &self.new_tables {
lines.push(format!(" + table: {}", t));
}
for t in &self.removed_tables {
lines.push(format!(" - table: {}", t));
}
for c in &self.new_columns {
lines.push(format!(" + column: {}.{}", c.table, c.column));
}
for c in &self.removed_columns {
lines.push(format!(" - column: {}.{}", c.table, c.column));
}
for c in &self.changed_columns {
if c.table.is_empty() {
lines.push(format!(" ~ {} ({})", c.change_type, c.details));
} else if c.column.is_empty() {
lines.push(format!(
" ~ {}: {} ({})",
c.table, c.change_type, c.details
));
} else {
lines.push(format!(
" ~ {}.{}: {} ({})",
c.table, c.column, c.change_type, c.details
));
}
}
lines.join("\n")
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ColumnRef {
pub table: String,
pub column: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ColumnChange {
pub table: String,
pub column: String,
pub change_type: String,
pub details: String,
}
pub fn compute_schema_hash(schema: &DatabaseSchema) -> String {
use sha2::{Digest, Sha256};
let normalized = normalize_for_hash(schema);
let serialized = serde_json::to_string(&normalized).unwrap_or_default();
let mut hasher = Sha256::new();
hasher.update(serialized.as_bytes());
format!("{:x}", hasher.finalize())
}
fn normalize_for_hash(schema: &DatabaseSchema) -> DatabaseSchema {
let mut normalized = schema.clone();
for table in normalized.tables.values_mut() {
if normalized.database_type == crate::schema::types::DatabaseType::PostgreSQL
&& table.schema_name.as_deref() == Some("public")
{
table.schema_name = None;
}
table.foreign_keys.sort_by(|a, b| {
a.source_columns
.cmp(&b.source_columns)
.then_with(|| a.referenced_schema.cmp(&b.referenced_schema))
.then_with(|| a.referenced_table.cmp(&b.referenced_table))
});
table
.unique_constraints
.sort_by(|a, b| a.columns.cmp(&b.columns));
table
.check_constraints
.sort_by(|a, b| a.expression.cmp(&b.expression));
}
normalized
}
pub fn check_drift(
schema_snapshot: &DatabaseSchema,
schema_hash: &str,
current_schema: &DatabaseSchema,
) -> DriftReport {
let current_hash = compute_schema_hash(current_schema);
if current_hash == schema_hash {
return DriftReport {
has_drift: false,
new_tables: Vec::new(),
removed_tables: Vec::new(),
new_columns: Vec::new(),
removed_columns: Vec::new(),
changed_columns: Vec::new(),
};
}
let mut report = check_drift_detailed(schema_snapshot, current_schema);
if !report.has_drift {
report.has_drift = true;
report.changed_columns.push(ColumnChange {
table: String::new(),
column: String::new(),
change_type: "schema_changed".to_string(),
details: "the schema hash differs from seedkit.lock (for example a constraint \
name, foreign key action, or column order)"
.to_string(),
});
}
report
}
fn column_type(column: &Column) -> String {
let base = column.data_type.to_string();
match (
column.max_length,
column.numeric_precision,
column.numeric_scale,
) {
(Some(length), _, _) => format!("{base}({length})"),
(None, Some(precision), Some(scale)) if column.data_type == DataType::Numeric => {
format!("{base}({precision},{scale})")
}
_ => base,
}
}
fn labels(values: Option<&[String]>) -> String {
values.map_or("(none)".to_owned(), |v| format!("[{}]", v.join(", ")))
}
pub fn check_drift_detailed(old: &DatabaseSchema, current: &DatabaseSchema) -> DriftReport {
let mut new_tables = Vec::new();
let mut removed_tables = Vec::new();
let mut new_columns = Vec::new();
let mut removed_columns = Vec::new();
let mut changed_columns = Vec::new();
for table_name in current.tables.keys() {
if !old.tables.contains_key(table_name) {
new_tables.push(table_name.clone());
}
}
for table_name in old.tables.keys() {
if !current.tables.contains_key(table_name) {
removed_tables.push(table_name.clone());
}
}
for (table_name, current_table) in ¤t.tables {
let old_table = match old.tables.get(table_name) {
Some(t) => t,
None => continue, };
for col_name in current_table.columns.keys() {
if !old_table.columns.contains_key(col_name) {
new_columns.push(ColumnRef {
table: table_name.clone(),
column: col_name.clone(),
});
}
}
for col_name in old_table.columns.keys() {
if !current_table.columns.contains_key(col_name) {
removed_columns.push(ColumnRef {
table: table_name.clone(),
column: col_name.clone(),
});
}
}
for (col_name, current_col) in ¤t_table.columns {
if let Some(old_col) = old_table.columns.get(col_name) {
let change = |change_type: &str, details: String| ColumnChange {
table: table_name.clone(),
column: col_name.clone(),
change_type: change_type.into(),
details,
};
let (old_type, current_type) = (column_type(old_col), column_type(current_col));
if old_type != current_type {
changed_columns.push(change(
"type_changed",
format!("{old_type} → {current_type}"),
));
} else if old_col.raw_type != current_col.raw_type {
changed_columns.push(change(
"type_changed",
format!("{} → {}", old_col.raw_type, current_col.raw_type),
));
}
if current_col.is_auto_increment != old_col.is_auto_increment {
changed_columns.push(change(
"identity_changed",
format!(
"auto increment: {} → {}",
old_col.is_auto_increment, current_col.is_auto_increment
),
));
}
if current_col.has_default != old_col.has_default {
changed_columns.push(change(
"default_changed",
format!(
"has default: {} → {}",
old_col.has_default, current_col.has_default
),
));
}
if current_col.enum_values != old_col.enum_values {
changed_columns.push(change(
"enum_changed",
format!(
"labels: {} → {}",
labels(old_col.enum_values.as_deref()),
labels(current_col.enum_values.as_deref())
),
));
}
if current_col.is_generated != old_col.is_generated {
changed_columns.push(ColumnChange {
table: table_name.clone(),
column: col_name.clone(),
change_type: "generated_changed".into(),
details: format!(
"database generated: {} → {}",
old_col.is_generated, current_col.is_generated
),
});
}
if current_col.nullable != old_col.nullable {
changed_columns.push(ColumnChange {
table: table_name.clone(),
column: col_name.clone(),
change_type: "nullable_changed".to_string(),
details: format!(
"nullable: {} → {}",
old_col.nullable, current_col.nullable
),
});
}
}
}
let primary_key = |table: &crate::schema::types::Table| {
table
.primary_key
.as_ref()
.map_or("(none)".to_owned(), |pk| {
format!("({})", pk.columns.join(","))
})
};
let (old_pk, current_pk) = (primary_key(old_table), primary_key(current_table));
if old_pk != current_pk {
changed_columns.push(ColumnChange {
table: table_name.clone(),
column: String::new(),
change_type: "pk_changed".to_string(),
details: format!("PRIMARY KEY {old_pk} → {current_pk}"),
});
}
let old_fks: std::collections::BTreeSet<String> = old_table
.foreign_keys
.iter()
.map(|fk| {
format!(
"({}) -> {}({})",
fk.source_columns.join(","),
fk.target_name(),
fk.referenced_columns.join(",")
)
})
.collect();
let cur_fks: std::collections::BTreeSet<String> = current_table
.foreign_keys
.iter()
.map(|fk| {
format!(
"({}) -> {}({})",
fk.source_columns.join(","),
fk.target_name(),
fk.referenced_columns.join(",")
)
})
.collect();
for added in cur_fks.difference(&old_fks) {
changed_columns.push(ColumnChange {
table: table_name.clone(),
column: String::new(),
change_type: "fk_added".to_string(),
details: format!("FOREIGN KEY {}", added),
});
}
for removed in old_fks.difference(&cur_fks) {
changed_columns.push(ColumnChange {
table: table_name.clone(),
column: String::new(),
change_type: "fk_removed".to_string(),
details: format!("FOREIGN KEY {}", removed),
});
}
let old_uqs: std::collections::BTreeSet<String> = old_table
.unique_constraints
.iter()
.map(|uq| format!("({})", uq.columns.join(",")))
.collect();
let cur_uqs: std::collections::BTreeSet<String> = current_table
.unique_constraints
.iter()
.map(|uq| format!("({})", uq.columns.join(",")))
.collect();
for added in cur_uqs.difference(&old_uqs) {
changed_columns.push(ColumnChange {
table: table_name.clone(),
column: String::new(),
change_type: "unique_added".to_string(),
details: format!("UNIQUE {}", added),
});
}
for removed in old_uqs.difference(&cur_uqs) {
changed_columns.push(ColumnChange {
table: table_name.clone(),
column: String::new(),
change_type: "unique_removed".to_string(),
details: format!("UNIQUE {}", removed),
});
}
let old_cks: std::collections::BTreeSet<String> = old_table
.check_constraints
.iter()
.map(|ck| ck.expression.clone())
.collect();
let cur_cks: std::collections::BTreeSet<String> = current_table
.check_constraints
.iter()
.map(|ck| ck.expression.clone())
.collect();
for added in cur_cks.difference(&old_cks) {
changed_columns.push(ColumnChange {
table: table_name.clone(),
column: String::new(),
change_type: "check_added".to_string(),
details: format!("CHECK ({})", added),
});
}
for removed in old_cks.difference(&cur_cks) {
changed_columns.push(ColumnChange {
table: table_name.clone(),
column: String::new(),
change_type: "check_removed".to_string(),
details: format!("CHECK ({})", removed),
});
}
}
let enum_names: std::collections::BTreeSet<&String> =
old.enums.keys().chain(current.enums.keys()).collect();
for name in enum_names {
let (old_labels, current_labels) = (old.enums.get(name), current.enums.get(name));
if old_labels != current_labels {
changed_columns.push(ColumnChange {
table: String::new(),
column: String::new(),
change_type: "enum_changed".to_string(),
details: format!(
"ENUM {name}: {} → {}",
labels(old_labels.map(Vec::as_slice)),
labels(current_labels.map(Vec::as_slice))
),
});
}
}
let has_drift = !new_tables.is_empty()
|| !removed_tables.is_empty()
|| !new_columns.is_empty()
|| !removed_columns.is_empty()
|| !changed_columns.is_empty();
DriftReport {
has_drift,
new_tables,
removed_tables,
new_columns,
removed_columns,
changed_columns,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::schema::types::*;
#[allow(clippy::type_complexity)]
fn make_schema(tables: Vec<(&str, Vec<(&str, DataType, bool)>)>) -> DatabaseSchema {
let mut schema = DatabaseSchema::new(DatabaseType::PostgreSQL, "test".to_string());
for (table_name, cols) in tables {
let mut table = Table::new(table_name.to_string());
for (i, (col_name, dt, nullable)) in cols.iter().enumerate() {
let mut col = Column::new(col_name.to_string(), dt.clone(), dt.to_string());
col.nullable = *nullable;
col.ordinal_position = i as u32;
table.columns.insert(col_name.to_string(), col);
}
schema.tables.insert(table_name.to_string(), table);
}
schema
}
#[test]
fn public_schema_retains_legacy_lock_hash() {
let old = make_schema(vec![("items", vec![("id", DataType::Integer, false)])]);
let mut current = old.clone();
current.tables["items"].schema_name = Some("public".into());
assert_eq!(compute_schema_hash(&old), compute_schema_hash(¤t));
current.tables["items"].schema_name = Some("tenant_a".into());
assert_ne!(compute_schema_hash(&old), compute_schema_hash(¤t));
}
#[test]
fn test_no_drift_identical_schemas() {
let schema = make_schema(vec![(
"users",
vec![
("id", DataType::Serial, false),
("name", DataType::VarChar, false),
],
)]);
let report = check_drift_detailed(&schema, &schema);
assert!(!report.has_drift);
}
#[test]
fn test_new_table_detected() {
let old = make_schema(vec![("users", vec![("id", DataType::Serial, false)])]);
let current = make_schema(vec![
("users", vec![("id", DataType::Serial, false)]),
("posts", vec![("id", DataType::Serial, false)]),
]);
let report = check_drift_detailed(&old, ¤t);
assert!(report.has_drift);
assert_eq!(report.new_tables, vec!["posts"]);
}
#[test]
fn test_removed_table_detected() {
let old = make_schema(vec![
("users", vec![("id", DataType::Serial, false)]),
("posts", vec![("id", DataType::Serial, false)]),
]);
let current = make_schema(vec![("users", vec![("id", DataType::Serial, false)])]);
let report = check_drift_detailed(&old, ¤t);
assert!(report.has_drift);
assert_eq!(report.removed_tables, vec!["posts"]);
}
#[test]
fn test_new_column_detected() {
let old = make_schema(vec![("users", vec![("id", DataType::Serial, false)])]);
let current = make_schema(vec![(
"users",
vec![
("id", DataType::Serial, false),
("email", DataType::VarChar, false),
],
)]);
let report = check_drift_detailed(&old, ¤t);
assert!(report.has_drift);
assert_eq!(report.new_columns.len(), 1);
assert_eq!(report.new_columns[0].column, "email");
}
#[test]
fn test_type_change_detected() {
let old = make_schema(vec![("users", vec![("age", DataType::Integer, false)])]);
let current = make_schema(vec![("users", vec![("age", DataType::BigInt, false)])]);
let report = check_drift_detailed(&old, ¤t);
assert!(report.has_drift);
assert_eq!(report.changed_columns.len(), 1);
assert_eq!(report.changed_columns[0].change_type, "type_changed");
assert!(report.changed_columns[0].details.contains("integer"));
assert!(report.changed_columns[0].details.contains("bigint"));
}
#[test]
fn test_nullable_change_detected() {
let old = make_schema(vec![("users", vec![("name", DataType::VarChar, false)])]);
let current = make_schema(vec![("users", vec![("name", DataType::VarChar, true)])]);
let report = check_drift_detailed(&old, ¤t);
assert!(report.has_drift);
assert_eq!(report.changed_columns.len(), 1);
assert_eq!(report.changed_columns[0].change_type, "nullable_changed");
}
#[test]
fn test_schema_hash_deterministic() {
let schema = make_schema(vec![("users", vec![("id", DataType::Serial, false)])]);
let h1 = compute_schema_hash(&schema);
let h2 = compute_schema_hash(&schema);
assert_eq!(h1, h2);
}
#[test]
fn test_summary_no_drift() {
let report = DriftReport {
has_drift: false,
new_tables: Vec::new(),
removed_tables: Vec::new(),
new_columns: Vec::new(),
removed_columns: Vec::new(),
changed_columns: Vec::new(),
};
assert_eq!(report.summary(), "No schema drift detected.");
}
#[test]
fn test_summary_with_drift() {
let report = DriftReport {
has_drift: true,
new_tables: vec!["posts".to_string()],
removed_tables: vec!["legacy".to_string()],
new_columns: vec![ColumnRef {
table: "users".into(),
column: "email".into(),
}],
removed_columns: Vec::new(),
changed_columns: Vec::new(),
};
let s = report.summary();
assert!(s.contains("+ table: posts"));
assert!(s.contains("- table: legacy"));
assert!(s.contains("+ column: users.email"));
}
#[test]
fn test_check_drift_hash_match_fast_path() {
let schema = make_schema(vec![("users", vec![("id", DataType::Serial, false)])]);
let hash = compute_schema_hash(&schema);
let report = check_drift(&schema, &hash, &schema);
assert!(!report.has_drift);
}
#[test]
fn test_check_drift_hash_mismatch_produces_detailed_report() {
let old = make_schema(vec![("users", vec![("id", DataType::Serial, false)])]);
let old_hash = compute_schema_hash(&old);
let current = make_schema(vec![
(
"users",
vec![
("id", DataType::Serial, false),
("email", DataType::VarChar, false),
],
),
("posts", vec![("id", DataType::Serial, false)]),
]);
let report = check_drift(&old, &old_hash, ¤t);
assert!(report.has_drift);
assert_eq!(report.new_tables, vec!["posts"]);
assert_eq!(report.new_columns.len(), 1);
assert_eq!(report.new_columns[0].column, "email");
}
#[test]
fn test_check_drift_removed_column_detailed() {
let old = make_schema(vec![(
"users",
vec![
("id", DataType::Serial, false),
("legacy_field", DataType::Text, true),
],
)]);
let old_hash = compute_schema_hash(&old);
let current = make_schema(vec![("users", vec![("id", DataType::Serial, false)])]);
let report = check_drift(&old, &old_hash, ¤t);
assert!(report.has_drift);
assert_eq!(report.removed_columns.len(), 1);
assert_eq!(report.removed_columns[0].column, "legacy_field");
}
#[test]
fn test_hash_stable_regardless_of_fk_order() {
let mut schema_a = make_schema(vec![(
"orders",
vec![
("id", DataType::Serial, false),
("user_id", DataType::Integer, false),
("product_id", DataType::Integer, false),
],
)]);
schema_a.tables.get_mut("orders").unwrap().foreign_keys = vec![
ForeignKey {
name: Some("fk_user".to_string()),
source_columns: vec!["user_id".to_string()],
referenced_schema: None,
referenced_table: "users".to_string(),
referenced_columns: vec!["id".to_string()],
on_delete: ForeignKeyAction::Cascade,
on_update: ForeignKeyAction::NoAction,
is_deferrable: false,
},
ForeignKey {
name: Some("fk_product".to_string()),
source_columns: vec!["product_id".to_string()],
referenced_schema: None,
referenced_table: "products".to_string(),
referenced_columns: vec!["id".to_string()],
on_delete: ForeignKeyAction::NoAction,
on_update: ForeignKeyAction::NoAction,
is_deferrable: false,
},
];
let mut schema_b = schema_a.clone();
schema_b
.tables
.get_mut("orders")
.unwrap()
.foreign_keys
.reverse();
let hash_a = compute_schema_hash(&schema_a);
let hash_b = compute_schema_hash(&schema_b);
assert_eq!(hash_a, hash_b, "Hash must be stable regardless of FK order");
}
#[test]
fn test_hash_stable_regardless_of_unique_constraint_order() {
let mut schema_a = make_schema(vec![(
"users",
vec![
("id", DataType::Serial, false),
("email", DataType::VarChar, false),
("username", DataType::VarChar, false),
],
)]);
schema_a.tables.get_mut("users").unwrap().unique_constraints = vec![
UniqueConstraint {
name: Some("uq_email".to_string()),
columns: vec!["email".to_string()],
},
UniqueConstraint {
name: Some("uq_username".to_string()),
columns: vec!["username".to_string()],
},
];
let mut schema_b = schema_a.clone();
schema_b
.tables
.get_mut("users")
.unwrap()
.unique_constraints
.reverse();
let hash_a = compute_schema_hash(&schema_a);
let hash_b = compute_schema_hash(&schema_b);
assert_eq!(
hash_a, hash_b,
"Hash must be stable regardless of unique constraint order"
);
}
#[test]
fn test_hash_stable_regardless_of_check_constraint_order() {
let mut schema_a = make_schema(vec![(
"products",
vec![
("price", DataType::Numeric, false),
("quantity", DataType::Integer, false),
],
)]);
schema_a
.tables
.get_mut("products")
.unwrap()
.check_constraints = vec![
CheckConstraint {
name: Some("ck_price".to_string()),
expression: "price >= 0".to_string(),
parsed: None,
},
CheckConstraint {
name: Some("ck_qty".to_string()),
expression: "quantity > 0".to_string(),
parsed: None,
},
];
let mut schema_b = schema_a.clone();
schema_b
.tables
.get_mut("products")
.unwrap()
.check_constraints
.reverse();
let hash_a = compute_schema_hash(&schema_a);
let hash_b = compute_schema_hash(&schema_b);
assert_eq!(
hash_a, hash_b,
"Hash must be stable regardless of check constraint order"
);
}
#[test]
fn test_drift_detects_added_foreign_key() {
let old = make_schema(vec![(
"orders",
vec![
("id", DataType::Serial, false),
("user_id", DataType::Integer, false),
],
)]);
let mut current = old.clone();
current
.tables
.get_mut("orders")
.unwrap()
.foreign_keys
.push(ForeignKey {
name: Some("fk_user".to_string()),
source_columns: vec!["user_id".to_string()],
referenced_schema: None,
referenced_table: "users".to_string(),
referenced_columns: vec!["id".to_string()],
on_delete: ForeignKeyAction::Cascade,
on_update: ForeignKeyAction::NoAction,
is_deferrable: false,
});
let report = check_drift_detailed(&old, ¤t);
assert!(report.has_drift);
assert!(
report
.changed_columns
.iter()
.any(|c| c.change_type == "fk_added"),
"Should detect added FK: {:?}",
report.changed_columns
);
}
#[test]
fn test_drift_detects_removed_unique_constraint() {
let mut old = make_schema(vec![(
"users",
vec![
("id", DataType::Serial, false),
("email", DataType::VarChar, false),
],
)]);
old.tables
.get_mut("users")
.unwrap()
.unique_constraints
.push(UniqueConstraint {
name: Some("uq_email".to_string()),
columns: vec!["email".to_string()],
});
let current = make_schema(vec![(
"users",
vec![
("id", DataType::Serial, false),
("email", DataType::VarChar, false),
],
)]);
let report = check_drift_detailed(&old, ¤t);
assert!(report.has_drift);
assert!(
report
.changed_columns
.iter()
.any(|c| c.change_type == "unique_removed"),
"Should detect removed unique constraint: {:?}",
report.changed_columns
);
}
#[test]
fn test_drift_detects_removed_check_constraint() {
let mut old = make_schema(vec![(
"products",
vec![("price", DataType::Numeric, false)],
)]);
old.tables
.get_mut("products")
.unwrap()
.check_constraints
.push(CheckConstraint {
name: Some("ck_price".to_string()),
expression: "price >= 0".to_string(),
parsed: None,
});
let current = make_schema(vec![(
"products",
vec![("price", DataType::Numeric, false)],
)]);
let report = check_drift_detailed(&old, ¤t);
assert!(report.has_drift);
assert!(
report
.changed_columns
.iter()
.any(|c| c.change_type == "check_removed"),
"Should detect removed check constraint: {:?}",
report.changed_columns
);
}
#[test]
fn test_drift_no_false_positive_on_identical_constraints() {
let mut schema = make_schema(vec![(
"orders",
vec![
("id", DataType::Serial, false),
("user_id", DataType::Integer, false),
],
)]);
schema
.tables
.get_mut("orders")
.unwrap()
.foreign_keys
.push(ForeignKey {
name: Some("fk_user".to_string()),
source_columns: vec!["user_id".to_string()],
referenced_schema: None,
referenced_table: "users".to_string(),
referenced_columns: vec!["id".to_string()],
on_delete: ForeignKeyAction::Cascade,
on_update: ForeignKeyAction::NoAction,
is_deferrable: false,
});
schema
.tables
.get_mut("orders")
.unwrap()
.unique_constraints
.push(UniqueConstraint {
name: Some("uq_user".to_string()),
columns: vec!["user_id".to_string()],
});
let report = check_drift_detailed(&schema, &schema);
assert!(
!report.has_drift,
"Identical schemas with constraints should not report drift"
);
}
fn change_types(report: &DriftReport) -> Vec<&str> {
report
.changed_columns
.iter()
.map(|c| c.change_type.as_str())
.collect()
}
#[test]
fn test_drift_detects_length_change() {
let old = make_schema(vec![("users", vec![("name", DataType::VarChar, false)])]);
let mut current = old.clone();
current.tables["users"].columns["name"].max_length = Some(20);
let mut old = old;
old.tables["users"].columns["name"].max_length = Some(10);
let report = check_drift_detailed(&old, ¤t);
assert!(report.has_drift);
assert_eq!(change_types(&report), ["type_changed"]);
assert_eq!(
report.changed_columns[0].details,
"varchar(10) → varchar(20)"
);
}
#[test]
fn test_drift_detects_primary_key_change() {
let old = make_schema(vec![(
"memberships",
vec![
("user_id", DataType::Integer, false),
("team_id", DataType::Integer, false),
],
)]);
let mut current = old.clone();
let mut old = old;
old.tables["memberships"].primary_key = Some(PrimaryKey {
columns: vec!["user_id".into()],
name: None,
});
current.tables["memberships"].primary_key = Some(PrimaryKey {
columns: vec!["user_id".into(), "team_id".into()],
name: None,
});
let report = check_drift_detailed(&old, ¤t);
assert!(report.has_drift);
assert_eq!(change_types(&report), ["pk_changed"]);
assert_eq!(
report.changed_columns[0].details,
"PRIMARY KEY (user_id) → (user_id,team_id)"
);
}
#[test]
fn test_drift_detects_enum_label_change() {
let mut old = make_schema(vec![("orders", vec![("status", DataType::Text, false)])]);
let mut current = old.clone();
old.tables["orders"].columns["status"].enum_values =
Some(vec!["open".into(), "closed".into()]);
current.tables["orders"].columns["status"].enum_values =
Some(vec!["open".into(), "closed".into(), "archived".into()]);
old.enums.insert("order_status".into(), vec!["open".into()]);
current.enums.insert(
"order_status".into(),
vec!["open".into(), "archived".into()],
);
let report = check_drift_detailed(&old, ¤t);
assert!(report.has_drift);
assert_eq!(change_types(&report), ["enum_changed", "enum_changed"]);
assert_eq!(report.changed_columns[0].table, "orders");
assert_eq!(report.changed_columns[1].table, "");
assert!(report.changed_columns[1].details.contains("order_status"));
}
#[test]
fn test_drift_detects_identity_change() {
let mut old = make_schema(vec![("users", vec![("id", DataType::Integer, false)])]);
let mut current = old.clone();
old.tables["users"].columns["id"].is_auto_increment = false;
current.tables["users"].columns["id"].is_auto_increment = true;
current.tables["users"].columns["id"].has_default = true;
let report = check_drift_detailed(&old, ¤t);
assert_eq!(
change_types(&report),
["identity_changed", "default_changed"]
);
}
#[test]
fn test_hash_mismatch_is_always_reported_as_drift() {
let mut old = make_schema(vec![("users", vec![("email", DataType::Text, false)])]);
old.tables["users"]
.unique_constraints
.push(UniqueConstraint {
name: Some("users_email_key".into()),
columns: vec!["email".into()],
});
let mut current = old.clone();
current.tables["users"].unique_constraints[0].name = Some("users_email_idx".into());
let old_hash = compute_schema_hash(&old);
assert_ne!(old_hash, compute_schema_hash(¤t));
let report = check_drift(&old, &old_hash, ¤t);
assert!(report.has_drift);
assert_eq!(change_types(&report), ["schema_changed"]);
assert!(report.summary().contains("schema_changed"));
}
}