feat(schema-diff): professional DDL diff view with deployment-aware colors and custom themes (#1112)

Co-authored-by: Sam <14344444@@qq.com>
This commit is contained in:
polempandSam authored and GitHub committed 2026-06-12 17:14:05 +08:00
1 parent 2554ed95be
commit ef5ed05ae7
41 files changed
+5695 -1215

No files matched your search

+517 -33
View File
@@ -3,7 +3,9 @@ use std::collections::{HashMap, HashSet};
use serde::{Deserialize, Serialize};
use crate::models::connection::DatabaseType;
use crate::types::{ColumnInfo, ForeignKeyInfo, IndexInfo, TableInfo, TriggerInfo};
use crate::types::{
ColumnInfo, ForeignKeyInfo, FunctionInfo, IndexInfo, OwnerInfo, RuleInfo, SequenceInfo, TableInfo, TriggerInfo,
};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
@@ -61,6 +63,62 @@ pub struct TriggerDiff {
pub changes: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FunctionDiff {
#[serde(rename = "type")]
pub diff_type: String,
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub source: Option<FunctionInfo>,
#[serde(skip_serializing_if = "Option::is_none")]
pub target: Option<FunctionInfo>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub changes: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SequenceDiff {
#[serde(rename = "type")]
pub diff_type: String,
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub source: Option<SequenceInfo>,
#[serde(skip_serializing_if = "Option::is_none")]
pub target: Option<SequenceInfo>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub changes: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RuleDiff {
#[serde(rename = "type")]
pub diff_type: String,
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub source: Option<RuleInfo>,
#[serde(skip_serializing_if = "Option::is_none")]
pub target: Option<RuleInfo>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub changes: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OwnerDiff {
#[serde(rename = "type")]
pub diff_type: String,
pub object_name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub source: Option<OwnerInfo>,
#[serde(skip_serializing_if = "Option::is_none")]
pub target: Option<OwnerInfo>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub changes: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TableDiff {
@@ -80,6 +138,8 @@ pub struct TableDiff {
#[serde(skip_serializing_if = "Option::is_none")]
pub ddl: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub target_ddl: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub source_table_comment: Option<Option<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub target_table_comment: Option<Option<String>>,
@@ -112,24 +172,63 @@ pub struct SchemaDiffPreparationOptions {
pub source_details: Vec<TableSchemaDetail>,
#[serde(default)]
pub target_details: Vec<TableSchemaDetail>,
#[serde(default)]
pub source_functions: Vec<FunctionInfo>,
#[serde(default)]
pub target_functions: Vec<FunctionInfo>,
#[serde(default)]
pub source_sequences: Vec<SequenceInfo>,
#[serde(default)]
pub target_sequences: Vec<SequenceInfo>,
#[serde(default)]
pub source_rules: Vec<RuleInfo>,
#[serde(default)]
pub target_rules: Vec<RuleInfo>,
#[serde(default)]
pub source_owners: Vec<OwnerInfo>,
#[serde(default)]
pub target_owners: Vec<OwnerInfo>,
pub database_type: DatabaseType,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub target_schema: Option<String>,
#[serde(default)]
pub ignore_comments: bool,
#[serde(default)]
pub cascade_delete: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SchemaDiffPreparation {
pub diffs: Vec<TableDiff>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub function_diffs: Vec<FunctionDiff>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub sequence_diffs: Vec<SequenceDiff>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub rule_diffs: Vec<RuleDiff>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub owner_diffs: Vec<OwnerDiff>,
pub sync_sql: String,
}
pub fn prepare_schema_diff(options: SchemaDiffPreparationOptions) -> SchemaDiffPreparation {
let diffs = diff_schema(&options);
let sync_sql = generate_schema_sync_sql(&diffs, options.database_type, options.target_schema.as_deref());
SchemaDiffPreparation { diffs, sync_sql }
let function_diffs = diff_functions(&options.source_functions, &options.target_functions);
let sequence_diffs = diff_sequences(&options.source_sequences, &options.target_sequences);
let rule_diffs = diff_rules(&options.source_rules, &options.target_rules);
let owner_diffs = diff_owners(&options.source_owners, &options.target_owners);
let sync_sql = generate_schema_sync_sql(
&diffs,
&function_diffs,
&sequence_diffs,
&rule_diffs,
&owner_diffs,
options.database_type,
options.target_schema.as_deref(),
options.cascade_delete,
);
SchemaDiffPreparation { diffs, function_diffs, sequence_diffs, rule_diffs, owner_diffs, sync_sql }
}
fn diff_schema(options: &SchemaDiffPreparationOptions) -> Vec<TableDiff> {
@@ -176,6 +275,7 @@ fn diff_schema(options: &SchemaDiffPreparationOptions) -> Vec<TableDiff> {
diff_type: "added".to_string(),
object_type: Some("table".to_string()),
ddl: source_details.get(name.as_str()).and_then(|detail| detail.ddl.clone()),
target_ddl: None,
name,
columns: None,
indexes: None,
@@ -187,6 +287,7 @@ fn diff_schema(options: &SchemaDiffPreparationOptions) -> Vec<TableDiff> {
}
for name in removed {
let name_clone = name.clone();
result.push(TableDiff {
diff_type: "removed".to_string(),
object_type: Some("table".to_string()),
@@ -196,12 +297,14 @@ fn diff_schema(options: &SchemaDiffPreparationOptions) -> Vec<TableDiff> {
foreign_keys: None,
triggers: None,
ddl: None,
target_ddl: target_details.get(name_clone.as_str()).and_then(|detail| detail.ddl.clone()),
source_table_comment: None,
target_table_comment: None,
});
}
for name in added_views {
let name_clone = name.clone();
result.push(TableDiff {
diff_type: "added".to_string(),
object_type: Some("view".to_string()),
@@ -210,13 +313,15 @@ fn diff_schema(options: &SchemaDiffPreparationOptions) -> Vec<TableDiff> {
indexes: None,
foreign_keys: None,
triggers: None,
ddl: None,
ddl: source_details.get(name_clone.as_str()).and_then(|detail| detail.ddl.clone()),
target_ddl: None,
source_table_comment: None,
target_table_comment: None,
});
}
for name in removed_views {
let name_clone = name.clone();
result.push(TableDiff {
diff_type: "removed".to_string(),
object_type: Some("view".to_string()),
@@ -226,6 +331,7 @@ fn diff_schema(options: &SchemaDiffPreparationOptions) -> Vec<TableDiff> {
foreign_keys: None,
triggers: None,
ddl: None,
target_ddl: target_details.get(name_clone.as_str()).and_then(|detail| detail.ddl.clone()),
source_table_comment: None,
target_table_comment: None,
});
@@ -243,27 +349,29 @@ fn diff_schema(options: &SchemaDiffPreparationOptions) -> Vec<TableDiff> {
let comment_changed = !options.ignore_comments
&& source_comment.clone().unwrap_or_default() != target_comment.clone().unwrap_or_default();
if !column_diffs.is_empty()
let has_diff = !column_diffs.is_empty()
|| !index_diffs.is_empty()
|| !foreign_key_diffs.is_empty()
|| !trigger_diffs.is_empty()
|| comment_changed
{
result.push(TableDiff {
diff_type: "modified".to_string(),
object_type: Some("table".to_string()),
name,
columns: (!column_diffs.is_empty()).then_some(column_diffs),
indexes: (!index_diffs.is_empty()).then_some(index_diffs),
foreign_keys: (!foreign_key_diffs.is_empty()).then_some(foreign_key_diffs),
triggers: (!trigger_diffs.is_empty()).then_some(trigger_diffs),
ddl: None,
source_table_comment: comment_changed.then_some(source_comment),
target_table_comment: comment_changed.then_some(target_comment),
});
}
|| comment_changed;
let name_clone = name.clone();
result.push(TableDiff {
diff_type: if has_diff { "modified".to_string() } else { "none".to_string() },
object_type: Some("table".to_string()),
name,
columns: if has_diff { (!column_diffs.is_empty()).then_some(column_diffs) } else { None },
indexes: if has_diff { (!index_diffs.is_empty()).then_some(index_diffs) } else { None },
foreign_keys: if has_diff { (!foreign_key_diffs.is_empty()).then_some(foreign_key_diffs) } else { None },
triggers: if has_diff { (!trigger_diffs.is_empty()).then_some(trigger_diffs) } else { None },
ddl: source_details.get(name_clone.as_str()).and_then(|detail| detail.ddl.clone()),
target_ddl: target_details.get(name_clone.as_str()).and_then(|detail| detail.ddl.clone()),
source_table_comment: if has_diff { comment_changed.then_some(source_comment) } else { None },
target_table_comment: if has_diff { comment_changed.then_some(target_comment) } else { None },
});
}
result.retain(|diff| diff.diff_type != "none");
result
}
@@ -563,6 +671,220 @@ fn is_mysql_like(db_type: DatabaseType) -> bool {
)
}
/// Normalize a function definition for comparison by:
/// - Converting CRLF to LF
/// - Collapsing all whitespace (tabs, multiple spaces) to single spaces
/// - Trimming each line and rejoining
fn normalize_definition(def: &str) -> String {
def.replace("\r\n", "\n")
.split('\n')
.map(|line| line.split_whitespace().collect::<Vec<_>>().join(" "))
.collect::<Vec<_>>()
.join("\n")
}
pub fn diff_functions(source: &[FunctionInfo], target: &[FunctionInfo]) -> Vec<FunctionDiff> {
let mut diffs = Vec::new();
// Use (name, arguments) as key to support PostgreSQL function overloading
let target_map: HashMap<(&str, &str), &FunctionInfo> =
target.iter().map(|f| ((f.name.as_str(), f.arguments.as_str()), f)).collect();
let source_map: HashMap<(&str, &str), &FunctionInfo> =
source.iter().map(|f| ((f.name.as_str(), f.arguments.as_str()), f)).collect();
for source_fn in source {
let key = (source_fn.name.as_str(), source_fn.arguments.as_str());
let Some(target_fn) = target_map.get(&key) else {
diffs.push(FunctionDiff {
diff_type: "added".to_string(),
name: source_fn.name.clone(),
source: Some(source_fn.clone()),
target: None,
changes: Vec::new(),
});
continue;
};
let mut changes = Vec::new();
if source_fn.function_type != target_fn.function_type {
changes.push(format!("type: {} → {}", target_fn.function_type, source_fn.function_type));
}
if source_fn.data_type != target_fn.data_type {
changes.push(format!("return type: {} → {}", target_fn.data_type, source_fn.data_type));
}
if normalize_definition(&source_fn.definition) != normalize_definition(&target_fn.definition) {
changes.push("definition changed".to_string());
}
if !changes.is_empty() {
diffs.push(FunctionDiff {
diff_type: "modified".to_string(),
name: source_fn.name.clone(),
source: Some(source_fn.clone()),
target: Some((*target_fn).clone()),
changes,
});
}
}
for target_fn in target {
let key = (target_fn.name.as_str(), target_fn.arguments.as_str());
if !source_map.contains_key(&key) {
diffs.push(FunctionDiff {
diff_type: "removed".to_string(),
name: target_fn.name.clone(),
source: None,
target: Some(target_fn.clone()),
changes: Vec::new(),
});
}
}
diffs
}
pub fn diff_sequences(source: &[SequenceInfo], target: &[SequenceInfo]) -> Vec<SequenceDiff> {
let mut diffs = Vec::new();
let target_map: HashMap<&str, &SequenceInfo> = target.iter().map(|s| (s.name.as_str(), s)).collect();
let source_map: HashMap<&str, &SequenceInfo> = source.iter().map(|s| (s.name.as_str(), s)).collect();
for source_seq in source {
let Some(target_seq) = target_map.get(source_seq.name.as_str()) else {
diffs.push(SequenceDiff {
diff_type: "added".to_string(),
name: source_seq.name.clone(),
source: Some(source_seq.clone()),
target: None,
changes: Vec::new(),
});
continue;
};
let mut changes = Vec::new();
if source_seq.data_type != target_seq.data_type {
changes.push(format!("data_type: {} → {}", target_seq.data_type, source_seq.data_type));
}
if source_seq.start_value != target_seq.start_value {
changes.push(format!("start: {} → {}", target_seq.start_value, source_seq.start_value));
}
if source_seq.min_value != target_seq.min_value {
changes.push(format!("min: {} → {}", target_seq.min_value, source_seq.min_value));
}
if source_seq.max_value != target_seq.max_value {
changes.push(format!("max: {} → {}", target_seq.max_value, source_seq.max_value));
}
if source_seq.increment != target_seq.increment {
changes.push(format!("increment: {} → {}", target_seq.increment, source_seq.increment));
}
if source_seq.cycle != target_seq.cycle {
changes.push(format!("cycle: {} → {}", target_seq.cycle, source_seq.cycle));
}
// Only compare last_value when both sides successfully retrieved it.
// Avoid false positives when one side lacks permission (returns None).
if let (Some(s), Some(t)) = (&source_seq.last_value, &target_seq.last_value) {
if s != t {
changes.push(format!("last_value: {} → {}", t, s));
}
}
if !changes.is_empty() {
diffs.push(SequenceDiff {
diff_type: "modified".to_string(),
name: source_seq.name.clone(),
source: Some(source_seq.clone()),
target: Some((*target_seq).clone()),
changes,
});
}
}
for target_seq in target {
if !source_map.contains_key(target_seq.name.as_str()) {
diffs.push(SequenceDiff {
diff_type: "removed".to_string(),
name: target_seq.name.clone(),
source: None,
target: Some(target_seq.clone()),
changes: Vec::new(),
});
}
}
diffs
}
pub fn diff_rules(source: &[RuleInfo], target: &[RuleInfo]) -> Vec<RuleDiff> {
let mut diffs = Vec::new();
let target_map: HashMap<&str, &RuleInfo> = target.iter().map(|r| (r.name.as_str(), r)).collect();
let source_map: HashMap<&str, &RuleInfo> = source.iter().map(|r| (r.name.as_str(), r)).collect();
for source_rule in source {
let Some(target_rule) = target_map.get(source_rule.name.as_str()) else {
diffs.push(RuleDiff {
diff_type: "added".to_string(),
name: source_rule.name.clone(),
source: Some(source_rule.clone()),
target: None,
changes: Vec::new(),
});
continue;
};
let mut changes = Vec::new();
if source_rule.definition != target_rule.definition {
changes.push("definition changed".to_string());
}
if !changes.is_empty() {
diffs.push(RuleDiff {
diff_type: "modified".to_string(),
name: source_rule.name.clone(),
source: Some(source_rule.clone()),
target: Some((*target_rule).clone()),
changes,
});
}
}
for target_rule in target {
if !source_map.contains_key(target_rule.name.as_str()) {
diffs.push(RuleDiff {
diff_type: "removed".to_string(),
name: target_rule.name.clone(),
source: None,
target: Some(target_rule.clone()),
changes: Vec::new(),
});
}
}
diffs
}
pub fn diff_owners(source: &[OwnerInfo], target: &[OwnerInfo]) -> Vec<OwnerDiff> {
let mut diffs = Vec::new();
let target_map: HashMap<&str, &OwnerInfo> = target.iter().map(|o| (o.object_name.as_str(), o)).collect();
let _source_map: HashMap<&str, &OwnerInfo> = source.iter().map(|o| (o.object_name.as_str(), o)).collect();
for source_owner in source {
let Some(target_owner) = target_map.get(source_owner.object_name.as_str()) else {
continue; // skip added/removed objects, only compare owners for common objects
};
let mut changes = Vec::new();
if source_owner.owner != target_owner.owner {
changes.push(format!("owner: {} → {}", target_owner.owner, source_owner.owner));
}
if !changes.is_empty() {
diffs.push(OwnerDiff {
diff_type: "modified".to_string(),
object_name: source_owner.object_name.clone(),
source: Some(source_owner.clone()),
target: Some((*target_owner).clone()),
changes,
});
}
}
diffs
}
fn quote_id(name: &str, db_type: DatabaseType) -> String {
if is_mysql_like(db_type) {
format!("`{}`", name.replace('`', "``"))
@@ -673,9 +995,9 @@ fn add_foreign_key_sql(table_name: &str, fk: &ForeignKeyInfo, db_type: DatabaseT
)
}
fn drop_object_sql(diff: &TableDiff, db_type: DatabaseType, schema: Option<&str>) -> String {
fn drop_object_sql(diff: &TableDiff, db_type: DatabaseType, schema: Option<&str>, cascade: &str) -> String {
let object_type = if diff.object_type.as_deref() == Some("view") { "VIEW" } else { "TABLE" };
format!("DROP {object_type} IF EXISTS {};", qualified_name(&diff.name, db_type, schema))
format!("DROP {object_type} IF EXISTS {}{cascade};", qualified_name(&diff.name, db_type, schema))
}
fn comment_literal(comment: &str) -> String {
@@ -707,9 +1029,19 @@ fn table_comment_sql(table_name: &str, comment: &str, db_type: DatabaseType, sch
}
}
pub fn generate_schema_sync_sql(diffs: &[TableDiff], db_type: DatabaseType, schema: Option<&str>) -> String {
pub fn generate_schema_sync_sql(
diffs: &[TableDiff],
function_diffs: &[FunctionDiff],
sequence_diffs: &[SequenceDiff],
rule_diffs: &[RuleDiff],
owner_diffs: &[OwnerDiff],
db_type: DatabaseType,
schema: Option<&str>,
cascade_delete: bool,
) -> String {
let mut lines = Vec::new();
let is_mysql = is_mysql_like(db_type);
let cascade = if cascade_delete { " CASCADE" } else { "" };
for diff in diffs {
let table = qualified_name(&diff.name, db_type, schema);
@@ -730,7 +1062,7 @@ pub fn generate_schema_sync_sql(diffs: &[TableDiff], db_type: DatabaseType, sche
if diff.diff_type == "removed" {
lines.push(format!("-- Drop {}: {}", diff.object_type.as_deref().unwrap_or("table"), diff.name));
lines.push(drop_object_sql(diff, db_type, schema));
lines.push(drop_object_sql(diff, db_type, schema, cascade));
lines.push(String::new());
continue;
}
@@ -853,10 +1185,8 @@ pub fn generate_schema_sync_sql(diffs: &[TableDiff], db_type: DatabaseType, sche
if let Some(foreign_keys) = &diff.foreign_keys {
for fk in foreign_keys {
if fk.diff_type == "added" || fk.diff_type == "modified" {
if let Some(source) = fk.source.as_ref() {
lines.push(add_foreign_key_sql(&diff.name, source, db_type, schema));
}
if (fk.diff_type == "added" || fk.diff_type == "modified") && fk.source.is_some() {
lines.push(add_foreign_key_sql(&diff.name, fk.source.as_ref().unwrap(), db_type, schema));
}
}
}
@@ -885,6 +1215,148 @@ pub fn generate_schema_sync_sql(diffs: &[TableDiff], db_type: DatabaseType, sche
}
}
// Function diffs
if !function_diffs.is_empty() {
lines.push(String::new());
lines.push("-- Functions".to_string());
for diff in function_diffs {
match diff.diff_type.as_str() {
"added" => {
if let Some(source) = &diff.source {
lines.push(format!("-- Create function: {}", diff.name));
lines.push(format!(
"CREATE OR REPLACE FUNCTION {} {};",
qualified_name(&diff.name, db_type, schema),
source.definition
));
}
}
"removed" => {
lines.push(format!("-- Drop function: {}", diff.name));
lines.push(format!(
"DROP FUNCTION IF EXISTS {}{cascade};",
qualified_name(&diff.name, db_type, schema)
));
}
"modified" => {
if let Some(source) = &diff.source {
lines.push(format!("-- Alter function: {}", diff.name));
lines.push(format!(
"CREATE OR REPLACE FUNCTION {} {};",
qualified_name(&diff.name, db_type, schema),
source.definition
));
}
}
_ => {}
}
}
}
// Sequence diffs
if !sequence_diffs.is_empty() {
lines.push(String::new());
lines.push("-- Sequences".to_string());
for diff in sequence_diffs {
match diff.diff_type.as_str() {
"added" => {
if let Some(source) = &diff.source {
lines.push(format!("-- Create sequence: {}", diff.name));
lines.push(format!(
"CREATE SEQUENCE {} AS {} START WITH {} INCREMENT BY {} MINVALUE {} MAXVALUE {} {};",
qualified_name(&diff.name, db_type, schema),
source.data_type,
source.start_value,
source.increment,
source.min_value,
source.max_value,
if source.cycle { "CYCLE" } else { "NO CYCLE" }
));
}
}
"removed" => {
lines.push(format!("-- Drop sequence: {}", diff.name));
lines.push(format!("DROP SEQUENCE {}{cascade};", qualified_name(&diff.name, db_type, schema)));
}
"modified" => {
if let Some(source) = &diff.source {
lines.push(format!("-- Alter sequence: {}", diff.name));
lines.push(format!(
"ALTER SEQUENCE {} AS {} START WITH {} INCREMENT BY {} MINVALUE {} MAXVALUE {} {};",
qualified_name(&diff.name, db_type, schema),
source.data_type,
source.start_value,
source.increment,
source.min_value,
source.max_value,
if source.cycle { "CYCLE" } else { "NO CYCLE" }
));
}
}
_ => {}
}
}
}
// Rule diffs
if !rule_diffs.is_empty() {
lines.push(String::new());
lines.push("-- Rules".to_string());
for diff in rule_diffs {
match diff.diff_type.as_str() {
"added" => {
if let Some(source) = &diff.source {
lines.push(format!("-- Create rule: {}", diff.name));
lines.push(source.definition.clone());
}
}
"removed" => {
lines.push(format!("-- Drop rule: {}", diff.name));
if let Some(source) = &diff.source {
lines.push(format!(
"DROP RULE IF EXISTS {} ON {};",
diff.name,
qualified_name(&source.table_name, db_type, schema)
));
}
}
"modified" => {
if let Some(source) = &diff.source {
lines.push(format!("-- Alter rule: {}", diff.name));
lines.push(format!(
"DROP RULE IF EXISTS {} ON {};",
diff.name,
qualified_name(&source.table_name, db_type, schema)
));
lines.push(source.definition.clone());
}
}
_ => {}
}
}
}
// Owner diffs
if !owner_diffs.is_empty() {
lines.push(String::new());
lines.push("-- Owners".to_string());
for diff in owner_diffs {
if let (Some(source), Some(_target)) = (&diff.source, &diff.target) {
let object_type = match source.object_type.as_str() {
"TABLE" => "TABLE",
"VIEW" => "VIEW",
"SEQUENCE" => "SEQUENCE",
_ => "TABLE",
};
lines.push(format!(
"ALTER {object_type} {} OWNER TO {};",
qualified_name(&diff.object_name, db_type, schema),
source.owner
));
}
}
}
lines.join("\n").trim().to_string()
}
@@ -912,8 +1384,8 @@ mod tests {
ref_schema: overrides.ref_schema,
ref_table: if overrides.ref_table.is_empty() { "users".to_string() } else { overrides.ref_table },
ref_column: if overrides.ref_column.is_empty() { "id".to_string() } else { overrides.ref_column },
on_update: None,
on_delete: None,
on_update: overrides.on_update,
on_delete: overrides.on_delete,
}
}
@@ -1058,12 +1530,13 @@ mod tests {
}]),
triggers: None,
ddl: None,
target_ddl: None,
source_table_comment: None,
target_table_comment: None,
}];
assert_eq!(
generate_schema_sync_sql(&diffs, DatabaseType::Postgres, None),
generate_schema_sync_sql(&diffs, &[], &[], &[], &[], DatabaseType::Postgres, None, false),
[
"ALTER TABLE \"orders\" DROP CONSTRAINT \"orders_user_id_fk\";",
"DROP INDEX IF EXISTS \"idx_orders_status\";",
@@ -1091,12 +1564,13 @@ mod tests {
foreign_keys: None,
triggers: None,
ddl: None,
target_ddl: None,
source_table_comment: Some(Some("用户表".to_string())),
target_table_comment: Some(Some("Users".to_string())),
}];
assert_eq!(
generate_schema_sync_sql(&diffs, DatabaseType::Mysql, None),
generate_schema_sync_sql(&diffs, &[], &[], &[], &[], DatabaseType::Mysql, None, false),
[
"-- Alter table: users",
"ALTER TABLE `users`",
@@ -1141,9 +1615,18 @@ mod tests {
triggers: Vec::new(),
ddl: None,
}],
source_functions: Vec::new(),
target_functions: Vec::new(),
source_sequences: Vec::new(),
target_sequences: Vec::new(),
source_rules: Vec::new(),
target_rules: Vec::new(),
source_owners: Vec::new(),
target_owners: Vec::new(),
database_type: DatabaseType::Mysql,
target_schema: None,
ignore_comments: true,
cascade_delete: false,
};
let result = prepare_schema_diff(options);
@@ -1194,12 +1677,13 @@ mod tests {
foreign_keys: None,
triggers: None,
ddl: None,
target_ddl: None,
source_table_comment: None,
target_table_comment: None,
}];
assert_eq!(
generate_schema_sync_sql(&diffs, DatabaseType::Postgres, Some("sales")),
generate_schema_sync_sql(&diffs, &[], &[], &[], &[], DatabaseType::Postgres, Some("sales"), false),
[
"-- Alter table: orders",
"ALTER TABLE \"sales\".\"orders\" ADD COLUMN \"status\" text;",