1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//! Domain diff logic for schema migrations
use crate::catalog::domain::Domain;
use crate::diff::operations::{DomainOperation, MigrationStep};
/// Build the CREATE DOMAIN definition string
fn build_domain_definition(domain: &Domain) -> String {
let mut parts = vec![format!("AS {}", domain.base_type)];
if let Some(default) = &domain.default {
parts.push(format!("DEFAULT {}", default));
}
if domain.not_null {
parts.push("NOT NULL".to_string());
}
if let Some(collation) = &domain.collation {
parts.push(format!("COLLATE \"{}\"", collation));
}
for constraint in &domain.check_constraints {
// pg_get_constraintdef returns the full CHECK clause
parts.push(format!(
"CONSTRAINT {} {}",
constraint.name, constraint.expression
));
}
parts.join(" ")
}
/// Diff a single domain
pub fn diff(old: Option<&Domain>, new: Option<&Domain>) -> Vec<MigrationStep> {
match (old, new) {
// CREATE new domain
(None, Some(n)) => {
vec![MigrationStep::Domain(DomainOperation::Create {
schema: n.schema.clone(),
name: n.name.clone(),
definition: build_domain_definition(n),
})]
}
// DROP removed domain
(Some(o), None) => {
vec![MigrationStep::Domain(DomainOperation::Drop {
schema: o.schema.clone(),
name: o.name.clone(),
})]
}
// ALTER existing domain
(Some(o), Some(n)) => {
let mut steps = Vec::new();
// Check if base type or collation changed - requires drop/recreate
if o.base_type != n.base_type || o.collation != n.collation {
// Drop and recreate
steps.push(MigrationStep::Domain(DomainOperation::Drop {
schema: o.schema.clone(),
name: o.name.clone(),
}));
steps.push(MigrationStep::Domain(DomainOperation::Create {
schema: n.schema.clone(),
name: n.name.clone(),
definition: build_domain_definition(n),
}));
return steps;
}
// Handle NOT NULL changes
if o.not_null != n.not_null {
if n.not_null {
steps.push(MigrationStep::Domain(DomainOperation::AlterSetNotNull {
schema: n.schema.clone(),
name: n.name.clone(),
}));
} else {
steps.push(MigrationStep::Domain(DomainOperation::AlterDropNotNull {
schema: n.schema.clone(),
name: n.name.clone(),
}));
}
}
// Handle DEFAULT changes
match (&o.default, &n.default) {
(None, Some(new_default)) => {
steps.push(MigrationStep::Domain(DomainOperation::AlterSetDefault {
schema: n.schema.clone(),
name: n.name.clone(),
default: new_default.clone(),
}));
}
(Some(_), None) => {
steps.push(MigrationStep::Domain(DomainOperation::AlterDropDefault {
schema: n.schema.clone(),
name: n.name.clone(),
}));
}
(Some(old_default), Some(new_default)) if old_default != new_default => {
steps.push(MigrationStep::Domain(DomainOperation::AlterSetDefault {
schema: n.schema.clone(),
name: n.name.clone(),
default: new_default.clone(),
}));
}
_ => {}
}
// Handle CHECK constraint changes
// Build maps of constraints by name for comparison
let old_constraints: std::collections::HashMap<&str, &str> = o
.check_constraints
.iter()
.map(|c| (c.name.as_str(), c.expression.as_str()))
.collect();
let new_constraints: std::collections::HashMap<&str, &str> = n
.check_constraints
.iter()
.map(|c| (c.name.as_str(), c.expression.as_str()))
.collect();
// Drop constraints that no longer exist or have changed expression
for (name, old_expr) in &old_constraints {
match new_constraints.get(name) {
None => {
// Constraint was removed
steps.push(MigrationStep::Domain(DomainOperation::DropConstraint {
schema: n.schema.clone(),
name: n.name.clone(),
constraint_name: name.to_string(),
}));
}
Some(new_expr) if old_expr != new_expr => {
// Constraint expression changed - drop and re-add
steps.push(MigrationStep::Domain(DomainOperation::DropConstraint {
schema: n.schema.clone(),
name: n.name.clone(),
constraint_name: name.to_string(),
}));
}
_ => {}
}
}
// Add new constraints or re-add changed constraints
for constraint in &n.check_constraints {
let name = constraint.name.as_str();
match old_constraints.get(name) {
None => {
// New constraint
steps.push(MigrationStep::Domain(DomainOperation::AddConstraint {
schema: n.schema.clone(),
name: n.name.clone(),
constraint_name: constraint.name.clone(),
expression: constraint.expression.clone(),
}));
}
Some(old_expr) if *old_expr != constraint.expression.as_str() => {
// Changed constraint - re-add after drop
steps.push(MigrationStep::Domain(DomainOperation::AddConstraint {
schema: n.schema.clone(),
name: n.name.clone(),
constraint_name: constraint.name.clone(),
expression: constraint.expression.clone(),
}));
}
_ => {}
}
}
steps
}
(None, None) => Vec::new(),
}
}