[pgmq] use Paths_haskell_pgmq for pgmq.sql

parent cd93101b
Pipeline #6741 passed with stages
in 8 minutes and 52 seconds
......@@ -66,6 +66,9 @@ library
, Database.PGMQ.Schema
, Database.PGMQ.Simple
, Database.PGMQ.Types
other-modules:
, Paths_haskell_pgmq
-- Modules included in this library but not exported.
-- other-modules:
......@@ -79,9 +82,7 @@ library
, bytestring >= 0.11 && < 0.13
, postgresql-simple >= 0.6 && < 0.8
, safe >= 0.3 && < 0.4
, template-haskell >= 2.19.0.0 && < 2.20
, text >= 1.2 && < 2.2
, th-utilities >= 0.2.5.0 && < 0.3
, time >= 1.10 && < 1.15
, units >= 2.4 && < 2.5
......
{-# LANGUAGE TemplateHaskell #-}
module Database.PGMQ.Schema where
import Language.Haskell.TH.Syntax (lift)
import TH.RelativePaths (qReadFileString)
import Paths_haskell_pgmq
pgmqSchema :: String
pgmqSchema = $(qReadFileString "sql/pgmq.sql" >>= lift)
pgmqSchema :: IO String
pgmqSchema = do
f <- getDataFileName "sql/pgmq.sql"
readFile f
......@@ -57,13 +57,14 @@ import Safe (headMay)
{-| Initialize PGMQ given a PostgreSQL connection. Mainly concerned
with creating the 'pgmq' extension. -}
initialize :: PSQL.Connection -> IO ()
initialize conn =
initialize conn = do
-- OK so this is a bit tricky because of the usage of IF NOT EXISTS:
-- https://stackoverflow.com/questions/29900845/create-schema-if-not-exists-raises-duplicate-key-error
-- PostgreSQL will complain badly if 'initialize' is called
-- from multiple threads at once.
-- Hence, we use 'pg_advisory_xact_lock' to lock ourselves
-- out of this situation.
pgmqSchema' <- pgmqSchema
PSQL.withTransaction conn $ do
let magicLockId = 1122334455 :: Int
_ <- PSQL.query conn [sql| SELECT pg_advisory_xact_lock(?) |] (PSQL.Only magicLockId) :: IO [PSQL.Only ()]
......@@ -75,7 +76,7 @@ initialize conn =
-- extension by only running the SQL, not using `CREATE
-- EXTENSION`. But it works and is quite hassle-free
void $ PSQL.execute_ conn [sql|CREATE SCHEMA pgmq |]
void $ PSQL.execute_ conn (fromString pgmqSchema)
void $ PSQL.execute_ conn (fromString pgmqSchema')
-- void $ PSQL.execute_ conn [sql| CREATE EXTENSION IF NOT EXISTS pgmq |]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment