Skip to content

Commit aee4a0e

Browse files
committed
Preserve even weirder Depends behavior
It seems the `Depends` _key_ is indeed required, but a `Null` as the _value_ is acceptable (and defaulted to no dependencies). I don't know if we need to preserve this too, but the tests fail if we don't. It was super fun to see one test assert it was optional and fail, only to have the test that asserts it as required then fail (:
1 parent 25127fc commit aee4a0e

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

src/Database/Schema/Migrations/Filesystem.hs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, ScopedTypeVariables, OverloadedStrings #-}
1+
{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, LambdaCase, ScopedTypeVariables, OverloadedStrings #-}
22
-- |This module provides a type for interacting with a
33
-- filesystem-backed 'MigrationStore'.
44
module Database.Schema.Migrations.Filesystem
@@ -28,6 +28,7 @@ import Control.Monad ( filterM )
2828
import Control.Exception ( Exception(..), throw, catch )
2929

3030
import Data.Aeson
31+
import Data.Aeson.Types (typeMismatch)
3132
import Data.HashMap.Strict as M (toList)
3233
import qualified Data.Yaml as Yaml
3334
import GHC.Generics (Generic)
@@ -113,8 +114,7 @@ data MigrationYaml = MigrationYaml
113114
, myDescription :: Maybe Text
114115
, myApply :: Text
115116
, myRevert :: Maybe Text
116-
, myDepends :: Maybe Text
117-
-- ^ White-space separated names
117+
, myDepends :: DependsYaml
118118
}
119119
deriving Generic
120120

@@ -139,7 +139,7 @@ migrationYamlToMigration theId my = Migration
139139
, mDesc = myDescription my
140140
, mApply = myApply my
141141
, mRevert = myRevert my
142-
, mDeps = maybe [] T.words $ myDepends my
142+
, mDeps = unDependsYaml $ myDepends my
143143
}
144144

145145
newtype UTCTimeYaml = UTCTimeYaml
@@ -159,3 +159,21 @@ instance ToJSON UTCTimeYaml where
159159
-- Keeps things as the old Show/Read-based format, e.g "2009-04-15 10:02:06 UTC"
160160
utcTimeYamlFormat :: String
161161
utcTimeYamlFormat = "%F %T UTC"
162+
163+
newtype DependsYaml = DependsYaml
164+
{ unDependsYaml :: [Text]
165+
}
166+
167+
instance FromJSON DependsYaml where
168+
parseJSON = \case
169+
Null -> pure $ DependsYaml []
170+
String t -> pure $ DependsYaml $ T.words t
171+
x -> typeMismatch "Null or whitespace-separated String" x
172+
173+
instance ToJSON DependsYaml where
174+
toJSON (DependsYaml ts) = case ts of
175+
[] -> toJSON Null
176+
_ -> toJSON $ T.unwords ts
177+
toEncoding (DependsYaml ts) = case ts of
178+
[] -> toEncoding Null
179+
_ -> toEncoding $ T.unwords ts

0 commit comments

Comments
 (0)