2022-03-16 03:39:21 +03:00
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
{-# LANGUAGE ViewPatterns #-}
|
|
|
|
|
2021-11-17 22:50:39 +03:00
|
|
|
{-# OPTIONS -Wno-redundant-constraints #-}
|
|
|
|
|
|
|
|
-- | MySQL helpers.
|
2022-01-21 10:48:27 +03:00
|
|
|
module Harness.Backend.Mysql
|
2021-11-17 22:50:39 +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,
|
2022-06-08 19:35:44 +03:00
|
|
|
setupTablesAction,
|
|
|
|
setupPermissionsAction,
|
2021-11-17 22:50:39 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Control.Concurrent
|
2021-11-26 21:21:01 +03:00
|
|
|
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)
|
|
|
|
import Data.Foldable (for_)
|
2021-11-17 22:50:39 +03:00
|
|
|
import Data.String
|
2022-04-12 18:39:36 +03:00
|
|
|
import Data.Text (Text, pack, replace)
|
2022-03-01 01:47:51 +03:00
|
|
|
import Data.Text qualified as T
|
|
|
|
import Data.Text.Extended (commaSeparated)
|
2022-04-12 18:39:36 +03:00
|
|
|
import Data.Time (defaultTimeLocale, formatTime)
|
2021-11-17 22:50:39 +03:00
|
|
|
import Database.MySQL.Simple as Mysql
|
2021-11-26 21:21:01 +03:00
|
|
|
import Harness.Constants as Constants
|
2022-04-04 17:45:12 +03:00
|
|
|
import Harness.Exceptions
|
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-06-08 19:35:44 +03:00
|
|
|
import Harness.Test.Context (BackendType (MySQL))
|
|
|
|
import Harness.Test.Fixture
|
|
|
|
import Harness.Test.Permissions qualified as Permissions
|
2022-04-19 18:39:02 +03:00
|
|
|
import Harness.Test.Schema (BackendScalarType (..), BackendScalarValue (..), ScalarValue (..))
|
2022-03-01 01:47:51 +03:00
|
|
|
import Harness.Test.Schema qualified as Schema
|
2022-04-20 20:15:42 +03:00
|
|
|
import Harness.TestEnvironment (TestEnvironment)
|
2022-04-12 18:39:36 +03:00
|
|
|
import Hasura.Prelude (tshow)
|
2021-11-17 22:50:39 +03:00
|
|
|
import System.Process.Typed
|
|
|
|
import Prelude
|
|
|
|
|
|
|
|
-- | Check that the MySQL service is live and ready to accept connections.
|
|
|
|
livenessCheck :: HasCallStack => IO ()
|
|
|
|
livenessCheck = loop Constants.mysqlLivenessCheckAttempts
|
|
|
|
where
|
|
|
|
loop 0 = error ("Liveness check failed for MySQL.")
|
|
|
|
loop attempts =
|
|
|
|
catch
|
|
|
|
( bracket
|
|
|
|
(Mysql.connect Constants.mysqlConnectInfo)
|
|
|
|
Mysql.close
|
|
|
|
(const (pure ()))
|
|
|
|
)
|
|
|
|
( \(_failure :: ExitCodeException) -> do
|
|
|
|
threadDelay
|
|
|
|
Constants.mysqlLivenessCheckIntervalMicroseconds
|
|
|
|
loop (attempts - 1)
|
|
|
|
)
|
|
|
|
|
|
|
|
-- | Run a plain SQL string against the server, ignore the
|
|
|
|
-- result. Just checks for errors.
|
|
|
|
run_ :: HasCallStack => String -> IO ()
|
|
|
|
run_ query' =
|
|
|
|
catch
|
|
|
|
( bracket
|
|
|
|
(Mysql.connect Constants.mysqlConnectInfo)
|
|
|
|
Mysql.close
|
|
|
|
(\conn -> void (Mysql.execute_ conn (fromString query')))
|
|
|
|
)
|
|
|
|
( \(e :: SomeException) ->
|
|
|
|
error
|
|
|
|
( unlines
|
|
|
|
[ "MySQL query error:",
|
|
|
|
show e,
|
|
|
|
"SQL was:",
|
|
|
|
query'
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
2022-01-25 19:34:29 +03:00
|
|
|
|
|
|
|
-- | Metadata source information for the default Mysql instance.
|
|
|
|
defaultSourceMetadata :: Value
|
|
|
|
defaultSourceMetadata =
|
2022-03-15 19:08:47 +03:00
|
|
|
let source = defaultSource MySQL
|
|
|
|
backendType = defaultBackendTypeString MySQL
|
|
|
|
in [yaml|
|
|
|
|
name: *source
|
|
|
|
kind: *backendType
|
2022-01-25 19:34:29 +03:00
|
|
|
tables: []
|
|
|
|
configuration:
|
2022-03-15 19:08:47 +03:00
|
|
|
database: *mysqlDb
|
2022-01-25 19:34:29 +03:00
|
|
|
user: *mysqlUser
|
|
|
|
password: *mysqlPassword
|
|
|
|
host: *mysqlHost
|
|
|
|
port: *mysqlPort
|
|
|
|
pool_settings: {}
|
|
|
|
|]
|
2022-03-01 01:47:51 +03:00
|
|
|
|
|
|
|
-- | Serialize Table into a SQL statement, as needed, and execute it on the MySQL backend
|
|
|
|
createTable :: Schema.Table -> IO ()
|
2022-06-17 11:44:04 +03:00
|
|
|
createTable Schema.Table {tableUniqueConstraints = _ : _} = error "Not Implemented: MySql test harness support for Unique constraints"
|
2022-03-01 01:47:51 +03:00
|
|
|
createTable Schema.Table {tableName, tableColumns, tablePrimaryKey = pk, tableReferences} = do
|
|
|
|
run_ $
|
|
|
|
T.unpack $
|
|
|
|
T.unwords
|
|
|
|
[ "CREATE TABLE",
|
2022-03-15 19:08:47 +03:00
|
|
|
T.pack Constants.mysqlDb <> "." <> tableName,
|
2022-03-01 01:47:51 +03:00
|
|
|
"(",
|
|
|
|
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 -> "INT UNSIGNED"
|
|
|
|
Schema.TStr -> "TEXT"
|
|
|
|
Schema.TUTCTime -> "DATETIME"
|
|
|
|
Schema.TBool -> "BIT"
|
2022-04-12 18:39:36 +03:00
|
|
|
Schema.TCustomType txt -> Schema.getBackendScalarType txt bstMysql
|
2022-03-10 14:18:13 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
[ "FOREIGN KEY",
|
|
|
|
"(",
|
|
|
|
referenceLocalColumn,
|
|
|
|
")",
|
|
|
|
"REFERENCES",
|
|
|
|
referenceTargetTable,
|
|
|
|
"(",
|
|
|
|
referenceTargetColumn,
|
|
|
|
")",
|
|
|
|
"ON DELETE CASCADE",
|
|
|
|
"ON UPDATE CASCADE"
|
|
|
|
]
|
2022-03-01 01:47:51 +03:00
|
|
|
|
|
|
|
-- | Serialize tableData into an SQL insert statement and execute it.
|
|
|
|
insertTable :: Schema.Table -> IO ()
|
2022-03-15 19:08:47 +03:00
|
|
|
insertTable Schema.Table {tableName, tableColumns, tableData}
|
|
|
|
| null tableData = pure ()
|
|
|
|
| otherwise = do
|
|
|
|
run_ $
|
|
|
|
T.unpack $
|
|
|
|
T.unwords
|
|
|
|
[ "INSERT INTO",
|
|
|
|
T.pack Constants.mysqlDb <> "." <> tableName,
|
|
|
|
"(",
|
|
|
|
commaSeparated (Schema.columnName <$> tableColumns),
|
|
|
|
")",
|
|
|
|
"VALUES",
|
|
|
|
commaSeparated $ mkRow <$> tableData,
|
|
|
|
";"
|
|
|
|
]
|
2022-03-10 14:18:13 +03:00
|
|
|
|
2022-04-12 18:39:36 +03:00
|
|
|
-- | 'ScalarValue' serializer for Mysql
|
|
|
|
serialize :: ScalarValue -> Text
|
|
|
|
serialize = \case
|
|
|
|
VInt i -> tshow i
|
|
|
|
VStr s -> "'" <> replace "'" "\'" s <> "'"
|
|
|
|
VUTCTime t -> pack $ formatTime defaultTimeLocale "'%F %T'" t
|
|
|
|
VBool b -> tshow @Int $ if b then 1 else 0
|
|
|
|
VNull -> "NULL"
|
2022-04-22 13:32:35 +03:00
|
|
|
VCustomValue bsv -> Schema.formatBackendScalarValueType $ Schema.backendScalarValue bsv bsvMysql
|
2022-04-12 18:39:36 +03:00
|
|
|
|
2022-03-10 14:18:13 +03:00
|
|
|
mkRow :: [Schema.ScalarValue] -> Text
|
|
|
|
mkRow row =
|
|
|
|
T.unwords
|
|
|
|
[ "(",
|
2022-04-12 18:39:36 +03:00
|
|
|
commaSeparated $ serialize <$> row,
|
2022-03-10 14:18:13 +03:00
|
|
|
")"
|
|
|
|
]
|
2022-03-01 01:47:51 +03:00
|
|
|
|
|
|
|
-- | Serialize Table into an 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-15 19:08:47 +03:00
|
|
|
T.pack Constants.mysqlDb <> "." <> tableName,
|
2022-03-01 01:47:51 +03:00
|
|
|
";"
|
|
|
|
]
|
|
|
|
|
2022-03-15 19:08:47 +03:00
|
|
|
-- | Post an http request to start tracking the table
|
2022-04-20 20:15:42 +03:00
|
|
|
trackTable :: TestEnvironment -> Schema.Table -> IO ()
|
|
|
|
trackTable testEnvironment table =
|
|
|
|
Schema.trackTable MySQL (defaultSource MySQL) table testEnvironment
|
2022-03-15 19:08:47 +03:00
|
|
|
|
2022-03-01 01:47:51 +03:00
|
|
|
-- | Post an http request to stop tracking the table
|
2022-04-20 20:15:42 +03:00
|
|
|
untrackTable :: TestEnvironment -> Schema.Table -> IO ()
|
|
|
|
untrackTable testEnvironment table =
|
|
|
|
Schema.untrackTable MySQL (defaultSource MySQL) table testEnvironment
|
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.
|
2022-04-20 20:15:42 +03:00
|
|
|
setup :: [Schema.Table] -> (TestEnvironment, ()) -> IO ()
|
|
|
|
setup tables (testEnvironment, _) = do
|
2022-03-01 01:47:51 +03:00
|
|
|
-- Clear and reconfigure the metadata
|
2022-05-11 09:14:25 +03:00
|
|
|
GraphqlEngine.setSource testEnvironment defaultSourceMetadata Nothing
|
2022-03-01 01:47:51 +03:00
|
|
|
-- Setup and track tables
|
|
|
|
for_ tables $ \table -> do
|
|
|
|
createTable table
|
|
|
|
insertTable table
|
2022-04-20 20:15:42 +03:00
|
|
|
trackTable testEnvironment table
|
2022-03-10 14:18:13 +03:00
|
|
|
-- Setup relationships
|
|
|
|
for_ tables $ \table -> do
|
2022-04-20 20:15:42 +03:00
|
|
|
Schema.trackObjectRelationships MySQL table testEnvironment
|
|
|
|
Schema.trackArrayRelationships MySQL table testEnvironment
|
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.
|
2022-04-20 20:15:42 +03:00
|
|
|
teardown :: [Schema.Table] -> (TestEnvironment, ()) -> IO ()
|
2022-06-27 17:32:31 +03:00
|
|
|
teardown (reverse -> tables) (testEnvironment, _) = do
|
|
|
|
finally
|
|
|
|
-- Teardown relationships first
|
|
|
|
( forFinally_ tables $ \table ->
|
|
|
|
Schema.untrackRelationships MySQL table testEnvironment
|
|
|
|
)
|
|
|
|
-- Then teardown tables
|
|
|
|
( forFinally_ tables $ \table ->
|
|
|
|
finally
|
2022-04-20 20:15:42 +03:00
|
|
|
(untrackTable testEnvironment table)
|
2022-03-15 19:08:47 +03:00
|
|
|
(dropTable table)
|
2022-06-27 17:32:31 +03:00
|
|
|
)
|
2022-06-08 19:35:44 +03:00
|
|
|
|
|
|
|
setupTablesAction :: [Schema.Table] -> TestEnvironment -> SetupAction
|
|
|
|
setupTablesAction ts env =
|
|
|
|
SetupAction
|
|
|
|
(setup ts (env, ()))
|
|
|
|
(const $ teardown ts (env, ()))
|
|
|
|
|
|
|
|
setupPermissionsAction :: [Permissions.Permission] -> TestEnvironment -> SetupAction
|
|
|
|
setupPermissionsAction permissions env =
|
|
|
|
SetupAction
|
|
|
|
(setupPermissions permissions env)
|
|
|
|
(const $ teardownPermissions permissions env)
|
|
|
|
|
|
|
|
-- | Setup the given permissions to the graphql engine in a TestEnvironment.
|
|
|
|
setupPermissions :: [Permissions.Permission] -> TestEnvironment -> IO ()
|
|
|
|
setupPermissions permissions env = Permissions.setup "mysql" permissions env
|
|
|
|
|
|
|
|
-- | Remove the given permissions from the graphql engine in a TestEnvironment.
|
|
|
|
teardownPermissions :: [Permissions.Permission] -> TestEnvironment -> IO ()
|
|
|
|
teardownPermissions permissions env = Permissions.teardown "mysql" permissions env
|