Skip to content

Commit 5116003

Browse files
authored
Merge pull request #15 from lamdera/fix-phantom-types-migration
Ignore phantom types when doing migration code gen
2 parents 2be7e74 + 88479e4 commit 5116003

File tree

8 files changed

+82
-9
lines changed

8 files changed

+82
-9
lines changed

extra/Lamdera/Evergreen/MigrationGenerator.hs

+8-3
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ migrateUnionDefinition_ author pkg oldUnion newUnion tvarMapOld tvarMapNew oldVe
218218

219219
tvarPairs :: [(Can.Type, Can.Type)]
220220
tvarPairs =
221-
zip (loadTvars tvarsOld tvarMapOld) (loadTvars tvarsNew tvarMapNew)
221+
zip (loadTvars (fmap fst paramMigrationPairs) tvarMapOld) (loadTvars (fmap snd paramMigrationPairs) tvarMapNew)
222222

223223
tvarMigrations :: [Migration]
224224
tvarMigrations = migrateTvars oldVersion newVersion scope interfaces recursionSet tvarMapOld tvarMapNew tvarPairs
@@ -254,12 +254,17 @@ migrateUnionDefinition_ author pkg oldUnion newUnion tvarMapOld tvarMapNew oldVe
254254
migration :: Text
255255
migration = migrationName <> " " <> tvarMigrationTextsCombined
256256

257-
paramMigrationPairs = zip tvarsOld tvarsNew
257+
paramMigrationPairs :: [(N.Name, N.Name)]
258+
paramMigrationPairs = filter (\(_, newTVar) -> isTVarInUseUnion newTVar) (zip tvarsOld tvarsNew)
258259

259260
paramMigrationFnsTypeSig :: [Text]
260261
paramMigrationFnsTypeSig =
261262
paramMigrationPairs
262-
& fmap (\(oldT, newT) -> T.concat [ "(", N.toText oldT, "_old -> ", N.toText newT, "_new)" ] )
263+
& fmap (\(oldT, newT) -> T.concat [ "(", N.toText oldT, "_old -> ", N.toText newT, "_new)" ])
264+
265+
isTVarInUseUnion :: N.Name -> Bool
266+
isTVarInUseUnion newTVar =
267+
any (\(Can.Ctor _ _ _ params) -> any (isTVarInUse newTVar) params) (Can._u_alts newUnion)
263268

264269
paramMigrationVars :: Text
265270
paramMigrationVars =

extra/Lamdera/Evergreen/MigrationGeneratorHelpers.hs

+29
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,35 @@ loadTvar tvarMap name =
645645
-- This would be the alternative:
646646
-- Can.Tvar name
647647

648+
isTVarInUse :: N.Name -> Can.Type -> Bool
649+
isTVarInUse tVar typeValue =
650+
case typeValue of
651+
Can.TLambda a b -> isTVarInUse tVar a || isTVarInUse tVar b
652+
653+
Can.TVar name -> tVar == name
654+
655+
Can.TType _ _ typeVars -> any (isTVarInUse tVar) typeVars
656+
657+
Can.TRecord fields maybeExtension ->
658+
any (\(Can.FieldType _ fieldType) -> isTVarInUse tVar fieldType) fields
659+
|| case maybeExtension of
660+
Just extension -> tVar == extension
661+
Nothing -> False
662+
663+
Can.TUnit -> False
664+
665+
Can.TTuple t0 t1 maybeT2 ->
666+
isTVarInUse tVar t0
667+
|| isTVarInUse tVar t1
668+
|| case maybeT2 of
669+
Just t2 -> isTVarInUse tVar t2
670+
Nothing -> False
671+
672+
Can.TAlias moduleNameCanonical name fields aliasType ->
673+
any (\(_, fieldType) -> isTVarInUse tVar fieldType) fields
674+
|| case aliasType of
675+
Can.Holey a -> isTVarInUse tVar a
676+
Can.Filled a -> isTVarInUse tVar a
648677

