2021-12-30 14:00:52 +03:00
|
|
|
{-# OPTIONS -Wno-redundant-constraints #-}
|
2022-03-10 14:18:13 +03:00
|
|
|
{-# LANGUAGE ViewPatterns #-}
|
2021-12-30 14:00:52 +03:00
|
|
|
|
|
|
|
-- | CitusQL helpers. Pretty much the same as postgres. Could refactor
|
|
|
|
-- if we add more things here.
|
2022-01-21 10:48:27 +03:00
|
|
|
module Harness.Backend.Citus
|
2021-12-30 14:00:52 +03:00
|
|
|
( livenessCheck,
|
|
|
|
run_,
|
2022-01-25 19:34:29 +03:00
|
|
|
defaultSourceMetadata,
|
2022-03-01 01:47:51 +03:00
|
|
|
createTable,
|
|
|
|
insertTable,
|
|
|
|
trackTable,
|
|
|
|
dropTable,
|
|
|
|
untrackTable,
|
|
|
|
setup,
|
|
|
|
teardown,
|
2021-12-30 14:00:52 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Control.Concurrent
|
|
|
|
import Control.Exception
|
|
|
|
import Control.Monad.Reader
|
2022-01-25 19:34:29 +03:00
|
|
|
import Data.Aeson (Value)
|
2022-03-01 01:47:51 +03:00
|
|
|
import Data.Bool (bool)
|
2021-12-30 14:00:52 +03:00
|
|
|
import Data.ByteString.Char8 qualified as S8
|
2022-03-01 01:47:51 +03:00
|
|
|
import Data.Foldable (for_)
|
2021-12-30 14:00:52 +03:00
|
|
|
import Data.String
|
2022-03-01 01:47:51 +03:00
|
|
|
import Data.Text (Text)
|
|
|
|
import Data.Text qualified as T
|
|
|
|
import Data.Text.Extended (commaSeparated)
|
2021-12-30 14:00:52 +03:00
|
|
|
import Database.PostgreSQL.Simple qualified as Postgres
|
|
|
|
import GHC.Stack
|
|
|
|
import Harness.Constants as Constants
|
2022-03-01 01:47:51 +03:00
|
|
|
import Harness.GraphqlEngine qualified as GraphqlEngine
|
2022-01-25 19:34:29 +03:00
|
|
|
import Harness.Quoter.Yaml (yaml)
|
2022-03-01 01:47:51 +03:00
|
|
|
import Harness.State (State)
|
|
|
|
import Harness.Test.Schema qualified as Schema
|
2021-12-30 14:00:52 +03:00
|
|
|
import System.Process.Typed
|
|
|
|
import Prelude
|
|
|
|
|
|
|
|
-- | Check the citus server is live and ready to accept connections.
|
|
|
|
livenessCheck :: HasCallStack => IO ()
|
|
|
|
livenessCheck = loop Constants.postgresLivenessCheckAttempts
|
|
|
|
where
|
|
|
|
loop 0 = error ("Liveness check failed for Citus.")
|
|
|
|
loop attempts =
|
|
|
|
catch
|
|
|
|
( bracket
|
|
|
|
( Postgres.connectPostgreSQL
|
|
|
|
(fromString Constants.citusConnectionString)
|
|
|
|
)
|
|
|
|
Postgres.close
|
|
|
|
(const (pure ()))
|
|
|
|
)
|
|
|
|
( \(_failure :: ExitCodeException) -> do
|
|
|
|
threadDelay
|
|
|
|
Constants.postgresLivenessCheckIntervalMicroseconds
|
|
|
|
loop (attempts - 1)
|
|
|
|
)
|
|
|
|
|
|
|
|
-- | Run a plain SQL query. On error, print something useful for
|
|
|
|
-- debugging.
|
|
|
|
run_ :: HasCallStack => String -> IO ()
|
|
|
|
run_ q =
|
|
|
|
catch
|
|
|
|
( bracket
|
|
|
|
( Postgres.connectPostgreSQL
|
|
|
|
(fromString Constants.citusConnectionString)
|
|
|
|
)
|
|
|
|
Postgres.close
|
|
|
|
(\conn -> void (Postgres.execute_ conn (fromString q)))
|
|
|
|
)
|
|
|
|
( \(e :: Postgres.SqlError) ->
|
|
|
|
error
|
|
|
|
( unlines
|
|
|
|
[ "Citus query error:",
|
|
|
|
S8.unpack (Postgres.sqlErrorMsg e),
|
|
|
|
"SQL was:",
|
|
|
|
q
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
2022-01-25 19:34:29 +03:00
|
|
|
|
|
|
|
-- | Metadata source information for the default Citus instance.
|
|
|
|
defaultSourceMetadata :: Value
|
|
|
|
defaultSourceMetadata =
|
|
|
|
[yaml|
|
|
|
|
name: citus
|
|
|
|
kind: citus
|
|
|
|
tables: []
|
|
|
|
configuration:
|
|
|
|
connection_info:
|
|
|
|
database_url: *citusConnectionString
|
|
|
|
pool_settings: {}
|
|
|
|
|]
|
2022-03-01 01:47:51 +03:00
|
|
|
|
|
|
|
-- | Serialize Table into a Citus-SQL statement, as needed, and execute it on the Citus backend
|
|
|
|
createTable :: Schema.Table -> IO ()
|
|
|
|
createTable Schema.Table {tableName, tableColumns, tablePrimaryKey = pk, tableReferences} = do
|
|
|
|
run_ $
|
|
|
|
T.unpack $
|
|
|
|
T.unwords
|
|
|
|
[ "CREATE TABLE",
|
|
|
|
T.pack Constants.citusDb <> "." <> tableName,
|
|
|
|
"(",
|
|
|
|
commaSeparated $
|
|
|
|
(mkColumn <$> tableColumns)
|
|
|
|
<> (bool [mkPrimaryKey pk] [] (null pk))
|
|
|
|
<> (mkReference <$> tableReferences),
|
|
|
|
");"
|
|
|
|
]
|
2022-03-10 14:18:13 +03:00
|
|
|
|
|
|
|
scalarType :: HasCallStack => Schema.ScalarType -> Text
|
|
|
|
scalarType = \case
|
|
|
|
Schema.TInt -> "SERIAL"
|
|
|
|
Schema.TStr -> "VARCHAR"
|
|
|
|
Schema.TUTCTime -> "TIMESTAMP"
|
|
|
|
Schema.TBool -> "BOOLEAN"
|
|
|
|
t -> error $ "Unexpected scalar type used for Citus: " <> show t
|
|
|
|
|
|
|
|
mkColumn :: Schema.Column -> Text
|
|
|
|
mkColumn Schema.Column {columnName, columnType, columnNullable, columnDefault} =
|
|
|
|
T.unwords
|
|
|
|
[ columnName,
|
|
|
|
scalarType columnType,
|
|
|
|
bool "NOT NULL" "DEFAULT NULL" columnNullable,
|
|
|
|
maybe "" ("DEFAULT " <>) columnDefault
|
|
|
|
]
|
|
|
|
|
|
|
|
mkPrimaryKey :: [Text] -> Text
|
|
|
|
mkPrimaryKey key =
|
|
|
|
T.unwords
|
|
|
|
[ "PRIMARY KEY",
|
|
|
|
"(",
|
|
|
|
commaSeparated key,
|
|
|
|
")"
|
|
|
|
]
|
|
|
|
|
|
|
|
mkReference :: Schema.Reference -> Text
|
|
|
|
mkReference Schema.Reference {referenceLocalColumn, referenceTargetTable, referenceTargetColumn} =
|
|
|
|
T.unwords
|
|
|
|
[ "CONSTRAINT FOREIGN KEY",
|
|
|
|
"(",
|
|
|
|
referenceLocalColumn,
|
|
|
|
")",
|
|
|
|
"REFERENCES",
|
|
|
|
referenceTargetTable,
|
|
|
|
"(",
|
|
|
|
referenceTargetColumn,
|
|
|
|
")",
|
|
|
|
"ON DELETE CASCADE",
|
|
|
|
"ON UPDATE CASCADE"
|
|
|
|
]
|
2022-03-01 01:47:51 +03:00
|
|
|
|
|
|
|
-- | Serialize tableData into a Citus-SQL insert statement and execute it.
|
|
|
|
insertTable :: Schema.Table -> IO ()
|
|
|
|
insertTable Schema.Table {tableName, tableColumns, tableData} =
|
|
|
|
run_ $
|
|
|
|
T.unpack $
|
|
|
|
T.unwords
|
|
|
|
[ "INSERT INTO",
|
|
|
|
T.pack Constants.citusDb <> "." <> tableName,
|
|
|
|
"(",
|
|
|
|
commaSeparated (Schema.columnName <$> tableColumns),
|
|
|
|
")",
|
|
|
|
"VALUES",
|
|
|
|
commaSeparated $ mkRow <$> tableData,
|
|
|
|
";"
|
|
|
|
]
|
2022-03-10 14:18:13 +03:00
|
|
|
|
|
|
|
mkRow :: [Schema.ScalarValue] -> Text
|
|
|
|
mkRow row =
|
|
|
|
T.unwords
|
|
|
|
[ "(",
|
|
|
|
commaSeparated $ Schema.serialize <$> row,
|
|
|
|
")"
|
|
|
|
]
|
2022-03-01 01:47:51 +03:00
|
|
|
|
|
|
|
-- | Post an http request to start tracking the table
|
|
|
|
trackTable :: State -> Schema.Table -> IO ()
|
2022-03-10 14:18:13 +03:00
|
|
|
trackTable state table = Schema.trackTable "citus" "citus" (T.pack Constants.citusDb) table state
|
2022-03-01 01:47:51 +03:00
|
|
|
|
|
|
|
-- | Serialize Table into a Citus-SQL DROP statement and execute it
|
|
|
|
dropTable :: Schema.Table -> IO ()
|
|
|
|
dropTable Schema.Table {tableName} = do
|
|
|
|
run_ $
|
|
|
|
T.unpack $
|
|
|
|
T.unwords
|
2022-03-10 14:18:13 +03:00
|
|
|
[ "DROP TABLE", -- we don't want @IF EXISTS@ here, because we don't want this to fail silently
|
2022-03-01 01:47:51 +03:00
|
|
|
T.pack Constants.citusDb <> "." <> tableName,
|
|
|
|
";"
|
|
|
|
]
|
|
|
|
|
|
|
|
-- | Post an http request to stop tracking the table
|
|
|
|
untrackTable :: State -> Schema.Table -> IO ()
|
2022-03-10 14:18:13 +03:00
|
|
|
untrackTable state table = Schema.untrackTable "citus" "citus" (T.pack Constants.citusDb) table state
|
2022-03-01 01:47:51 +03:00
|
|
|
|
|
|
|
-- | Setup the schema in the most expected way.
|
|
|
|
-- NOTE: Certain test modules may warrant having their own local version.
|
|
|
|
setup :: [Schema.Table] -> (State, ()) -> IO ()
|
|
|
|
setup tables (state, _) = do
|
|
|
|
-- Clear and reconfigure the metadata
|
|
|
|
GraphqlEngine.setSource state defaultSourceMetadata
|
|
|
|
-- Setup and track tables
|
|
|
|
for_ tables $ \table -> do
|
|
|
|
createTable table
|
|
|
|
insertTable table
|
|
|
|
trackTable state table
|
2022-03-10 14:18:13 +03:00
|
|
|
-- Setup relationships
|
|
|
|
for_ tables $ \table -> do
|
|
|
|
Schema.trackObjectRelationships "citus" (T.pack Constants.citusDb) table state
|
|
|
|
Schema.trackArrayRelationships "citus" (T.pack Constants.citusDb) table state
|
2022-03-01 01:47:51 +03:00
|
|
|
|
|
|
|
-- | Teardown the schema and tracking in the most expected way.
|
|
|
|
-- NOTE: Certain test modules may warrant having their own version.
|
|
|
|
teardown :: [Schema.Table] -> (State, ()) -> IO ()
|
2022-03-10 14:18:13 +03:00
|
|
|
teardown (reverse -> tables) (state, _) = do
|
|
|
|
-- Teardown relationships first
|
|
|
|
for_ tables $ \table ->
|
|
|
|
Schema.untrackRelationships "citus" (T.pack Constants.citusDb) table state
|
|
|
|
-- Then teardown tables
|
2022-03-01 01:47:51 +03:00
|
|
|
for_ tables $ \table -> do
|
|
|
|
untrackTable state table
|
|
|
|
dropTable table
|