Skip to content

Commit 9667923

Browse files
committed
Use custom UTCTime type to retain Show/Read format
There's no functional need for this, using the JSON representation would've been readable enough, but there are going to be tons of migration files in the wild with these simplified values. We could write encoding at JSON and a decoder that accepts either, but I'm not sure it's worth it vs just using a completely custom newtype.
1 parent 72b1f3a commit 9667923

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
@@ -20,10 +20,10 @@ import Data.String.Conversions ( cs, (<>) )
2020

2121
import Data.Typeable ( Typeable )
2222
import Data.Time.Clock ( UTCTime )
23-
import Data.Time () -- for UTCTime Show instance
23+
import Data.Time ( defaultTimeLocale, formatTime, parseTimeM )
2424
import qualified Data.Map as Map
2525

26-
import Control.Applicative ( (<$>) )
26+
import Control.Applicative ( (<$>), (<|>) )
2727
import Control.Monad ( filterM )
2828
import Control.Exception ( Exception(..), throw, catch )
2929

@@ -109,7 +109,7 @@ migrationFromPath path = do
109109

110110
-- | TODO: re-use this for the generation side too
111111
data MigrationYaml = MigrationYaml
112-
{ myCreated :: Maybe UTCTime
112+
{ myCreated :: Maybe UTCTimeYaml
113113
, myDescription :: Maybe Text
114114
, myApply :: Text
115115
, myRevert :: Maybe Text
@@ -134,10 +134,28 @@ jsonOptions = defaultOptions
134134

135135
migrationYamlToMigration :: Text -> MigrationYaml -> Migration
136136
migrationYamlToMigration theId my = Migration
137-
{ mTimestamp = myCreated my
137+
{ mTimestamp = unUTCTimeYaml <$> myCreated my
138138
, mId = theId
139139
, mDesc = myDescription my
140140
, mApply = myApply my
141141
, mRevert = myRevert my
142142
, mDeps = T.words $ myDepends my
143143
}
144+
145+
newtype UTCTimeYaml = UTCTimeYaml
146+
{ unUTCTimeYaml :: UTCTime
147+
}
148+
149+
instance FromJSON UTCTimeYaml where
150+
parseJSON = withText "UTCTime"
151+
$ maybe (fail "Unable to parse UTCTime") (pure . UTCTimeYaml)
152+
. parseTimeM True defaultTimeLocale utcTimeYamlFormat
153+
. cs
154+
155+
instance ToJSON UTCTimeYaml where
156+
toJSON = toJSON . formatTime defaultTimeLocale utcTimeYamlFormat . unUTCTimeYaml
157+
toEncoding = toEncoding . formatTime defaultTimeLocale utcTimeYamlFormat . unUTCTimeYaml
158+
159+
-- Keeps things as the old Show/Read-based format, e.g "2009-04-15 10:02:06 UTC"
160+
utcTimeYamlFormat :: String
161+
utcTimeYamlFormat = "%F %T UTC"

0 commit comments

Comments
 (0)