version-migrate 0.20.0

Explicit, type-safe schema versioning and migration for Rust
Documentation
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use serde::{Deserialize, Serialize};
use version_migrate::{IntoDomain, MigratesTo, Migrator, Versioned, VersionedWrapper};

// Version 1.0.0 of Task
#[derive(Serialize, Deserialize, Versioned)]
#[versioned(version = "1.0.0")]
struct TaskV1_0_0 {
    id: String,
    title: String,
}

// Version 1.1.0 of Task (added description field)
#[derive(Serialize, Deserialize, Versioned)]
#[versioned(version = "1.1.0")]
struct TaskV1_1_0 {
    id: String,
    title: String,
    description: Option<String>,
}

// Domain model (clean, version-agnostic)
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct TaskEntity {
    id: String,
    title: String,
    description: Option<String>,
}

// Migration from V1.0.0 to V1.1.0
impl MigratesTo<TaskV1_1_0> for TaskV1_0_0 {
    fn migrate(self) -> TaskV1_1_0 {
        TaskV1_1_0 {
            id: self.id,
            title: self.title,
            description: None, // Default value for new field
        }
    }
}

// Conversion from latest version to domain model
impl IntoDomain<TaskEntity> for TaskV1_1_0 {
    fn into_domain(self) -> TaskEntity {
        TaskEntity {
            id: self.id,
            title: self.title,
            description: self.description,
        }
    }
}

#[test]
fn test_migration_from_v1_0_0_to_domain() {
    // Create old version data
    let old_task = TaskV1_0_0 {
        id: "task-1".to_string(),
        title: "Test Task".to_string(),
    };

    // Wrap it with version info
    let wrapped = VersionedWrapper::from_versioned(old_task);
    let json = serde_json::to_string(&wrapped).expect("Failed to serialize");

    // Setup migrator
    let task_path = Migrator::define("task")
        .from::<TaskV1_0_0>()
        .step::<TaskV1_1_0>()
        .into::<TaskEntity>();

    let mut migrator = Migrator::new();
    migrator.register(task_path).unwrap();

    // Load and migrate
    let task: TaskEntity = migrator.load("task", &json).expect("Migration failed");

    // Verify the result
    assert_eq!(task.id, "task-1");
    assert_eq!(task.title, "Test Task");
    assert_eq!(task.description, None);
}

#[test]
fn test_load_latest_version() {
    // Create latest version data
    let latest_task = TaskV1_1_0 {
        id: "task-2".to_string(),
        title: "Latest Task".to_string(),
        description: Some("This is a description".to_string()),
    };

    // Wrap it with version info
    let wrapped = VersionedWrapper::from_versioned(latest_task);
    let json = serde_json::to_string(&wrapped).expect("Failed to serialize");

    // Setup migrator (no migration steps needed, just conversion to domain)
    let task_path = Migrator::define("task")
        .from::<TaskV1_1_0>()
        .into::<TaskEntity>();

    let mut migrator = Migrator::new();
    migrator.register(task_path).unwrap();

    // Load
    let task: TaskEntity = migrator.load("task", &json).expect("Load failed");

    // Verify the result
    assert_eq!(task.id, "task-2");
    assert_eq!(task.title, "Latest Task");
    assert_eq!(task.description, Some("This is a description".to_string()));
}

#[test]
fn test_versioned_trait() {
    assert_eq!(TaskV1_0_0::VERSION, "1.0.0");
    assert_eq!(TaskV1_1_0::VERSION, "1.1.0");
}

#[test]
fn test_versioned_wrapper() {
    let task = TaskV1_0_0 {
        id: "test".to_string(),
        title: "Test".to_string(),
    };

    let wrapper = VersionedWrapper::from_versioned(task);
    assert_eq!(wrapper.version, "1.0.0");
    assert_eq!(wrapper.data.id, "test");
}

