Skip to content

Commit

Permalink
Merge pull request #15 from lamdera/fix-phantom-types-migration
Browse files Browse the repository at this point in the history
Ignore phantom types when doing migration code gen
  • Loading branch information
supermario authored Feb 18, 2024
2 parents 2be7e74 + 88479e4 commit 5116003
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 9 deletions.
11 changes: 8 additions & 3 deletions extra/Lamdera/Evergreen/MigrationGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ migrateUnionDefinition_ author pkg oldUnion newUnion tvarMapOld tvarMapNew oldVe

tvarPairs :: [(Can.Type, Can.Type)]
tvarPairs =
zip (loadTvars tvarsOld tvarMapOld) (loadTvars tvarsNew tvarMapNew)
zip (loadTvars (fmap fst paramMigrationPairs) tvarMapOld) (loadTvars (fmap snd paramMigrationPairs) tvarMapNew)

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

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

paramMigrationFnsTypeSig :: [Text]
paramMigrationFnsTypeSig =
paramMigrationPairs
& fmap (\(oldT, newT) -> T.concat [ "(", N.toText oldT, "_old -> ", N.toText newT, "_new)" ] )
& fmap (\(oldT, newT) -> T.concat [ "(", N.toText oldT, "_old -> ", N.toText newT, "_new)" ])

isTVarInUseUnion :: N.Name -> Bool
isTVarInUseUnion newTVar =
any (\(Can.Ctor _ _ _ params) -> any (isTVarInUse newTVar) params) (Can._u_alts newUnion)

paramMigrationVars :: Text
paramMigrationVars =
Expand Down
29 changes: 29 additions & 0 deletions extra/Lamdera/Evergreen/MigrationGeneratorHelpers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,35 @@ loadTvar tvarMap name =
-- This would be the alternative:
-- Can.Tvar name

isTVarInUse :: N.Name -> Can.Type -> Bool
isTVarInUse tVar typeValue =
case typeValue of
Can.TLambda a b -> isTVarInUse tVar a || isTVarInUse tVar b

Can.TVar name -> tVar == name

Can.TType _ _ typeVars -> any (isTVarInUse tVar) typeVars

Can.TRecord fields maybeExtension ->
any (\(Can.FieldType _ fieldType) -> isTVarInUse tVar fieldType) fields
|| case maybeExtension of
Just extension -> tVar == extension
Nothing -> False

Can.TUnit -> False

Can.TTuple t0 t1 maybeT2 ->
isTVarInUse tVar t0
|| isTVarInUse tVar t1
|| case maybeT2 of
Just t2 -> isTVarInUse tVar t2
Nothing -> False

Can.TAlias moduleNameCanonical name fields aliasType ->
any (\(_, fieldType) -> isTVarInUse tVar fieldType) fields
|| case aliasType of
Can.Holey a -> isTVarInUse tVar a
Can.Filled a -> isTVarInUse tVar a

tvarResolveParams :: [Can.Type] -> [(N.Name, Can.Type)] -> [Can.Type]
tvarResolveParams params tvarMap =
Expand Down
8 changes: 8 additions & 0 deletions test/scenario-migration-generate/src/Migrate_All/Actual.elm
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ migrate_Migrate_All_New_BackendModel old =
, url = old.url
, userCache = old.userCache |> migrate_AssocList_Dict identity migrate_IncludedBySpecialCasedParam_Custom
, apps = (Unimplemented {- Type `Dict (String) (Migrate_All.New.App)` was added in V2. I need you to set a default value. -})
, id = old.id |> migrate_Migrate_All_New_Id
, 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. -})
, 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. -})
, 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. -})
Expand All @@ -197,6 +198,13 @@ migrate_Migrate_All_New_CustomType old =
Migrate_All.New.CustomTwo


migrate_Migrate_All_New_Id : Migrate_All.Old.Id a_old -> Migrate_All.New.Id a_new
migrate_Migrate_All_New_Id old =
case old of
Migrate_All.Old.Id p0 ->
Migrate_All.New.Id p0


