graphql-engine/server/src-lib/Hasura/Backends/MSSQL/DDL/Source/Version.hs
paritosh-08 db710d38b7 server, pro: event trigger auto cleanup (increment 2)
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5746
Co-authored-by: pranshi06 <85474619+pranshi06@users.noreply.github.com>
Co-authored-by: Puru Gupta <32328846+purugupta99@users.noreply.github.com>
Co-authored-by: Karthikeyan Chinnakonda <15602904+codingkarthik@users.noreply.github.com>
GitOrigin-RevId: f4e5e06389ca57bdb5f7771f459c07418787111d
2022-09-13 08:34:52 +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.SQL.Backend (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 3
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 |])