2022-03-16 03:39:21 +03:00
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
|
2022-02-24 11:13:19 +03:00
|
|
|
module Hasura.Backends.MSSQL.DDL.Source.Version
|
2022-07-04 12:30:53 +03:00
|
|
|
( SourceCatalogVersion,
|
|
|
|
initialSourceCatalogVersion,
|
|
|
|
latestSourceCatalogVersion,
|
2022-06-29 11:18:32 +03:00
|
|
|
previousSourceCatalogVersions,
|
2022-02-24 11:13:19 +03:00
|
|
|
setSourceCatalogVersion,
|
2022-04-21 10:19:37 +03:00
|
|
|
getSourceCatalogVersion,
|
2022-02-24 11:13:19 +03:00
|
|
|
)
|
|
|
|
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
|
2023-04-24 21:35:48 +03:00
|
|
|
import Hasura.RQL.Types.BackendType (BackendType (MSSQL))
|
2022-07-04 12:30:53 +03:00
|
|
|
import Hasura.Server.Migrate.Version qualified as Version
|
2022-02-24 11:13:19 +03:00
|
|
|
|
2022-07-04 12:30:53 +03:00
|
|
|
type SourceCatalogVersion = Version.SourceCatalogVersion 'MSSQL
|
2022-02-24 11:13:19 +03:00
|
|
|
|
2022-07-04 12:30:53 +03:00
|
|
|
initialSourceCatalogVersion :: SourceCatalogVersion
|
|
|
|
initialSourceCatalogVersion = Version.SourceCatalogVersion 1
|
2022-04-21 10:19:37 +03:00
|
|
|
|
2022-07-04 12:30:53 +03:00
|
|
|
latestSourceCatalogVersion :: SourceCatalogVersion
|
2023-01-20 14:15:04 +03:00
|
|
|
latestSourceCatalogVersion = Version.SourceCatalogVersion 4
|
2022-06-29 11:18:32 +03:00
|
|
|
|
2022-07-04 12:30:53 +03:00
|
|
|
previousSourceCatalogVersions :: [SourceCatalogVersion]
|
|
|
|
previousSourceCatalogVersions = [initialSourceCatalogVersion .. pred latestSourceCatalogVersion]
|
|
|
|
|
2023-05-24 16:51:56 +03:00
|
|
|
setSourceCatalogVersion :: (MonadMSSQLTx m) => SourceCatalogVersion -> m ()
|
2022-07-04 12:30:53 +03:00
|
|
|
setSourceCatalogVersion (Version.SourceCatalogVersion version) =
|
|
|
|
liftMSSQLTx $ unitQueryE HGE.defaultMSSQLTxErrorHandler setSourceCatalogVersionQuery
|
2022-02-24 11:13:19 +03:00
|
|
|
where
|
2022-07-01 09:50:13 +03:00
|
|
|
setSourceCatalogVersionQuery =
|
2022-07-04 12:30:53 +03:00
|
|
|
[ODBC.sql|
|
2022-07-01 09:50:13 +03:00
|
|
|
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
|
|
|
|
|]
|
2022-04-21 10:19:37 +03:00
|
|
|
|
2023-05-24 16:51:56 +03:00
|
|
|
getSourceCatalogVersion :: (MonadMSSQLTx m) => m SourceCatalogVersion
|
2022-04-21 10:19:37 +03:00
|
|
|
getSourceCatalogVersion =
|
2022-07-04 12:30:53 +03:00
|
|
|
Version.SourceCatalogVersion
|
|
|
|
<$> liftMSSQLTx (singleRowQueryE HGE.defaultMSSQLTxErrorHandler [ODBC.sql| SELECT version FROM hdb_catalog.hdb_source_catalog_version |])
|