649678
tvarResolveParams :: [Can.Type] -> [(N.Name, Can.Type)] -> [Can.Type]
650679
tvarResolveParams params tvarMap =

test/scenario-migration-generate/src/Migrate_All/Actual.elm

+8
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ migrate_Migrate_All_New_BackendModel old =
181181
, url = old.url
182182
, userCache = old.userCache |> migrate_AssocList_Dict identity migrate_IncludedBySpecialCasedParam_Custom
183183
, apps = (Unimplemented {- Type `Dict (String) (Migrate_All.New.App)` was added in V2. I need you to set a default value. -})
184+
, id = old.id |> migrate_Migrate_All_New_Id
184185
, depthTests = (Unimplemented {- Field of type `Dict (String) (Migrate_All.Old.Depth)` was removed in V2. I need you to do something with the `old.depthTests` value if you wish to keep the data, then remove this line. -})
185186
, removed = (Unimplemented {- Field of type `String` was removed in V2. I need you to do something with the `old.removed` value if you wish to keep the data, then remove this line. -})
186187
, removedRecord = (Unimplemented {- Field of type `Evergreen.V1.External.AllCoreTypes` was removed in V2. I need you to do something with the `old.removedRecord` value if you wish to keep the data, then remove this line. -})
@@ -197,6 +198,13 @@ migrate_Migrate_All_New_CustomType old =
197198
Migrate_All.New.CustomTwo
198199

199200

201+
migrate_Migrate_All_New_Id : Migrate_All.Old.Id a_old -> Migrate_All.New.Id a_new
202+
migrate_Migrate_All_New_Id old =
203+
case old of
204+
Migrate_All.Old.Id p0 ->
205+
Migrate_All.New.Id p0
206+
207+
200208
migrate_Migrate_All_New_UserType : Migrate_All.Old.UserType -> Migrate_All.New.UserType
201209
migrate_Migrate_All_New_UserType old =
202210
case old of

test/scenario-migration-generate/src/Migrate_All/Expected.elm

+9
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ migrate_Migrate_All_New_BackendModel old =
181181
, url = old.url
182182
, userCache = old.userCache |> migrate_AssocList_Dict identity migrate_IncludedBySpecialCasedParam_Custom
183183
, apps = (Unimplemented {- Type `Dict (String) (Migrate_All.New.App)` was added in V2. I need you to set a default value. -})
184+
, id = old.id |> migrate_Migrate_All_New_Id
184185
, depthTests = (Unimplemented {- Field of type `Dict (String) (Migrate_All.Old.Depth)` was removed in V2. I need you to do something with the `old.depthTests` value if you wish to keep the data, then remove this line. -})
185186
, removed = (Unimplemented {- Field of type `String` was removed in V2. I need you to do something with the `old.removed` value if you wish to keep the data, then remove this line. -})
186187
, removedRecord = (Unimplemented {- Field of type `Evergreen.V1.External.AllCoreTypes` was removed in V2. I need you to do something with the `old.removedRecord` value if you wish to keep the data, then remove this line. -})
@@ -197,6 +198,14 @@ migrate_Migrate_All_New_CustomType old =
197198
Migrate_All.New.CustomTwo
198199

199200

201+
migrate_Migrate_All_New_Id : Migrate_All.Old.Id a_old -> Migrate_All.New.Id a_new
202+
migrate_Migrate_All_New_Id old =
203+
case old of
204+
Migrate_All.Old.Id p0 ->
205+
Migrate_All.New.Id p0
206+
207+
208+
200209
migrate_Migrate_All_New_UserType : Migrate_All.Old.UserType -> Migrate_All.New.UserType
201210
migrate_Migrate_All_New_UserType old =
202211
case old of

test/scenario-migration-generate/src/Migrate_All/New.elm

+11
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ type alias BackendModel =
5656

5757
-- WIP
5858
, apps : Dict String App
59+
60+
-- Phantom type
61+
, id : Id UserId
5962
}
6063

6164

@@ -144,3 +147,11 @@ type alias ConfigUses =
144147

