2022-03-16 03:39:21 +03:00
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
|
2022-01-11 01:54:51 +03:00
|
|
|
-- | MSSQL Source
|
|
|
|
--
|
|
|
|
-- Implements the Source related methods of the
|
|
|
|
-- 'Hasura.RQL.Types.Metadata.Backend.BackendMetadata' type class
|
|
|
|
-- for the MSSQL backend, which provides an interface for identifying the
|
|
|
|
-- MSSQL database instance (source) and manipulate it.
|
|
|
|
--
|
|
|
|
-- The actual instance is defined in "Hasura.Backends.MSSQL.Instances.Metadata".
|
2021-02-23 20:37:27 +03:00
|
|
|
module Hasura.Backends.MSSQL.DDL.Source
|
2021-09-24 01:56:37 +03:00
|
|
|
( resolveSourceConfig,
|
|
|
|
resolveDatabaseMetadata,
|
|
|
|
postDropSourceHook,
|
2022-05-05 16:43:50 +03:00
|
|
|
prepareCatalog,
|
2021-02-23 20:37:27 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2021-10-22 17:49:15 +03:00
|
|
|
import Control.Monad.Trans.Control (MonadBaseControl)
|
2021-09-24 01:56:37 +03:00
|
|
|
import Data.Environment qualified as Env
|
2022-02-24 11:13:19 +03:00
|
|
|
import Data.FileEmbed (makeRelativeToProject)
|
|
|
|
import Database.MSSQL.Transaction
|
|
|
|
import Database.MSSQL.Transaction qualified as Tx
|
|
|
|
import Database.ODBC.SQLServer
|
|
|
|
import Database.ODBC.TH qualified as ODBC
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Backends.MSSQL.Connection
|
2022-04-21 10:19:37 +03:00
|
|
|
import Hasura.Backends.MSSQL.DDL.EventTrigger
|
2022-02-24 11:13:19 +03:00
|
|
|
import Hasura.Backends.MSSQL.DDL.Source.Version
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Backends.MSSQL.Meta
|
2022-02-24 11:13:19 +03:00
|
|
|
import Hasura.Backends.MSSQL.SQL.Error qualified as HGE
|
|
|
|
import Hasura.Backends.MSSQL.Types
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.Prelude
|
2022-04-29 05:13:13 +03:00
|
|
|
import Hasura.RQL.Types.Backend (BackendConfig)
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.RQL.Types.Common
|
2022-02-24 11:13:19 +03:00
|
|
|
import Hasura.RQL.Types.EventTrigger (RecreateEventTriggers (..))
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.RQL.Types.Source
|
2021-10-29 17:42:07 +03:00
|
|
|
import Hasura.RQL.Types.SourceCustomization
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.SQL.Backend
|
2022-04-21 10:19:37 +03:00
|
|
|
import Language.Haskell.TH.Lib qualified as TH
|
|
|
|
import Language.Haskell.TH.Syntax qualified as TH
|
|
|
|
import Text.Shakespeare.Text qualified as ST
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
resolveSourceConfig ::
|
2022-01-04 14:53:50 +03:00
|
|
|
(MonadIO m, MonadResolveSource m) =>
|
2021-09-24 01:56:37 +03:00
|
|
|
SourceName ->
|
|
|
|
MSSQLConnConfiguration ->
|
2022-04-29 05:13:13 +03:00
|
|
|
BackendSourceKind 'MSSQL ->
|
|
|
|
BackendConfig 'MSSQL ->
|
2021-09-24 01:56:37 +03:00
|
|
|
Env.Environment ->
|
|
|
|
m (Either QErr MSSQLSourceConfig)
|
2022-04-29 05:13:13 +03:00
|
|
|
resolveSourceConfig name config _backendKind _backendConfig _env = runExceptT do
|
2022-01-04 14:53:50 +03:00
|
|
|
sourceResolver <- getMSSQLSourceResolver
|
|
|
|
liftEitherM $ liftIO $ sourceResolver name config
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
resolveDatabaseMetadata ::
|
2021-10-22 17:49:15 +03:00
|
|
|
(MonadIO m, MonadBaseControl IO m) =>
|
2021-09-24 01:56:37 +03:00
|
|
|
MSSQLSourceConfig ->
|
2021-10-29 17:42:07 +03:00
|
|
|
SourceTypeCustomization ->
|
2021-09-24 01:56:37 +03:00
|
|
|
m (Either QErr (ResolvedSource 'MSSQL))
|
2021-10-29 17:42:07 +03:00
|
|
|
resolveDatabaseMetadata config customization = runExceptT do
|
2022-01-14 17:08:17 +03:00
|
|
|
dbTablesMetadata <- mssqlRunReadOnly mssqlExecCtx $ loadDBMetadata
|
2021-10-29 17:42:07 +03:00
|
|
|
pure $ ResolvedSource config customization dbTablesMetadata mempty mempty
|
2021-02-23 20:37:27 +03:00
|
|
|
where
|
2022-01-04 14:53:50 +03:00
|
|
|
MSSQLSourceConfig _connString mssqlExecCtx = config
|
2021-02-23 20:37:27 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
postDropSourceHook ::
|
2022-02-24 11:13:19 +03:00
|
|
|
(MonadIO m, MonadBaseControl IO m) =>
|
2021-09-24 01:56:37 +03:00
|
|
|
MSSQLSourceConfig ->
|
|
|
|
m ()
|
2022-01-04 14:53:50 +03:00
|
|
|
postDropSourceHook (MSSQLSourceConfig _ mssqlExecCtx) = do
|
2022-02-24 11:13:19 +03:00
|
|
|
_ <- runExceptT $ mssqlRunReadWrite mssqlExecCtx dropSourceCatalog
|
2021-02-23 20:37:27 +03:00
|
|
|
-- Close the connection
|
2022-01-04 14:53:50 +03:00
|
|
|
liftIO $ mssqlDestroyConn mssqlExecCtx
|
2022-02-24 11:13:19 +03:00
|
|
|
|
|
|
|
doesSchemaExist :: MonadMSSQLTx m => SchemaName -> m Bool
|
|
|
|
doesSchemaExist (SchemaName schemaName) = do
|
|
|
|
liftMSSQLTx $
|
|
|
|
Tx.singleRowQueryE
|
|
|
|
HGE.defaultMSSQLTxErrorHandler
|
|
|
|
[ODBC.sql|
|
|
|
|
SELECT CAST (
|
|
|
|
CASE
|
|
|
|
WHEN EXISTS( SELECT 1 FROM sys.schemas WHERE name = $schemaName )
|
2022-04-29 05:13:13 +03:00
|
|
|
THEN 1
|
|
|
|
ELSE 0
|
2022-02-24 11:13:19 +03:00
|
|
|
END
|
|
|
|
AS BIT)
|
|
|
|
|]
|
|
|
|
|
2022-04-21 10:19:37 +03:00
|
|
|
doesTableExist :: MonadMSSQLTx m => TableName -> m Bool
|
|
|
|
doesTableExist tableName = do
|
|
|
|
liftMSSQLTx $
|
|
|
|
Tx.singleRowQueryE
|
|
|
|
HGE.defaultMSSQLTxErrorHandler
|
|
|
|
[ODBC.sql|
|
|
|
|
SELECT CAST (
|
|
|
|
CASE
|
|
|
|
WHEN (Select OBJECT_ID($qualifiedTable)) IS NOT NULL
|
2022-04-29 05:13:13 +03:00
|
|
|
THEN 1
|
|
|
|
ELSE 0
|
2022-04-21 10:19:37 +03:00
|
|
|
END
|
|
|
|
AS BIT)
|
|
|
|
|]
|
|
|
|
where
|
|
|
|
qualifiedTable = qualifyTableName tableName
|
|
|
|
|
2022-02-24 11:13:19 +03:00
|
|
|
-- | Initialise catalog tables for a source, including those required by the event delivery subsystem.
|
2022-05-05 16:43:50 +03:00
|
|
|
prepareCatalog ::
|
|
|
|
(MonadIO m, MonadBaseControl IO m) =>
|
|
|
|
MSSQLSourceConfig ->
|
|
|
|
ExceptT QErr m RecreateEventTriggers
|
|
|
|
prepareCatalog sourceConfig = mssqlRunReadWrite (_mscExecCtx sourceConfig) do
|
2022-02-24 11:13:19 +03:00
|
|
|
hdbCatalogExist <- doesSchemaExist "hdb_catalog"
|
2022-04-21 10:19:37 +03:00
|
|
|
eventLogTableExist <- doesTableExist $ TableName "event_log" "hdb_catalog"
|
|
|
|
sourceVersionTableExist <- doesTableExist $ TableName "hdb_source_catalog_version" "hdb_catalog"
|
2022-02-24 11:13:19 +03:00
|
|
|
if
|
|
|
|
-- Fresh database
|
|
|
|
| not hdbCatalogExist -> liftMSSQLTx do
|
|
|
|
unitQueryE HGE.defaultMSSQLTxErrorHandler "CREATE SCHEMA hdb_catalog"
|
|
|
|
initSourceCatalog
|
|
|
|
return RETDoNothing
|
2022-04-21 10:19:37 +03:00
|
|
|
-- Only 'hdb_catalog' schema defined
|
|
|
|
| not sourceVersionTableExist && not eventLogTableExist -> do
|
|
|
|
liftMSSQLTx initSourceCatalog
|
|
|
|
return RETDoNothing
|
|
|
|
| otherwise -> migrateSourceCatalog
|
2022-02-24 11:13:19 +03:00
|
|
|
where
|
|
|
|
initSourceCatalog = do
|
2022-04-21 10:19:37 +03:00
|
|
|
unitQueryE HGE.defaultMSSQLTxErrorHandler $(makeRelativeToProject "src-rsr/mssql/init_mssql_source.sql" >>= ODBC.sqlFile)
|
2022-02-24 11:13:19 +03:00
|
|
|
setSourceCatalogVersion latestSourceCatalogVersion
|
|
|
|
|
|
|
|
dropSourceCatalog :: MonadMSSQLTx m => m ()
|
|
|
|
dropSourceCatalog = do
|
2022-04-21 10:19:37 +03:00
|
|
|
let sql = $(makeRelativeToProject "src-rsr/mssql/drop_mssql_source.sql" >>= ODBC.sqlFile)
|
2022-02-24 11:13:19 +03:00
|
|
|
liftMSSQLTx $ unitQueryE HGE.defaultMSSQLTxErrorHandler sql
|
2022-04-21 10:19:37 +03:00
|
|
|
|
|
|
|
migrateSourceCatalog :: MonadMSSQLTx m => m RecreateEventTriggers
|
|
|
|
migrateSourceCatalog =
|
|
|
|
getSourceCatalogVersion >>= migrateSourceCatalogFrom
|
|
|
|
|
|
|
|
migrateSourceCatalogFrom :: MonadMSSQLTx m => Int -> m RecreateEventTriggers
|
|
|
|
migrateSourceCatalogFrom prevVersion
|
|
|
|
| prevVersion == latestSourceCatalogVersion = pure RETDoNothing
|
|
|
|
| [] <- neededMigrations =
|
|
|
|
throw400 NotSupported $
|
|
|
|
"Expected source catalog version <= "
|
|
|
|
<> latestSourceCatalogVersionText
|
|
|
|
<> ", but the current version is "
|
|
|
|
<> (tshow prevVersion)
|
|
|
|
| otherwise = do
|
|
|
|
liftMSSQLTx $ traverse_ snd neededMigrations
|
|
|
|
setSourceCatalogVersion latestSourceCatalogVersion
|
|
|
|
pure RETRecreate
|
|
|
|
where
|
|
|
|
neededMigrations =
|
|
|
|
dropWhile ((/= prevVersion) . fst) sourceMigrations
|
|
|
|
|
|
|
|
sourceMigrations :: [(Int, TxE QErr ())]
|
|
|
|
sourceMigrations =
|
|
|
|
$( let migrationFromFile from =
|
|
|
|
let to = from + 1
|
|
|
|
path = "src-rsr/mssql_source_migrations/" <> show from <> "_to_" <> show to <> ".sql"
|
|
|
|
in [|multiRowQueryE defaultTxErrorHandler $(makeRelativeToProject path >>= ST.stextFile)|]
|
|
|
|
|
|
|
|
migrationsFromFile = map $ \(from :: Int) ->
|
|
|
|
[|($(TH.lift $ from), $(migrationFromFile from))|]
|
|
|
|
in TH.listE $ migrationsFromFile [1 .. (latestSourceCatalogVersion - 1)]
|
|
|
|
)
|