-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmigration_roundtrip.rs
More file actions
833 lines (764 loc) · 22.8 KB
/
Copy pathmigration_roundtrip.rs
File metadata and controls
833 lines (764 loc) · 22.8 KB
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
//! Round-trip behavioral coverage for `migration_chain!` at 1- through
//! 5-step chain arities, plus error paths. This is the regression pin the
//! proc-macro implementation must reproduce unchanged as chain length grows.
//!
//! Lives under `tests/` (a separate crate) rather than `#[cfg(test)] mod
//! tests` inside `src/migratable.rs`: the macro emits absolute
//! `::aimdb_data_contracts::...` paths (matching the `RecordKey` /
//! `aimdb_core` precedent in `aimdb-derive`), which only resolve from
//! outside the defining crate.
#![cfg(feature = "migratable")]
use aimdb_data_contracts::{
migration_chain, MigrationChain, MigrationError, MigrationStep, SchemaType,
};
use serde::{Deserialize, Serialize};
// ═══════════════════════════════════════════════════════════════════
// 1-step chain: Widget v1 -> v2
// ═══════════════════════════════════════════════════════════════════
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct WidgetV1 {
#[serde(default = "widget_v1")]
schema_version: u32,
count: u32,
}
fn widget_v1() -> u32 {
1
}
impl SchemaType for WidgetV1 {
const NAME: &'static str = "widget";
const VERSION: u32 = 1;
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct WidgetV2 {
#[serde(default = "widget_v2")]
schema_version: u32,
count: u64,
}
fn widget_v2() -> u32 {
2
}
impl SchemaType for WidgetV2 {
const NAME: &'static str = "widget";
const VERSION: u32 = 2;
}
struct WidgetV1ToV2;
impl MigrationStep for WidgetV1ToV2 {
type Older = WidgetV1;
type Newer = WidgetV2;
const FROM_VERSION: u32 = 1;
const TO_VERSION: u32 = 2;
fn up(v1: WidgetV1) -> Result<WidgetV2, MigrationError> {
Ok(WidgetV2 {
schema_version: 2,
count: u64::from(v1.count),
})
}
fn down(v2: WidgetV2) -> Result<WidgetV1, MigrationError> {
Ok(WidgetV1 {
schema_version: 1,
count: v2.count as u32,
})
}
}
migration_chain! {
type Current = WidgetV2;
version_field = "schema_version";
steps {
WidgetV1ToV2: WidgetV1 => WidgetV2,
}
}
#[test]
fn widget_upgrades_from_v1() {
let v1 = WidgetV1 {
schema_version: 1,
count: 7,
};
let bytes = serde_json::to_vec(&v1).unwrap();
let current = WidgetV2::migrate_from_bytes(&bytes).unwrap();
assert_eq!(
current,
WidgetV2 {
schema_version: 2,
count: 7
}
);
}
#[test]
fn widget_parses_current_version_directly() {
let v2 = WidgetV2 {
schema_version: 2,
count: 42,
};
let bytes = serde_json::to_vec(&v2).unwrap();
let current = WidgetV2::migrate_from_bytes(&bytes).unwrap();
assert_eq!(current, v2);
}
#[test]
fn widget_downgrades_to_v1() {
let v2 = WidgetV2 {
schema_version: 2,
count: 9,
};
let bytes = v2.migrate_to_version(1).unwrap();
let v1: WidgetV1 = serde_json::from_slice(&bytes).unwrap();
assert_eq!(
v1,
WidgetV1 {
schema_version: 1,
count: 9
}
);
}
#[test]
fn widget_serializes_current_version_directly() {
let v2 = WidgetV2 {
schema_version: 2,
count: 5,
};
let bytes = v2.migrate_to_version(2).unwrap();
let round_tripped: WidgetV2 = serde_json::from_slice(&bytes).unwrap();
assert_eq!(round_tripped, v2);
}
// ═══════════════════════════════════════════════════════════════════
// 2-step chain: Gadget v1 -> v2 -> v3
// ═══════════════════════════════════════════════════════════════════
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct GadgetV1 {
#[serde(default = "gadget_v1")]
schema_version: u32,
watts: u32,
}
fn gadget_v1() -> u32 {
1
}
impl SchemaType for GadgetV1 {
const NAME: &'static str = "gadget";
const VERSION: u32 = 1;
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct GadgetV2 {
#[serde(default = "gadget_v2")]
schema_version: u32,
milliwatts: u32,
}
fn gadget_v2() -> u32 {
2
}
impl SchemaType for GadgetV2 {
const NAME: &'static str = "gadget";
const VERSION: u32 = 2;
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct GadgetV3 {
#[serde(default = "gadget_v3")]
schema_version: u32,
milliwatts: u64,
}
fn gadget_v3() -> u32 {
3
}
impl SchemaType for GadgetV3 {
const NAME: &'static str = "gadget";
const VERSION: u32 = 3;
}
struct GadgetV1ToV2;
impl MigrationStep for GadgetV1ToV2 {
type Older = GadgetV1;
type Newer = GadgetV2;
const FROM_VERSION: u32 = 1;
const TO_VERSION: u32 = 2;
fn up(v1: GadgetV1) -> Result<GadgetV2, MigrationError> {
Ok(GadgetV2 {
schema_version: 2,
milliwatts: v1.watts * 1000,
})
}
fn down(v2: GadgetV2) -> Result<GadgetV1, MigrationError> {
Ok(GadgetV1 {
schema_version: 1,
watts: v2.milliwatts / 1000,
})
}
}
struct GadgetV2ToV3;
impl MigrationStep for GadgetV2ToV3 {
type Older = GadgetV2;
type Newer = GadgetV3;
const FROM_VERSION: u32 = 2;
const TO_VERSION: u32 = 3;
fn up(v2: GadgetV2) -> Result<GadgetV3, MigrationError> {
Ok(GadgetV3 {
schema_version: 3,
milliwatts: u64::from(v2.milliwatts),
})
}
fn down(v3: GadgetV3) -> Result<GadgetV2, MigrationError> {
Ok(GadgetV2 {
schema_version: 2,
milliwatts: v3.milliwatts as u32,
})
}
}
migration_chain! {
type Current = GadgetV3;
version_field = "schema_version";
steps {
GadgetV1ToV2: GadgetV1 => GadgetV2,
GadgetV2ToV3: GadgetV2 => GadgetV3,
}
}
#[test]
fn gadget_upgrades_from_v1() {
let v1 = GadgetV1 {
schema_version: 1,
watts: 2,
};
let bytes = serde_json::to_vec(&v1).unwrap();
let current = GadgetV3::migrate_from_bytes(&bytes).unwrap();
assert_eq!(
current,
GadgetV3 {
schema_version: 3,
milliwatts: 2000
}
);
}
#[test]
fn gadget_upgrades_from_v2() {
let v2 = GadgetV2 {
schema_version: 2,
milliwatts: 1500,
};
let bytes = serde_json::to_vec(&v2).unwrap();
let current = GadgetV3::migrate_from_bytes(&bytes).unwrap();
assert_eq!(
current,
GadgetV3 {
schema_version: 3,
milliwatts: 1500
}
);
}
#[test]
fn gadget_downgrades_to_v1() {
let v3 = GadgetV3 {
schema_version: 3,
milliwatts: 4000,
};
let bytes = v3.migrate_to_version(1).unwrap();
let v1: GadgetV1 = serde_json::from_slice(&bytes).unwrap();
assert_eq!(
v1,
GadgetV1 {
schema_version: 1,
watts: 4
}
);
}
#[test]
fn gadget_downgrades_to_v2() {
let v3 = GadgetV3 {
schema_version: 3,
milliwatts: 4000,
};
let bytes = v3.migrate_to_version(2).unwrap();
let v2: GadgetV2 = serde_json::from_slice(&bytes).unwrap();
assert_eq!(
v2,
GadgetV2 {
schema_version: 2,
milliwatts: 4000
}
);
}
// ═══════════════════════════════════════════════════════════════════
// 3-step chain: Gizmo v1 -> v2 -> v3 -> v4
// ═══════════════════════════════════════════════════════════════════
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct GizmoV1 {
#[serde(default = "gizmo_v1")]
schema_version: u32,
label: String,
}
fn gizmo_v1() -> u32 {
1
}
impl SchemaType for GizmoV1 {
const NAME: &'static str = "gizmo";
const VERSION: u32 = 1;
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct GizmoV2 {
#[serde(default = "gizmo_v2")]
schema_version: u32,
label: String,
revision: u32,
}
fn gizmo_v2() -> u32 {
2
}
impl SchemaType for GizmoV2 {
const NAME: &'static str = "gizmo";
const VERSION: u32 = 2;
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct GizmoV3 {
#[serde(default = "gizmo_v3")]
schema_version: u32,
label: String,
revision: u32,
active: bool,
}
fn gizmo_v3() -> u32 {
3
}
impl SchemaType for GizmoV3 {
const NAME: &'static str = "gizmo";
const VERSION: u32 = 3;
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct GizmoV4 {
#[serde(default = "gizmo_v4")]
schema_version: u32,
label: String,
revision: u64,
active: bool,
}
fn gizmo_v4() -> u32 {
4
}
impl SchemaType for GizmoV4 {
const NAME: &'static str = "gizmo";
const VERSION: u32 = 4;
}
struct GizmoV1ToV2;
impl MigrationStep for GizmoV1ToV2 {
type Older = GizmoV1;
type Newer = GizmoV2;
const FROM_VERSION: u32 = 1;
const TO_VERSION: u32 = 2;
fn up(v1: GizmoV1) -> Result<GizmoV2, MigrationError> {
Ok(GizmoV2 {
schema_version: 2,
label: v1.label,
revision: 0,
})
}
fn down(v2: GizmoV2) -> Result<GizmoV1, MigrationError> {
Ok(GizmoV1 {
schema_version: 1,
label: v2.label,
})
}
}
struct GizmoV2ToV3;
impl MigrationStep for GizmoV2ToV3 {
type Older = GizmoV2;
type Newer = GizmoV3;
const FROM_VERSION: u32 = 2;
const TO_VERSION: u32 = 3;
fn up(v2: GizmoV2) -> Result<GizmoV3, MigrationError> {
Ok(GizmoV3 {
schema_version: 3,
label: v2.label,
revision: v2.revision,
active: true,
})
}
fn down(v3: GizmoV3) -> Result<GizmoV2, MigrationError> {
Ok(GizmoV2 {
schema_version: 2,
label: v3.label,
revision: v3.revision,
})
}
}
struct GizmoV3ToV4;
impl MigrationStep for GizmoV3ToV4 {
type Older = GizmoV3;
type Newer = GizmoV4;
const FROM_VERSION: u32 = 3;
const TO_VERSION: u32 = 4;
fn up(v3: GizmoV3) -> Result<GizmoV4, MigrationError> {
Ok(GizmoV4 {
schema_version: 4,
label: v3.label,
revision: u64::from(v3.revision),
active: v3.active,
})
}
fn down(v4: GizmoV4) -> Result<GizmoV3, MigrationError> {
Ok(GizmoV3 {
schema_version: 3,
label: v4.label,
revision: v4.revision as u32,
active: v4.active,
})
}
}
migration_chain! {
type Current = GizmoV4;
version_field = "schema_version";
steps {
GizmoV1ToV2: GizmoV1 => GizmoV2,
GizmoV2ToV3: GizmoV2 => GizmoV3,
GizmoV3ToV4: GizmoV3 => GizmoV4,
}
}
#[test]
fn gizmo_upgrades_from_every_historical_version() {
let v1 = GizmoV1 {
schema_version: 1,
label: "a".into(),
};
let from_v1 = GizmoV4::migrate_from_bytes(&serde_json::to_vec(&v1).unwrap()).unwrap();
assert_eq!(
from_v1,
GizmoV4 {
schema_version: 4,
label: "a".into(),
revision: 0,
active: true
}
);
let v2 = GizmoV2 {
schema_version: 2,
label: "b".into(),
revision: 5,
};
let from_v2 = GizmoV4::migrate_from_bytes(&serde_json::to_vec(&v2).unwrap()).unwrap();
assert_eq!(
from_v2,
GizmoV4 {
schema_version: 4,
label: "b".into(),
revision: 5,
active: true
}
);
let v3 = GizmoV3 {
schema_version: 3,
label: "c".into(),
revision: 9,
active: false,
};
let from_v3 = GizmoV4::migrate_from_bytes(&serde_json::to_vec(&v3).unwrap()).unwrap();
assert_eq!(
from_v3,
GizmoV4 {
schema_version: 4,
label: "c".into(),
revision: 9,
active: false
}
);
let v4 = GizmoV4 {
schema_version: 4,
label: "d".into(),
revision: 12,
active: true,
};
let from_v4 = GizmoV4::migrate_from_bytes(&serde_json::to_vec(&v4).unwrap()).unwrap();
assert_eq!(from_v4, v4);
}
#[test]
fn gizmo_downgrades_to_every_target_version() {
let current = GizmoV4 {
schema_version: 4,
label: "z".into(),
revision: 3,
active: true,
};
let to_v1: GizmoV1 = serde_json::from_slice(¤t.migrate_to_version(1).unwrap()).unwrap();
assert_eq!(
to_v1,
GizmoV1 {
schema_version: 1,
label: "z".into()
}
);
let to_v2: GizmoV2 = serde_json::from_slice(¤t.migrate_to_version(2).unwrap()).unwrap();
assert_eq!(
to_v2,
GizmoV2 {
schema_version: 2,
label: "z".into(),
revision: 3
}
);
let to_v3: GizmoV3 = serde_json::from_slice(¤t.migrate_to_version(3).unwrap()).unwrap();
assert_eq!(
to_v3,
GizmoV3 {
schema_version: 3,
label: "z".into(),
revision: 3,
active: true
}
);
let to_v4: GizmoV4 = serde_json::from_slice(¤t.migrate_to_version(4).unwrap()).unwrap();
assert_eq!(to_v4, current);
}
// ═══════════════════════════════════════════════════════════════════
// Error paths (checked once, against the richest — 3-step — chain)
// ═══════════════════════════════════════════════════════════════════
#[test]
fn error_on_malformed_payload() {
let err = GizmoV4::migrate_from_bytes(b"not json").unwrap_err();
assert_eq!(err, MigrationError::DeserializationFailed("invalid JSON"));
}
#[test]
fn error_on_missing_version_field() {
let err = GizmoV4::migrate_from_bytes(br#"{"label": "no version"}"#).unwrap_err();
assert_eq!(err, MigrationError::MissingVersion);
}
#[test]
fn error_on_version_too_new() {
let payload = br#"{"schema_version": 99, "label": "future"}"#;
let err = GizmoV4::migrate_from_bytes(payload).unwrap_err();
assert_eq!(
err,
MigrationError::VersionTooNew {
source: 99,
current: 4
}
);
}
#[test]
fn error_on_source_version_too_old() {
// A payload version below MIN_VERSION (1) is too old, not too new — it
// must report VersionTooOld, mirroring the migrate_to_version path.
let payload = br#"{"schema_version": 0, "label": "ancient"}"#;
let err = GizmoV4::migrate_from_bytes(payload).unwrap_err();
assert_eq!(
err,
MigrationError::VersionTooOld {
target: 0,
minimum: 1
}
);
}
#[test]
fn error_on_target_version_too_old() {
let current = GizmoV4 {
schema_version: 4,
label: "z".into(),
revision: 3,
active: true,
};
let err = current.migrate_to_version(0).unwrap_err();
assert_eq!(
err,
MigrationError::VersionTooOld {
target: 0,
minimum: 1
}
);
}
#[test]
fn error_on_target_version_too_new() {
let current = GizmoV4 {
schema_version: 4,
label: "z".into(),
revision: 3,
active: true,
};
let err = current.migrate_to_version(5).unwrap_err();
assert_eq!(
err,
MigrationError::VersionTooNew {
source: 5,
current: 4
}
);
}
// ═══════════════════════════════════════════════════════════════════
// 4-step chain: Sprocket v1 -> v2 -> v3 -> v4 -> v5
// (proves migration_chain! arity is unbounded)
// ═══════════════════════════════════════════════════════════════════
macro_rules! sprocket_version {
($name:ident, $version:literal) => {
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct $name {
#[serde(default = "sprocket_default_version")]
schema_version: u32,
teeth: u32,
}
impl SchemaType for $name {
const NAME: &'static str = "sprocket";
const VERSION: u32 = $version;
}
};
}
fn sprocket_default_version() -> u32 {
0
}
sprocket_version!(SprocketV1, 1);
sprocket_version!(SprocketV2, 2);
sprocket_version!(SprocketV3, 3);
sprocket_version!(SprocketV4, 4);
sprocket_version!(SprocketV5, 5);
macro_rules! sprocket_step {
($step:ident, $older:ident, $newer:ident, $to_version:literal) => {
struct $step;
impl MigrationStep for $step {
type Older = $older;
type Newer = $newer;
const FROM_VERSION: u32 = $to_version - 1;
const TO_VERSION: u32 = $to_version;
fn up(v: $older) -> Result<$newer, MigrationError> {
Ok($newer {
schema_version: $to_version,
teeth: v.teeth,
})
}
fn down(v: $newer) -> Result<$older, MigrationError> {
Ok($older {
schema_version: $to_version - 1,
teeth: v.teeth,
})
}
}
};
}
sprocket_step!(SprocketV1ToV2, SprocketV1, SprocketV2, 2);
sprocket_step!(SprocketV2ToV3, SprocketV2, SprocketV3, 3);
sprocket_step!(SprocketV3ToV4, SprocketV3, SprocketV4, 4);
sprocket_step!(SprocketV4ToV5, SprocketV4, SprocketV5, 5);
migration_chain! {
type Current = SprocketV5;
version_field = "schema_version";
steps {
SprocketV1ToV2: SprocketV1 => SprocketV2,
SprocketV2ToV3: SprocketV2 => SprocketV3,
SprocketV3ToV4: SprocketV3 => SprocketV4,
SprocketV4ToV5: SprocketV4 => SprocketV5,
}
}
#[test]
fn sprocket_upgrades_from_every_historical_version() {
for (version, teeth) in [(1u32, 10u32), (2, 11), (3, 12), (4, 13), (5, 14)] {
let bytes = serde_json::to_vec(&serde_json::json!({
"schema_version": version,
"teeth": teeth,
}))
.unwrap();
let current = SprocketV5::migrate_from_bytes(&bytes).unwrap();
assert_eq!(
current,
SprocketV5 {
schema_version: 5,
teeth
}
);
}
}
#[test]
fn sprocket_downgrades_to_every_target_version() {
let current = SprocketV5 {
schema_version: 5,
teeth: 20,
};
for target in 1u32..=5 {
let bytes = current.migrate_to_version(target).unwrap();
let value: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
assert_eq!(value["schema_version"], target);
assert_eq!(value["teeth"], 20);
}
}
// ═══════════════════════════════════════════════════════════════════
// 5-step chain: Doohickey v1 -> v2 -> v3 -> v4 -> v5 -> v6
// ═══════════════════════════════════════════════════════════════════
macro_rules! doohickey_version {
($name:ident, $version:literal) => {
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct $name {
#[serde(default = "doohickey_default_version")]
schema_version: u32,
size: u32,
}
impl SchemaType for $name {
const NAME: &'static str = "doohickey";
const VERSION: u32 = $version;
}
};
}
fn doohickey_default_version() -> u32 {
0
}
doohickey_version!(DoohickeyV1, 1);
doohickey_version!(DoohickeyV2, 2);
doohickey_version!(DoohickeyV3, 3);
doohickey_version!(DoohickeyV4, 4);
doohickey_version!(DoohickeyV5, 5);
doohickey_version!(DoohickeyV6, 6);
macro_rules! doohickey_step {
($step:ident, $older:ident, $newer:ident, $to_version:literal) => {
struct $step;
impl MigrationStep for $step {
type Older = $older;
type Newer = $newer;
const FROM_VERSION: u32 = $to_version - 1;
const TO_VERSION: u32 = $to_version;
fn up(v: $older) -> Result<$newer, MigrationError> {
Ok($newer {
schema_version: $to_version,
size: v.size,
})
}
fn down(v: $newer) -> Result<$older, MigrationError> {
Ok($older {
schema_version: $to_version - 1,
size: v.size,
})
}
}
};
}
doohickey_step!(DoohickeyV1ToV2, DoohickeyV1, DoohickeyV2, 2);
doohickey_step!(DoohickeyV2ToV3, DoohickeyV2, DoohickeyV3, 3);
doohickey_step!(DoohickeyV3ToV4, DoohickeyV3, DoohickeyV4, 4);
doohickey_step!(DoohickeyV4ToV5, DoohickeyV4, DoohickeyV5, 5);
doohickey_step!(DoohickeyV5ToV6, DoohickeyV5, DoohickeyV6, 6);
migration_chain! {
type Current = DoohickeyV6;
version_field = "schema_version";
steps {
DoohickeyV1ToV2: DoohickeyV1 => DoohickeyV2,
DoohickeyV2ToV3: DoohickeyV2 => DoohickeyV3,
DoohickeyV3ToV4: DoohickeyV3 => DoohickeyV4,
DoohickeyV4ToV5: DoohickeyV4 => DoohickeyV5,
DoohickeyV5ToV6: DoohickeyV5 => DoohickeyV6,
}
}
#[test]
fn doohickey_upgrades_from_every_historical_version() {
for (version, size) in [(1u32, 1u32), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)] {
let bytes = serde_json::to_vec(&serde_json::json!({
"schema_version": version,
"size": size,
}))
.unwrap();
let current = DoohickeyV6::migrate_from_bytes(&bytes).unwrap();
assert_eq!(
current,
DoohickeyV6 {
schema_version: 6,
size
}
);
}
}
#[test]
fn doohickey_downgrades_to_every_target_version() {
let current = DoohickeyV6 {
schema_version: 6,
size: 99,
};
for target in 1u32..=6 {
let bytes = current.migrate_to_version(target).unwrap();
let value: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
assert_eq!(value["schema_version"], target);
assert_eq!(value["size"], 99);
}
}