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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//! Schema evolution and compatibility checking.
use super::{MessageSchema, SchemaField};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
/// Schema compatibility mode
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum CompatibilityMode {
/// No compatibility checking
None,
/// New schema can read data written by old schema (backward compatible)
#[default]
Backward,
/// Old schema can read data written by new schema (forward compatible)
Forward,
/// Both backward and forward compatible
Full,
/// No schema changes allowed
Strict,
}
/// Schema evolution operation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SchemaEvolution {
/// Field added (name, type, optional/required, default_value)
FieldAdded {
name: String,
field_type: String,
required: bool,
default_value: Option<serde_json::Value>,
},
/// Field removed
FieldRemoved { name: String },
/// Field made optional (was required)
FieldMadeOptional { name: String },
/// Field made required (was optional) - requires default value
FieldMadeRequired {
name: String,
default_value: serde_json::Value,
},
/// Field type changed
FieldTypeChanged {
name: String,
old_type: String,
new_type: String,
},
/// Field renamed
FieldRenamed { old_name: String, new_name: String },
}
/// Schema compatibility check result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompatibilityCheck {
/// Whether schemas are compatible
pub compatible: bool,
/// List of incompatibilities found
pub incompatibilities: Vec<String>,
/// List of evolutions detected
pub evolutions: Vec<SchemaEvolution>,
/// Warnings (non-breaking but notable changes)
pub warnings: Vec<String>,
}
impl CompatibilityCheck {
/// Check if two schemas are compatible
pub fn check(old: &MessageSchema, new: &MessageSchema, mode: CompatibilityMode) -> Self {
let mut incompatibilities = Vec::new();
let mut evolutions = Vec::new();
let mut warnings = Vec::new();
// Build field maps for easier comparison
let old_fields: HashMap<String, &SchemaField> = old.fields.iter().map(|f| (f.name.clone(), f)).collect();
let new_fields: HashMap<String, &SchemaField> = new.fields.iter().map(|f| (f.name.clone(), f)).collect();
match mode {
CompatibilityMode::None => {
// No checking required
return Self {
compatible: true,
incompatibilities,
evolutions,
warnings,
};
}
CompatibilityMode::Strict => {
// No changes allowed
if old.fields.len() != new.fields.len() {
incompatibilities.push("Field count changed".to_string());
}
for (name, old_field) in &old_fields {
if let Some(new_field) = new_fields.get(name) {
if old_field.field_type != new_field.field_type {
incompatibilities.push(format!("Field '{}' type changed", name));
}
if old_field.required != new_field.required {
incompatibilities.push(format!("Field '{}' required status changed", name));
}
} else {
incompatibilities.push(format!("Field '{}' removed", name));
}
}
for name in new_fields.keys() {
if !old_fields.contains_key(name) {
incompatibilities.push(format!("Field '{}' added", name));
}
}
}
CompatibilityMode::Backward => {
// New schema can read old data
// - Can add optional fields
// - Can make required fields optional
// - Cannot remove required fields
// - Cannot add required fields
// - Cannot change field types
for (name, old_field) in &old_fields {
if let Some(new_field) = new_fields.get(name) {
// Field exists in both
if old_field.field_type != new_field.field_type {
incompatibilities.push(format!(
"Field '{}' type changed from {} to {}",
name, old_field.field_type, new_field.field_type
));
evolutions.push(SchemaEvolution::FieldTypeChanged {
name: name.clone(),
old_type: old_field.field_type.clone(),
new_type: new_field.field_type.clone(),
});
}
if old_field.required && !new_field.required {
// Field made optional - OK for backward compatibility
evolutions.push(SchemaEvolution::FieldMadeOptional { name: name.clone() });
} else if !old_field.required && new_field.required {
// Field made required - breaks backward compatibility
incompatibilities.push(format!("Field '{}' made required (was optional)", name));
evolutions.push(SchemaEvolution::FieldMadeRequired {
name: name.clone(),
default_value: serde_json::Value::Null,
});
}
} else {
// Field removed
if old_field.required {
incompatibilities.push(format!("Required field '{}' removed", name));
} else {
warnings.push(format!("Optional field '{}' removed", name));
}
evolutions.push(SchemaEvolution::FieldRemoved { name: name.clone() });
}
}
// Check for added fields
for (name, new_field) in &new_fields {
if !old_fields.contains_key(name) {
if new_field.required {
incompatibilities.push(format!("Required field '{}' added", name));
} else {
warnings.push(format!("Optional field '{}' added", name));
}
evolutions.push(SchemaEvolution::FieldAdded {
name: name.clone(),
field_type: new_field.field_type.clone(),
required: new_field.required,
default_value: None,
});
}
}
}
CompatibilityMode::Forward => {
// Old schema can read new data
// - Can remove optional fields
// - Can make optional fields required
// - Cannot add required fields
// - Cannot remove required fields
// - Cannot change field types
for (name, old_field) in &old_fields {
if let Some(new_field) = new_fields.get(name) {
if old_field.field_type != new_field.field_type {
incompatibilities.push(format!("Field '{}' type changed", name));
evolutions.push(SchemaEvolution::FieldTypeChanged {
name: name.clone(),
old_type: old_field.field_type.clone(),
new_type: new_field.field_type.clone(),
});
}
} else if old_field.required {
incompatibilities.push(format!("Required field '{}' removed", name));
evolutions.push(SchemaEvolution::FieldRemoved { name: name.clone() });
}
}
for (name, new_field) in &new_fields {
if !old_fields.contains_key(name) && new_field.required {
incompatibilities.push(format!("Required field '{}' added", name));
evolutions.push(SchemaEvolution::FieldAdded {
name: name.clone(),
field_type: new_field.field_type.clone(),
required: new_field.required,
default_value: None,
});
}
}
}
CompatibilityMode::Full => {
// Both backward and forward compatible
// - Can only add/remove optional fields
// - Cannot change field types
// - Cannot change required status
for (name, old_field) in &old_fields {
if let Some(new_field) = new_fields.get(name) {
if old_field.field_type != new_field.field_type {
incompatibilities.push(format!("Field '{}' type changed", name));
}
if old_field.required != new_field.required {
incompatibilities.push(format!("Field '{}' required status changed", name));
}
} else if old_field.required {
incompatibilities.push(format!("Required field '{}' removed", name));
}
}
for (name, new_field) in &new_fields {
if !old_fields.contains_key(name) && new_field.required {
incompatibilities.push(format!("Required field '{}' added", name));
}
}
}
}
Self {
compatible: incompatibilities.is_empty(),
incompatibilities,
evolutions,
warnings,
}
}
}