#[test]
fn test_save_and_load_with_migrator() {
    let migrator = Migrator::new();

    // Save V1.0.0 data
    let task_v1 = TaskV1_0_0 {
        id: "task-save".to_string(),
        title: "Saved Task".to_string(),
    };

    let json = migrator.save(task_v1).expect("Save failed");

    // Verify JSON format
    assert!(json.contains("\"version\":\"1.0.0\""));
    assert!(json.contains("\"task-save\""));
    assert!(json.contains("\"Saved Task\""));

    // Setup migration path
    let task_path = Migrator::define("task")
        .from::<TaskV1_0_0>()
        .step::<TaskV1_1_0>()
        .into::<TaskEntity>();

    let mut migrator = Migrator::new();
    migrator.register(task_path).unwrap();

    // Load and migrate the saved data
    let loaded: TaskEntity = migrator.load("task", &json).expect("Load failed");

    assert_eq!(loaded.id, "task-save");
    assert_eq!(loaded.title, "Saved Task");
    assert_eq!(loaded.description, None); // Default from migration
}

#[test]
fn test_save_latest_and_load() {
    let migrator = Migrator::new();

    // Save V1.1.0 data (latest version)
    let task_v1_1 = TaskV1_1_0 {
        id: "task-latest".to_string(),
        title: "Latest Task".to_string(),
        description: Some("With description".to_string()),
    };

    let json = migrator.save(task_v1_1).expect("Save failed");

    // Setup migration path for latest version
    let task_path = Migrator::define("task")
        .from::<TaskV1_1_0>()
        .into::<TaskEntity>();

    let mut migrator = Migrator::new();
    migrator.register(task_path).unwrap();

    // Load without migration needed
    let loaded: TaskEntity = migrator.load("task", &json).expect("Load failed");

    assert_eq!(loaded.id, "task-latest");
    assert_eq!(loaded.title, "Latest Task");
    assert_eq!(loaded.description, Some("With description".to_string()));
}

#[test]
fn test_roundtrip_preserves_data() {
    let migrator = Migrator::new();

    // Original data
    let original = TaskV1_1_0 {
        id: "roundtrip-1".to_string(),
        title: "Roundtrip Test".to_string(),
        description: Some("Testing roundtrip".to_string()),
    };

    // Save
    let json = migrator.save(original).expect("Save failed");

    // Load back
    let wrapper: VersionedWrapper<TaskV1_1_0> = serde_json::from_str(&json).expect("Parse failed");

    // Verify all fields preserved
    assert_eq!(wrapper.version, "1.1.0");
    assert_eq!(wrapper.data.id, "roundtrip-1");
    assert_eq!(wrapper.data.title, "Roundtrip Test");
    assert_eq!(
        wrapper.data.description,
        Some("Testing roundtrip".to_string())
    );
}

#[test]
fn test_load_from_toml() {
    // Create TOML representation of versioned data
    let toml_str = r#"
version = "1.0.0"

[data]
id = "task-toml"
title = "Task from TOML"
"#;

    // Parse TOML
    let toml_value: toml::Value = toml::from_str(toml_str).expect("Failed to parse TOML");

    // Setup migrator
    let task_path = Migrator::define("task")
        .from::<TaskV1_0_0>()
        .step::<TaskV1_1_0>()
        .into::<TaskEntity>();

    let mut migrator = Migrator::new();
    migrator.register(task_path).unwrap();

    // Load from TOML using load_from
    let task: TaskEntity = migrator
        .load_from("task", toml_value)
        .expect("Failed to load from TOML");

    // Verify the result
    assert_eq!(task.id, "task-toml");
    assert_eq!(task.title, "Task from TOML");
    assert_eq!(task.description, None); // Default from migration
}