145148
type alias ConfigUse =
146149
( String, String, List String )
150+
151+
152+
type Id a =
153+
Id String
154+
155+
156+
type UserId =
157+
UserId

test/scenario-migration-generate/src/Migrate_All/Old.elm

+11
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ type alias BackendModel =
5757

5858
-- WIP
5959
, depthTests : Dict String Depth
60+
61+
-- Phantom type
62+
, id : Id UserId
6063
}
6164

6265

@@ -154,3 +157,11 @@ type UnionThatGetsMoved
154157

155158
type alias AliasThatGetsMoved =
156159
{ someThing : String }
160+
161+
162+
type Id a =
163+
Id String
164+
165+
166+
type UserId =
167+
UserId

test/scenario-migration-generate/src/Migrate_External_Paramed/Actual.elm

+3-3
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ migrate_Migrate_External_Paramed_New_NColor old =
103103

104104
migrate_Migrate_External_Paramed_New_Point2d : (units_old -> units_new) -> (coordinates_old -> coordinates_new) -> Migrate_External_Paramed.Old.Point2d units_old coordinates_old -> Migrate_External_Paramed.New.Point2d units_new coordinates_new
105105
migrate_Migrate_External_Paramed_New_Point2d migrate_units migrate_coordinates old =
106-
old |> migrate_Migrate_External_Paramed_New_Point2d_ migrate_units migrate_coordinates
106+
old |> migrate_Migrate_External_Paramed_New_Point2d_
107107

108108

109-
migrate_Migrate_External_Paramed_New_Point2d_ : (units_old -> units_new) -> (coordinates_old -> coordinates_new) -> Migrate_External_Paramed.Old.Point2d_ units_old coordinates_old -> Migrate_External_Paramed.New.Point2d_ units_new coordinates_new
110-
migrate_Migrate_External_Paramed_New_Point2d_ migrate_units migrate_coordinates old =
109+
migrate_Migrate_External_Paramed_New_Point2d_ : Migrate_External_Paramed.Old.Point2d_ units_old coordinates_old -> Migrate_External_Paramed.New.Point2d_ units_new coordinates_new
110+
migrate_Migrate_External_Paramed_New_Point2d_ old =
111111
case old of
112112
Migrate_External_Paramed.Old.Point2d_ p0 ->
113113
Migrate_External_Paramed.New.Point2d_ p0

test/scenario-migration-generate/src/Migrate_External_Paramed/Expected.elm

+3-3
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ migrate_Migrate_External_Paramed_New_NColor old =
103103

104104
migrate_Migrate_External_Paramed_New_Point2d : (units_old -> units_new) -> (coordinates_old -> coordinates_new) -> Migrate_External_Paramed.Old.Point2d units_old coordinates_old -> Migrate_External_Paramed.New.Point2d units_new coordinates_new
105105
migrate_Migrate_External_Paramed_New_Point2d migrate_units migrate_coordinates old =
106-
old |> migrate_Migrate_External_Paramed_New_Point2d_ migrate_units migrate_coordinates
106+
old |> migrate_Migrate_External_Paramed_New_Point2d_
107107

108108

109-
migrate_Migrate_External_Paramed_New_Point2d_ : (units_old -> units_new) -> (coordinates_old -> coordinates_new) -> Migrate_External_Paramed.Old.Point2d_ units_old coordinates_old -> Migrate_External_Paramed.New.Point2d_ units_new coordinates_new
110-
migrate_Migrate_External_Paramed_New_Point2d_ migrate_units migrate_coordinates old =
109+
migrate_Migrate_External_Paramed_New_Point2d_ : Migrate_External_Paramed.Old.Point2d_ units_old coordinates_old -> Migrate_External_Paramed.New.Point2d_ units_new coordinates_new
110+
migrate_Migrate_External_Paramed_New_Point2d_ old =
111111
case old of
112112
Migrate_External_Paramed.Old.Point2d_ p0 ->
113113
Migrate_External_Paramed.New.Point2d_ p0

0 commit comments

Comments
 (0)