Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append ::uuid to Postgres UUID value syntax #583

Merged
merged 1 commit into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions beam-postgres/Database/Beam/Postgres/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import qualified Data.Text.Lazy as TL
import Data.Time (LocalTime, UTCTime, TimeOfDay, NominalDiffTime, Day)
import Data.UUID.Types (UUID)
import Data.UUID.Types (UUID, toASCIIBytes)
import Data.Word
import qualified Data.Vector as V
import GHC.TypeLits
Expand Down Expand Up @@ -1197,7 +1197,6 @@ DEFAULT_SQL_SYNTAX(UTCTime)
DEFAULT_SQL_SYNTAX(TimeOfDay)
DEFAULT_SQL_SYNTAX(NominalDiffTime)
DEFAULT_SQL_SYNTAX(Day)
DEFAULT_SQL_SYNTAX(UUID)
DEFAULT_SQL_SYNTAX([Char])
DEFAULT_SQL_SYNTAX(Pg.HStoreMap)
DEFAULT_SQL_SYNTAX(Pg.HStoreList)
Expand Down Expand Up @@ -1225,6 +1224,12 @@ instance HasSqlValueSyntax PgValueSyntax B.ByteString where
instance HasSqlValueSyntax PgValueSyntax BL.ByteString where
sqlValueSyntax = defaultPgValueSyntax . Pg.Binary

-- This should be removed in favor of the default syntax if/when
-- https://github.com/lpsmith/postgresql-simple/issues/277 is fixed upstream.
instance HasSqlValueSyntax PgValueSyntax UUID where
sqlValueSyntax v = PgValueSyntax $
emit "'" <> emit (toASCIIBytes v) <> emit "'::uuid"

instance Pg.ToField a => HasSqlValueSyntax PgValueSyntax (V.Vector a) where
sqlValueSyntax = defaultPgValueSyntax

Expand Down
15 changes: 15 additions & 0 deletions beam-postgres/test/Database/Beam/Postgres/Test/Select.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import qualified Data.Vector as V
import Test.Tasty
import Test.Tasty.HUnit
import Data.UUID (UUID, nil)
import qualified Data.UUID.V5 as V5

import Database.Beam
import Database.Beam.Backend.SQL.SQL92
Expand Down Expand Up @@ -39,6 +40,7 @@ tests getConn = testGroup "Selection Tests"
pgUuidGenerateV4 ext
, testUuidFunction getConn "uuid_generate_v5" $ \ext ->
pgUuidGenerateV5 ext (val_ nil) "nil"
, testUuuidInValues getConn
]
, testInRowValues getConn
, testReturningMany getConn
Expand Down Expand Up @@ -70,6 +72,19 @@ testUuidFunction getConn name mkUuid = testFunction getConn name $ \conn ->
return $ mkUuid $ getPgExtension $ _uuidOssp $ unCheckDatabase db
return ()

-- | Regression test for <https://github.com/haskell-beam/beam/issues/555 #555>
testUuuidInValues :: IO ByteString -> TestTree
testUuuidInValues getConn = testCase "UUID in values_ works" $
withTestPostgres "uuid_values" getConn $ \conn -> do
result <- runBeamPostgres conn $ do
db <- executeMigration runNoReturn $ UuidSchema <$>
pgCreateExtension @UuidOssp
let ext = getPgExtension $ _uuidOssp $ unCheckDatabase db
runSelectReturningList $ select $ do
v <- values_ [val_ nil]
return $ pgUuidGenerateV5 ext v ""
assertEqual "result" [V5.generateNamed nil []] result

data Pair f = Pair
{ _left :: C f Bool
, _right :: C f Bool
Expand Down