#[test]
fn test_load_from_toml_latest_version() {
    // Create TOML representation with latest version
    let toml_str = r#"
version = "1.1.0"

[data]
id = "task-toml-latest"
title = "Latest Task from TOML"
description = "TOML description"
"#;

    // Parse TOML
    let toml_value: toml::Value = toml::from_str(toml_str).expect("Failed to parse TOML");

    // Setup migrator
    let task_path = Migrator::define("task")
        .from::<TaskV1_1_0>()
        .into::<TaskEntity>();

    let mut migrator = Migrator::new();
    migrator.register(task_path).unwrap();

    // Load from TOML
    let task: TaskEntity = migrator
        .load_from("task", toml_value)
        .expect("Failed to load from TOML");

    // Verify the result
    assert_eq!(task.id, "task-toml-latest");
    assert_eq!(task.title, "Latest Task from TOML");
    assert_eq!(task.description, Some("TOML description".to_string()));
}

#[test]
fn test_load_from_yaml() {
    // Create YAML representation of versioned data
    let yaml_str = r#"
version: "1.0.0"
data:
  id: "task-yaml"
  title: "Task from YAML"
"#;

    // Parse YAML
    let yaml_value: serde_yaml::Value =
        serde_yaml::from_str(yaml_str).expect("Failed to parse YAML");

    // Setup migrator
    let task_path = Migrator::define("task")
        .from::<TaskV1_0_0>()
        .step::<TaskV1_1_0>()
        .into::<TaskEntity>();

    let mut migrator = Migrator::new();
    migrator.register(task_path).unwrap();

    // Load from YAML using load_from
    let task: TaskEntity = migrator
        .load_from("task", yaml_value)
        .expect("Failed to load from YAML");

    // Verify the result
    assert_eq!(task.id, "task-yaml");
    assert_eq!(task.title, "Task from YAML");
    assert_eq!(task.description, None); // Default from migration
}

#[test]
fn test_load_from_yaml_latest_version() {
    // Create YAML representation with latest version
    let yaml_str = r#"
version: "1.1.0"
data:
  id: "task-yaml-latest"
  title: "Latest Task from YAML"
  description: "YAML description"
"#;

    // Parse YAML
    let yaml_value: serde_yaml::Value =
        serde_yaml::from_str(yaml_str).expect("Failed to parse YAML");

    // Setup migrator
    let task_path = Migrator::define("task")
        .from::<TaskV1_1_0>()
        .into::<TaskEntity>();

    let mut migrator = Migrator::new();
    migrator.register(task_path).unwrap();

    // Load from YAML
    let task: TaskEntity = migrator
        .load_from("task", yaml_value)
        .expect("Failed to load from YAML");

    // Verify the result
    assert_eq!(task.id, "task-yaml-latest");
    assert_eq!(task.title, "Latest Task from YAML");
    assert_eq!(task.description, Some("YAML description".to_string()));
}

#[test]
fn test_load_from_multi_format_consistency() {
    // Setup migrator once
    let task_path = Migrator::define("task")
        .from::<TaskV1_0_0>()
        .step::<TaskV1_1_0>()
        .into::<TaskEntity>();

    let mut migrator = Migrator::new();
    migrator.register(task_path).unwrap();

    // Same data in different formats
    let json_str =
        r#"{"version":"1.0.0","data":{"id":"multi-format","title":"Multi Format Test"}}"#;

    let toml_str = r#"
version = "1.0.0"
[data]
id = "multi-format"
title = "Multi Format Test"
"#;

    let yaml_str = r#"
version: "1.0.0"
data:
  id: "multi-format"
  title: "Multi Format Test"
"#;

    // Load from JSON
    let from_json: TaskEntity = migrator.load("task", json_str).expect("JSON load failed");

    // Load from TOML
    let toml_value: toml::Value = toml::from_str(toml_str).expect("TOML parse failed");
    let from_toml: TaskEntity = migrator
        .load_from("task", toml_value)
        .expect("TOML load failed");

    // Load from YAML
    let yaml_value: serde_yaml::Value = serde_yaml::from_str(yaml_str).expect("YAML parse failed");
    let from_yaml: TaskEntity = migrator
        .load_from("task", yaml_value)
        .expect("YAML load failed");

    // All should produce the same result
    assert_eq!(from_json, from_toml);
    assert_eq!(from_json, from_yaml);
    assert_eq!(from_json.id, "multi-format");
    assert_eq!(from_json.title, "Multi Format Test");
}