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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
use serde::{Deserialize, Serialize};
use version_migrate::{migrate_path, IntoDomain, MigratesTo, Migrator, Versioned};

// Simple test versions for Vec notation
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
struct V1 {
    id: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
struct V2 {
    id: String,
    name: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
struct V3 {
    id: String,
    name: String,
    age: i32,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
struct V4 {
    id: String,
    name: String,
    age: i32,
    email: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
struct V5 {
    id: String,
    name: String,
    age: i32,
    email: String,
    active: bool,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
struct V6 {
    id: String,
    name: String,
    age: i32,
    email: String,
    active: bool,
    tags: Vec<String>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
struct V7 {
    id: String,
    name: String,
    age: i32,
    email: String,
    active: bool,
    tags: Vec<String>,
    score: f64,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
struct V8 {
    id: String,
    name: String,
    age: i32,
    email: String,
    active: bool,
    tags: Vec<String>,
    score: f64,
    level: i32,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
struct V9 {
    id: String,
    name: String,
    age: i32,
    email: String,
    active: bool,
    tags: Vec<String>,
    score: f64,
    level: i32,
    created_at: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
struct V10 {
    id: String,
    name: String,
    age: i32,
    email: String,
    active: bool,
    tags: Vec<String>,
    score: f64,
    level: i32,
    created_at: String,
    updated_at: String,
}

// Implement Versioned for all versions
impl Versioned for V1 {
    const VERSION: &'static str = "1.0.0";
}
impl Versioned for V2 {
    const VERSION: &'static str = "2.0.0";
}
impl Versioned for V3 {
    const VERSION: &'static str = "3.0.0";
}
impl Versioned for V4 {
    const VERSION: &'static str = "4.0.0";
}
impl Versioned for V5 {
    const VERSION: &'static str = "5.0.0";
}
impl Versioned for V6 {
    const VERSION: &'static str = "6.0.0";
}
impl Versioned for V7 {
    const VERSION: &'static str = "7.0.0";
}
impl Versioned for V8 {
    const VERSION: &'static str = "8.0.0";
}
impl Versioned for V9 {
    const VERSION: &'static str = "9.0.0";
}
impl Versioned for V10 {
    const VERSION: &'static str = "10.0.0";
}

// Implement migration chain
impl MigratesTo<V2> for V1 {
    fn migrate(self) -> V2 {
        V2 {
            id: self.id,
            name: "Unknown".to_string(),
        }
    }
}

impl MigratesTo<V3> for V2 {
    fn migrate(self) -> V3 {
        V3 {
            id: self.id,
            name: self.name,
            age: 0,
        }
    }
}

impl MigratesTo<V4> for V3 {
    fn migrate(self) -> V4 {
        V4 {
            id: self.id,
            name: self.name,
            age: self.age,
            email: "unknown@example.com".to_string(),
        }
    }
}

impl MigratesTo<V5> for V4 {
    fn migrate(self) -> V5 {
        V5 {
            id: self.id,
            name: self.name,
            age: self.age,
            email: self.email,
            active: true,
        }
    }
}

impl MigratesTo<V6> for V5 {
    fn migrate(self) -> V6 {
        V6 {
            id: self.id,
            name: self.name,
            age: self.age,
            email: self.email,
            active: self.active,
            tags: vec![],
        }
    }
}

impl MigratesTo<V7> for V6 {
    fn migrate(self) -> V7 {
        V7 {
            id: self.id,
            name: self.name,
            age: self.age,
            email: self.email,
            active: self.active,
            tags: self.tags,
            score: 0.0,
        }
    }
}

impl MigratesTo<V8> for V7 {
    fn migrate(self) -> V8 {
        V8 {
            id: self.id,
            name: self.name,
            age: self.age,
            email: self.email,
            active: self.active,
            tags: self.tags,
            score: self.score,
            level: 1,
        }
    }
}

impl MigratesTo<V9> for V8 {
    fn migrate(self) -> V9 {
        V9 {
            id: self.id,
            name: self.name,
            age: self.age,
            email: self.email,
            active: self.active,
            tags: self.tags,
            score: self.score,
            level: self.level,
            created_at: "2023-01-01T00:00:00Z".to_string(),
        }
    }
}

impl MigratesTo<V10> for V9 {
    fn migrate(self) -> V10 {
        V10 {
            id: self.id,
            name: self.name,
            age: self.age,
            email: self.email,
            active: self.active,
            tags: self.tags,
            score: self.score,
            level: self.level,
            created_at: self.created_at,
            updated_at: "2023-01-01T00:00:00Z".to_string(),
        }
    }
}

// Domain entity
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct UserEntity {
    id: String,
    name: String,
    age: i32,
    email: String,
    active: bool,
    tags: Vec<String>,
    score: f64,
    level: i32,
    created_at: String,
    updated_at: String,
}

// Implement IntoDomain for migration chain
impl IntoDomain<V2> for V1 {
    fn into_domain(self) -> V2 {
        self.migrate()
    }
}

impl IntoDomain<V2> for V2 {
    fn into_domain(self) -> V2 {
        self
    }
}

impl IntoDomain<V3> for V2 {
    fn into_domain(self) -> V3 {
        self.migrate()
    }
}

impl IntoDomain<V3> for V3 {
    fn into_domain(self) -> V3 {
        self
    }
}

impl IntoDomain<V4> for V3 {
    fn into_domain(self) -> V4 {
        self.migrate()
    }
}

impl IntoDomain<V5> for V4 {
    fn into_domain(self) -> V5 {
        self.migrate()
    }
}

impl IntoDomain<V6> for V5 {
    fn into_domain(self) -> V6 {
        self.migrate()
    }
}

impl IntoDomain<V6> for V6 {
    fn into_domain(self) -> V6 {
        self
    }
}

impl IntoDomain<V7> for V6 {
    fn into_domain(self) -> V7 {
        self.migrate()
    }
}

impl IntoDomain<V7> for V7 {
    fn into_domain(self) -> V7 {
        self
    }
}

impl IntoDomain<V8> for V7 {
    fn into_domain(self) -> V8 {
        self.migrate()
    }
}

impl IntoDomain<V8> for V8 {
    fn into_domain(self) -> V8 {
        self
    }
}

impl IntoDomain<V9> for V8 {
    fn into_domain(self) -> V9 {
        self.migrate()
    }
}

impl IntoDomain<V9> for V9 {
    fn into_domain(self) -> V9 {
        self
    }
}

impl IntoDomain<V10> for V9 {
    fn into_domain(self) -> V10 {
        self.migrate()
    }
}

impl IntoDomain<V10> for V10 {
    fn into_domain(self) -> V10 {
        self
    }
}

impl IntoDomain<UserEntity> for V9 {
    fn into_domain(self) -> UserEntity {
        // V9 -> V10 -> UserEntity
        self.migrate().into_domain()
    }
}

impl IntoDomain<UserEntity> for V10 {
    fn into_domain(self) -> UserEntity {
        UserEntity {
            id: self.id,
            name: self.name,
            age: self.age,
            email: self.email,
            active: self.active,
            tags: self.tags,
            score: self.score,
            level: self.level,
            created_at: self.created_at,
            updated_at: self.updated_at,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_vec_notation_2_versions() {
        let path = migrate_path!("test", [V1, V2]);
        let mut migrator = Migrator::new();
        migrator.register(path).unwrap();

        let original = V1 {
            id: "1".to_string(),
        };
        let json = migrator.save(original).unwrap();
        let result: V2 = migrator.load("test", &json).unwrap();
        assert_eq!(result.id, "1");
        assert_eq!(result.name, "Unknown");
    }

    #[test]
    fn test_vec_notation_6_versions() {
        let path = migrate_path!("test", [V1, V2, V3, V4, V5, V6]);
        let mut migrator = Migrator::new();
        migrator.register(path).unwrap();

        let original = V1 {
            id: "1".to_string(),
        };
        let json = migrator.save(original).unwrap();
        let result: V6 = migrator.load("test", &json).unwrap();
        assert_eq!(result.id, "1");
        assert_eq!(result.name, "Unknown");
        assert_eq!(result.tags, Vec::<String>::new());
    }

    #[test]
    fn test_vec_notation_7_versions() {
        let path = migrate_path!("test", [V1, V2, V3, V4, V5, V6, V7]);
        let mut migrator = Migrator::new();
        migrator.register(path).unwrap();

        let original = V1 {
            id: "1".to_string(),
        };
        let json = migrator.save(original).unwrap();
        let result: V7 = migrator.load("test", &json).unwrap();
        assert_eq!(result.id, "1");
        assert_eq!(result.score, 0.0);
    }

    #[test]
    fn test_vec_notation_8_versions() {
        let path = migrate_path!("test", [V1, V2, V3, V4, V5, V6, V7, V8]);
        let mut migrator = Migrator::new();
        migrator.register(path).unwrap();

        let original = V1 {
            id: "1".to_string(),
        };
        let json = migrator.save(original).unwrap();
        let result: V8 = migrator.load("test", &json).unwrap();
        assert_eq!(result.id, "1");
        assert_eq!(result.level, 1);
    }

    #[test]
    fn test_vec_notation_9_versions() {
        let path = migrate_path!("test", [V1, V2, V3, V4, V5, V6, V7, V8, V9]);
        let mut migrator = Migrator::new();
        migrator.register(path).unwrap();

        let original = V1 {
            id: "1".to_string(),
        };
        let json = migrator.save(original).unwrap();
        let result: V9 = migrator.load("test", &json).unwrap();
        assert_eq!(result.id, "1");
        assert_eq!(result.created_at, "2023-01-01T00:00:00Z");
    }

    #[test]
    fn test_vec_notation_10_versions_to_domain() {
        let path = migrate_path!("test", [V1, V2, V3, V4, V5, V6, V7, V8, V9, UserEntity]);
        let mut migrator = Migrator::new();
        migrator.register(path).unwrap();

        let original = V1 {
            id: "1".to_string(),
        };
        let json = migrator.save(original).unwrap();
        let result: UserEntity = migrator.load("test", &json).unwrap();
        assert_eq!(result.id, "1");
        assert_eq!(result.name, "Unknown");
        assert_eq!(result.updated_at, "2023-01-01T00:00:00Z");
    }

    #[test]
    fn test_vec_notation_with_default_keys() {
        // Test vec notation with default version/data keys
        let path = migrate_path!("test", [V1, V2, V3, V4, V5]);
        let mut migrator = Migrator::new();
        migrator.register(path).unwrap();

        let original = V1 {
            id: "custom-1".to_string(),
        };
        let json = migrator.save(original).unwrap();

        // Check that default keys are used
        assert!(json.contains("\"version\""));
        assert!(json.contains("\"data\""));

        let result: V5 = migrator.load("test", &json).unwrap();
        assert_eq!(result.id, "custom-1");
        assert!(result.active);
    }

    #[test]
    fn test_compile_time_vec_syntax() {
        // These should all compile successfully
        let _path1 = migrate_path!("two", [V1, V2]);
        let _path2 = migrate_path!("three", [V1, V2, V3]);
        let _path3 = migrate_path!("four", [V1, V2, V3, V4]);
        let _path4 = migrate_path!("five", [V1, V2, V3, V4, V5]);
        let _path5 = migrate_path!("six", [V1, V2, V3, V4, V5, V6]);
        let _path6 = migrate_path!("seven", [V1, V2, V3, V4, V5, V6, V7]);
        let _path7 = migrate_path!("eight", [V1, V2, V3, V4, V5, V6, V7, V8]);
        let _path8 = migrate_path!("nine", [V1, V2, V3, V4, V5, V6, V7, V8, V9]);
        let _path9 = migrate_path!("ten", [V1, V2, V3, V4, V5, V6, V7, V8, V9, V10]);

        // With custom keys
        let _path_custom = migrate_path!(
            "custom",
            [V1, V2, V3, V4, V5],
            version_key = "version",
            data_key = "data"
        );
    }

    #[test]
    fn test_middle_version_start() {
        let path = migrate_path!("test", [V1, V2, V3, V4, V5, V6]);
        let mut migrator = Migrator::new();
        migrator.register(path).unwrap();

        // Start from middle version
        let original = V3 {
            id: "mid-1".to_string(),
            name: "Middle User".to_string(),
            age: 25,
        };
        let json = migrator.save(original).unwrap();

        let result: V6 = migrator.load("test", &json).unwrap();
        assert_eq!(result.id, "mid-1");
        assert_eq!(result.name, "Middle User");
        assert_eq!(result.age, 25);
        assert_eq!(result.email, "unknown@example.com"); // Added in V4
        assert!(result.active); // Added in V5
        assert_eq!(result.tags, Vec::<String>::new()); // Added in V6
    }
}