migrate_Migrate_All_New_UserType : Migrate_All.Old.UserType -> Migrate_All.New.UserType
migrate_Migrate_All_New_UserType old =
case old of
Expand Down
9 changes: 9 additions & 0 deletions test/scenario-migration-generate/src/Migrate_All/Expected.elm
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ migrate_Migrate_All_New_BackendModel old =
, url = old.url
, userCache = old.userCache |> migrate_AssocList_Dict identity migrate_IncludedBySpecialCasedParam_Custom
, apps = (Unimplemented {- Type `Dict (String) (Migrate_All.New.App)` was added in V2. I need you to set a default value. -})
, id = old.id |> migrate_Migrate_All_New_Id
, 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. -})
, 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. -})
, 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. -})
Expand All @@ -197,6 +198,14 @@ migrate_Migrate_All_New_CustomType old =
Migrate_All.New.CustomTwo


migrate_Migrate_All_New_Id : Migrate_All.Old.Id a_old -> Migrate_All.New.Id a_new
migrate_Migrate_All_New_Id old =
case old of
Migrate_All.Old.Id p0 ->
Migrate_All.New.Id p0



migrate_Migrate_All_New_UserType : Migrate_All.Old.UserType -> Migrate_All.New.UserType
migrate_Migrate_All_New_UserType old =
case old of
Expand Down
11 changes: 11 additions & 0 deletions test/scenario-migration-generate/src/Migrate_All/New.elm
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ type alias BackendModel =

-- WIP
, apps : Dict String App

-- Phantom type
, id : Id UserId
}


Expand Down Expand Up @@ -144,3 +147,11 @@ type alias ConfigUses =

type alias ConfigUse =
( String, String, List String )


type Id a =
Id String


type UserId =
UserId
11 changes: 11 additions & 0 deletions test/scenario-migration-generate/src/Migrate_All/Old.elm
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ type alias BackendModel =

-- WIP
, depthTests : Dict String Depth

-- Phantom type
, id : Id UserId
}


Expand Down Expand Up @@ -154,3 +157,11 @@ type UnionThatGetsMoved

type alias AliasThatGetsMoved =
{ someThing : String }


type Id a =
Id String


type UserId =
UserId
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ migrate_Migrate_External_Paramed_New_NColor old =

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
migrate_Migrate_External_Paramed_New_Point2d migrate_units migrate_coordinates old =
old |> migrate_Migrate_External_Paramed_New_Point2d_ migrate_units migrate_coordinates
old |> migrate_Migrate_External_Paramed_New_Point2d_


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
migrate_Migrate_External_Paramed_New_Point2d_ migrate_units migrate_coordinates old =
migrate_Migrate_External_Paramed_New_Point2d_ : Migrate_External_Paramed.Old.Point2d_ units_old coordinates_old -> Migrate_External_Paramed.New.Point2d_ units_new coordinates_new
migrate_Migrate_External_Paramed_New_Point2d_ old =
case old of
Migrate_External_Paramed.Old.Point2d_ p0 ->
Migrate_External_Paramed.New.Point2d_ p0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ migrate_Migrate_External_Paramed_New_NColor old =

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
migrate_Migrate_External_Paramed_New_Point2d migrate_units migrate_coordinates old =
old |> migrate_Migrate_External_Paramed_New_Point2d_ migrate_units migrate_coordinates
old |> migrate_Migrate_External_Paramed_New_Point2d_


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
migrate_Migrate_External_Paramed_New_Point2d_ migrate_units migrate_coordinates old =
migrate_Migrate_External_Paramed_New_Point2d_ : Migrate_External_Paramed.Old.Point2d_ units_old coordinates_old -> Migrate_External_Paramed.New.Point2d_ units_new coordinates_new
migrate_Migrate_External_Paramed_New_Point2d_ old =
case old of
Migrate_External_Paramed.Old.Point2d_ p0 ->
Migrate_External_Paramed.New.Point2d_ p0
Expand Down

0 comments on commit 5116003

Please sign in to comment.