2022-03-16 03:39:21 +03:00
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
2022-03-10 14:18:13 +03:00
|
|
|
{-# LANGUAGE ViewPatterns #-}
|
2021-12-30 14:00:52 +03:00
|
|
|
|
2022-03-16 03:39:21 +03:00
|
|
|
{-# OPTIONS -Wno-redundant-constraints #-}
|
|
|
|
|
2021-12-30 14:00:52 +03:00
|
|
|
-- | SQLServer helpers.
|
2022-01-21 10:48:27 +03:00
|
|
|
module Harness.Backend.Sqlserver
|
2021-12-30 14:00:52 +03:00
|
|
|
( livenessCheck,
|
|
|
|
run_,
|
2022-01-25 19:34:29 +03:00
|
|
|
defaultSourceMetadata,
|
2022-02-14 20:24:24 +03:00
|
|
|
defaultSourceConfiguration,
|
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.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-12-30 14:00:52 +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 (pack, unpack, unwords)
|
|
|
|
import Data.Text.Extended (commaSeparated)
|
2022-04-12 18:39:36 +03:00
|
|
|
import Data.Time (defaultTimeLocale, formatTime)
|
2021-12-30 14:00:52 +03:00
|
|
|
import Database.ODBC.SQLServer qualified as Sqlserver
|
2022-03-10 14:18:13 +03:00
|
|
|
import Harness.Constants qualified as Constants
|
2022-03-15 19:08:47 +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-03-01 01:47:51 +03:00
|
|
|
import Harness.State (State)
|
2022-03-15 19:08:47 +03:00
|
|
|
import Harness.Test.Context (BackendType (SQLServer), defaultBackendTypeString, defaultSource)
|
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-12 18:39:36 +03:00
|
|
|
import Hasura.Prelude (tshow)
|
2021-12-30 14:00:52 +03:00
|
|
|
import System.Process.Typed
|
|
|
|
import Prelude
|
|
|
|
|
|
|
|
-- | Check that the SQLServer service is live and ready to accept connections.
|
|
|
|
livenessCheck :: HasCallStack => IO ()
|
|
|
|
livenessCheck = loop Constants.sqlserverLivenessCheckAttempts
|
|
|
|
where
|
|
|
|
loop 0 = error ("Liveness check failed for SQLServer.")
|
|
|
|
loop attempts =
|
|
|
|
catch
|
|
|
|
( bracket
|
|
|
|
(Sqlserver.connect Constants.sqlserverConnectInfo)
|
|
|
|
Sqlserver.close
|
|
|
|
(const (pure ()))
|
|
|
|
)
|
|
|
|
( \(_failure :: ExitCodeException) -> do
|
|
|
|
threadDelay
|
|
|
|
Constants.sqlserverLivenessCheckIntervalMicroseconds
|
|
|
|
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
|
|
|
|
(Sqlserver.connect Constants.sqlserverConnectInfo)
|
|
|
|
Sqlserver.close
|
|
|
|
(\conn -> void (Sqlserver.exec conn (fromString query')))
|
|
|
|
)
|
|
|
|
( \(e :: SomeException) ->
|
|
|
|
error
|
|
|
|
( unlines
|
|
|
|
[ "SQLServer query error:",
|
|
|
|
show e,
|
|
|
|
"SQL was:",
|
|
|
|
query'
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
2022-01-25 19:34:29 +03:00
|
|
|
|
|
|
|
-- | Metadata source information for the default MSSQL instance.
|
|
|
|
defaultSourceMetadata :: Value
|
|
|
|
defaultSourceMetadata =
|
2022-03-15 19:08:47 +03:00
|
|
|
let source = defaultSource SQLServer
|
|
|
|
backendType = defaultBackendTypeString SQLServer
|
|
|
|
in [yaml|
|
|
|
|
name: *source
|
|
|
|
kind: *backendType
|
2022-01-25 19:34:29 +03:00
|
|
|
tables: []
|
2022-02-14 20:24:24 +03:00
|
|
|
configuration: *defaultSourceConfiguration
|
2022-01-25 19:34:29 +03:00
|
|
|
|]
|
2022-02-14 20:24:24 +03:00
|
|
|
|
|
|
|
defaultSourceConfiguration :: Value
|
|
|
|
defaultSourceConfiguration =
|
|
|
|
[yaml|
|
|
|
|
connection_info:
|
|
|
|
database_url: *sqlserverConnectInfo
|
|
|
|
pool_settings: {}
|
|
|
|
|]
|
2022-03-10 14:18:13 +03:00
|
|
|
where
|
|
|
|
sqlserverConnectInfo = Constants.sqlserverConnectInfo
|
2022-03-01 01:47:51 +03:00
|
|
|
|
|
|
|
-- | Serialize Table into a T-SQL statement, as needed, and execute it on the Sqlserver backend
|
|
|
|
createTable :: Schema.Table -> IO ()
|
|
|
|
createTable Schema.Table {tableName, tableColumns, tablePrimaryKey = pk, tableReferences} = do
|
|
|
|
run_ $
|
|
|
|
T.unpack $
|
|
|
|
T.unwords
|
|
|
|
[ "CREATE TABLE",
|
|
|
|
T.pack Constants.sqlserverDb <> "." <> 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 -> "INT"
|
|
|
|
Schema.TStr -> "NVARCHAR(127)"
|
|
|
|
Schema.TUTCTime -> "DATETIME"
|
|
|
|
Schema.TBool -> "BOOLEAN"
|
2022-04-12 18:39:36 +03:00
|
|
|
Schema.TCustomType txt -> Schema.getBackendScalarType txt bstMssql
|
2022-03-10 14:18:13 +03:00
|
|
|
|
|
|
|
mkColumn :: Schema.Column -> Text
|
|
|
|
mkColumn Schema.Column {columnName, columnType, columnNullable, columnDefault} =
|
|
|
|
T.unwords
|
2022-03-18 13:04:52 +03:00
|
|
|
[ wrapIdentifier columnName,
|
2022-03-10 14:18:13 +03:00
|
|
|
scalarType columnType,
|
|
|
|
bool "NOT NULL" "DEFAULT NULL" columnNullable,
|
|
|
|
maybe "" ("DEFAULT " <>) columnDefault
|
|
|
|
]
|
|
|
|
|
|
|
|
mkPrimaryKey :: [Text] -> Text
|
|
|
|
mkPrimaryKey key =
|
|
|
|
T.unwords
|
|
|
|
[ "PRIMARY KEY",
|
|
|
|
"(",
|
2022-03-18 13:04:52 +03:00
|
|
|
commaSeparated $ map wrapIdentifier key,
|
2022-03-10 14:18:13 +03:00
|
|
|
")"
|
|
|
|
]
|
|
|
|
|
|
|
|
mkReference :: Schema.Reference -> Text
|
|
|
|
mkReference Schema.Reference {referenceLocalColumn, referenceTargetTable, referenceTargetColumn} =
|
|
|
|
T.unwords
|
|
|
|
[ "FOREIGN KEY",
|
|
|
|
"(",
|
2022-03-18 13:04:52 +03:00
|
|
|
wrapIdentifier referenceLocalColumn,
|
2022-03-10 14:18:13 +03:00
|
|
|
")",
|
|
|
|
"REFERENCES",
|
2022-04-05 13:09:35 +03:00
|
|
|
T.pack Constants.sqlserverDb <> "." <> referenceTargetTable,
|
2022-03-10 14:18:13 +03:00
|
|
|
"(",
|
2022-03-18 13:04:52 +03:00
|
|
|
wrapIdentifier referenceTargetColumn,
|
2022-03-10 14:18:13 +03:00
|
|
|
")",
|
|
|
|
"ON DELETE CASCADE",
|
|
|
|
"ON UPDATE CASCADE"
|
|
|
|
]
|
2022-03-01 01:47:51 +03:00
|
|
|
|
|
|
|
-- | Serialize tableData into a T-SQL insert statement and execute it.
|
2022-03-15 19:08:47 +03:00
|
|
|
insertTable :: HasCallStack => Schema.Table -> IO ()
|
|
|
|
insertTable Schema.Table {tableName, tableColumns, tableData}
|
|
|
|
| null tableData = pure ()
|
|
|
|
| otherwise = do
|
|
|
|
run_ $
|
|
|
|
T.unpack $
|
|
|
|
T.unwords
|
|
|
|
[ "INSERT INTO",
|
2022-03-18 13:04:52 +03:00
|
|
|
T.pack Constants.sqlserverDb <> "." <> wrapIdentifier tableName,
|
2022-03-15 19:08:47 +03:00
|
|
|
"(",
|
2022-03-18 13:04:52 +03:00
|
|
|
commaSeparated (wrapIdentifier . Schema.columnName <$> tableColumns),
|
2022-03-15 19:08:47 +03:00
|
|
|
")",
|
|
|
|
"VALUES",
|
|
|
|
commaSeparated $ mkRow <$> tableData,
|
|
|
|
";"
|
|
|
|
]
|
2022-03-10 14:18:13 +03:00
|
|
|
|
2022-03-18 13:04:52 +03:00
|
|
|
-- | MSSQL identifiers which may contain spaces or be case-sensitive needs to be wrapped in @[]@.
|
|
|
|
--
|
|
|
|
-- More information can be found in the mssql docs:
|
|
|
|
-- https://docs.microsoft.com/en-us/sql/relational-databases/databases/database-identifiers
|
|
|
|
wrapIdentifier :: Text -> Text
|
|
|
|
wrapIdentifier identifier = "[" <> identifier <> "]"
|
|
|
|
|
2022-04-12 18:39:36 +03:00
|
|
|
-- | 'ScalarValue' serializer for Mssql
|
|
|
|
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-19 18:39:02 +03:00
|
|
|
VCustomValue bsv -> Schema.formatBackendScalarValueType $ Schema.backendScalarValue bsv bsvMssql
|
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 a T-SQL DROP statement and execute it
|
2022-03-15 19:08:47 +03:00
|
|
|
dropTable :: HasCallStack => Schema.Table -> IO ()
|
2022-03-01 01:47:51 +03:00
|
|
|
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.sqlserverDb <> "." <> tableName,
|
|
|
|
";"
|
|
|
|
]
|
|
|
|
|
2022-03-15 19:08:47 +03:00
|
|
|
-- | Post an http request to start tracking the table
|
|
|
|
trackTable :: HasCallStack => State -> Schema.Table -> IO ()
|
|
|
|
trackTable state table =
|
|
|
|
Schema.trackTable SQLServer (defaultSource SQLServer) table state
|
|
|
|
|
2022-03-01 01:47:51 +03:00
|
|
|
-- | Post an http request to stop tracking the table
|
2022-03-15 19:08:47 +03:00
|
|
|
untrackTable :: HasCallStack => State -> Schema.Table -> IO ()
|
|
|
|
untrackTable state table =
|
|
|
|
Schema.untrackTable SQLServer (defaultSource SQLServer) 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.
|
2022-03-15 19:08:47 +03:00
|
|
|
setup :: HasCallStack => [Schema.Table] -> (State, ()) -> IO ()
|
2022-03-01 01:47:51 +03:00
|
|
|
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
|
2022-03-15 19:08:47 +03:00
|
|
|
Schema.trackObjectRelationships SQLServer table state
|
|
|
|
Schema.trackArrayRelationships SQLServer 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.
|
2022-03-15 19:08:47 +03:00
|
|
|
teardown :: HasCallStack => [Schema.Table] -> (State, ()) -> IO ()
|
|
|
|
teardown tables (state, _) = do
|
2022-04-04 17:45:12 +03:00
|
|
|
forFinally_ (reverse tables) $ \table ->
|
2022-03-15 19:08:47 +03:00
|
|
|
finally
|
|
|
|
(Schema.untrackRelationships SQLServer table state)
|
|
|
|
( finally
|
|
|
|
(untrackTable state table)
|
|
|
|
(dropTable table)
|
|
|
|
)
|