Skip to content

Commit 691b3c2

Browse files
erikddcoutts
authored andcommitted
db: Add handling of PersistRational for PersistField DbWord64
Due to an impedance mismatch between PostgreSQL, the C langauge and Haskell's Persistent library, 'Word64' values larger than the maxiumum value of a signed 64 bit integer come back as a 'PersistRational' with a denominator of '1'. We have to handle that case. Closes: #334
1 parent a967b1b commit 691b3c2

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

cardano-db/src/Cardano/Db/Schema/Orphans.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ import Shelley.Spec.Ledger.PParams (ProtVer (..))
2020
instance PersistField DbWord64 where
2121
toPersistValue = PersistText . Text.pack . show . unDbWord64
2222
fromPersistValue (PersistText bs) = Right $ DbWord64 (read $ Text.unpack bs)
23+
fromPersistValue x@(PersistRational r) =
24+
-- If the value is greater than MAX_INT64, it comes back as a PersistRational (wat??).
25+
if denominator r == 1
26+
then Right $ DbWord64 (fromIntegral $ numerator r)
27+
else Left $ mconcat [ "Failed to parse Haskell type DbWord64: ", Text.pack (show x) ]
2328
fromPersistValue x =
24-
Left $ mconcat [ "Failed to parse Haskell type Word64: ", Text.pack (show x) ]
29+
Left $ mconcat [ "Failed to parse Haskell type DbWord64: ", Text.pack (show x) ]
2530

2631
instance PersistField Word128 where
2732
toPersistValue = PersistText . Text.pack . show

0 commit comments

Comments
 (0)