-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUnionMigrationData.hs
More file actions
86 lines (69 loc) · 2.16 KB
/
UnionMigrationData.hs
File metadata and controls
86 lines (69 loc) · 2.16 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
{-# LANGUAGE QuasiQuotes #-}
-- | Data for union alternative migration tests
--
-- This module tests the 'alternative changed' changelog feature, which allows
-- swapping the type of a union alternative from one type to a completely
-- different type, with a custom migration function to transform the data.
module Data.API.Test.UnionMigrationData
( -- * Type swap scenario (PersonV1 -> PersonV2)
startTypeSwapSchema
, endTypeSwapSchema
, typeSwapChangelog
) where
import Data.API.Changes
import Data.API.Parse
import Data.API.Types
-- -----------------------------------------------------------------------------
-- Type Swap Scenario
--
-- This tests the primary use case: migrating a union alternative from one
-- type (PersonV1) to a completely different type (PersonV2).
--
-- PersonV1 has: name :: string
-- PersonV2 has: fullName :: string, age :: integer
--
-- The migration function transforms PersonV1 data to PersonV2 data.
-- -----------------------------------------------------------------------------
-- | Initial schema with PersonV1
startTypeSwapSchema :: API
startTypeSwapSchema = [api|
personV1Prefix :: PersonV1
= record
name :: string
containerPrefix :: Container
= record
person :: MyUnion
myUnionPrefix :: MyUnion
= union
| person :: PersonV1
| other :: integer
|]
-- | Final schema with PersonV2 and changelog
endTypeSwapSchema :: API
typeSwapChangelog :: APIChangelog
(endTypeSwapSchema, typeSwapChangelog) = [apiWithChangelog|
personV1Prefix :: PersonV1
= record
name :: string
personV2Prefix :: PersonV2
= record
fullName :: string
age :: integer
containerPrefix :: Container
= record
person :: MyUnion
myUnionPrefix :: MyUnion
= union
| person :: PersonV2
| other :: integer
changes
version "1.0"
// Note: changes are processed bottom-up, so we must list the union change
// before adding the new type it references
changed union MyUnion
alternative changed person :: PersonV2 migration MigratePersonV1ToV2
added PersonV2 record
fullName :: string
age :: integer
version "0"
|]