graphql-engine/server/src-lib/Hasura/Backends/MSSQL/DDL/Source/Version.hs
Tom Harding e0c0043e76 Upgrade Ormolu to 0.7.0.0
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/9284
GitOrigin-RevId: 2f2cf2ad01900a54e4bdb970205ac0ef313c7e00
2023-05-24 13:53:53 +00:00

57 lines
2.1 KiB
Haskell

{-# LANGUAGE QuasiQuotes #-}
module Hasura.Backends.MSSQL.DDL.Source.Version
( SourceCatalogVersion,
initialSourceCatalogVersion,
latestSourceCatalogVersion,
previousSourceCatalogVersions,
setSourceCatalogVersion,
getSourceCatalogVersion,
)
where
import Database.MSSQL.Transaction
import Database.ODBC.SQLServer
import Database.ODBC.TH qualified as ODBC
import Hasura.Backends.MSSQL.Connection (MonadMSSQLTx (..))
import Hasura.Backends.MSSQL.SQL.Error qualified as HGE
import Hasura.Prelude
import Hasura.RQL.Types.BackendType (BackendType (MSSQL))
import Hasura.Server.Migrate.Version qualified as Version
type SourceCatalogVersion = Version.SourceCatalogVersion 'MSSQL
initialSourceCatalogVersion :: SourceCatalogVersion
initialSourceCatalogVersion = Version.SourceCatalogVersion 1
latestSourceCatalogVersion :: SourceCatalogVersion
latestSourceCatalogVersion = Version.SourceCatalogVersion 4
previousSourceCatalogVersions :: [SourceCatalogVersion]
previousSourceCatalogVersions = [initialSourceCatalogVersion .. pred latestSourceCatalogVersion]
setSourceCatalogVersion :: (MonadMSSQLTx m) => SourceCatalogVersion -> m ()
setSourceCatalogVersion (Version.SourceCatalogVersion version) =
liftMSSQLTx $ unitQueryE HGE.defaultMSSQLTxErrorHandler setSourceCatalogVersionQuery
where
setSourceCatalogVersionQuery =
[ODBC.sql|
BEGIN TRANSACTION
IF EXISTS (select 1 from hdb_catalog.hdb_source_catalog_version WITH (UPDLOCK,SERIALIZABLE))
BEGIN
UPDATE hdb_catalog.hdb_source_catalog_version
SET version = $version, upgraded_on = SYSDATETIMEOFFSET()
END
ELSE
BEGIN
INSERT INTO hdb_catalog.hdb_source_catalog_version (version, upgraded_on)
values ($version, SYSDATETIMEOFFSET())
END
COMMIT TRANSACTION
|]
getSourceCatalogVersion :: (MonadMSSQLTx m) => m SourceCatalogVersion
getSourceCatalogVersion =
Version.SourceCatalogVersion
<$> liftMSSQLTx (singleRowQueryE HGE.defaultMSSQLTxErrorHandler [ODBC.sql| SELECT version FROM hdb_catalog.hdb_source_catalog_version |])