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-02-24 11:13:19 +03:00
|
|
|
initCatalogForSource,
|
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-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
|
|
|
|
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
|
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 ->
|
|
|
|
Env.Environment ->
|
|
|
|
m (Either QErr MSSQLSourceConfig)
|
2022-01-04 14:53:50 +03:00
|
|
|
resolveSourceConfig name config _env = runExceptT do
|
|
|
|
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 )
|
|
|
|
THEN 1
|
|
|
|
ELSE 0
|
|
|
|
END
|
|
|
|
AS BIT)
|
|
|
|
|]
|
|
|
|
|
|
|
|
-- | Initialise catalog tables for a source, including those required by the event delivery subsystem.
|
|
|
|
initCatalogForSource :: MonadMSSQLTx m => m RecreateEventTriggers
|
|
|
|
initCatalogForSource = do
|
|
|
|
hdbCatalogExist <- doesSchemaExist "hdb_catalog"
|
|
|
|
|
|
|
|
if
|
|
|
|
-- Fresh database
|
|
|
|
| not hdbCatalogExist -> liftMSSQLTx do
|
|
|
|
unitQueryE HGE.defaultMSSQLTxErrorHandler "CREATE SCHEMA hdb_catalog"
|
|
|
|
initSourceCatalog
|
|
|
|
return RETDoNothing
|
|
|
|
-- TODO: When we need to make any changes to the source catalog, we'll have to introduce code which which will migrate
|
|
|
|
-- from one source catalog version to the next one
|
|
|
|
| otherwise -> pure RETDoNothing
|
|
|
|
where
|
|
|
|
initSourceCatalog = do
|
|
|
|
unitQueryE HGE.defaultMSSQLTxErrorHandler $(makeRelativeToProject "src-rsr/init_mssql_source.sql" >>= ODBC.sqlFile)
|
|
|
|
setSourceCatalogVersion latestSourceCatalogVersion
|
|
|
|
|
|
|
|
dropSourceCatalog :: MonadMSSQLTx m => m ()
|
|
|
|
dropSourceCatalog = do
|
|
|
|
let sql = $(makeRelativeToProject "src-rsr/drop_mssql_source.sql" >>= ODBC.sqlFile)
|
|
|
|
liftMSSQLTx $ unitQueryE HGE.defaultMSSQLTxErrorHandler sql
|