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
|
|
|
|
( latestSourceCatalogVersion,
|
2022-06-29 11:18:32 +03:00
|
|
|
latestSourceCatalogVersionText,
|
|
|
|
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
|
2022-06-29 11:18:32 +03:00
|
|
|
import Hasura.Base.Error
|
2022-02-24 11:13:19 +03:00
|
|
|
import Hasura.Prelude
|
2022-06-29 11:18:32 +03:00
|
|
|
import Hasura.Server.Migrate.Version
|
2022-02-24 11:13:19 +03:00
|
|
|
|
2022-06-29 11:18:32 +03:00
|
|
|
latestSourceCatalogVersion :: CatalogVersion
|
2022-07-01 09:50:13 +03:00
|
|
|
latestSourceCatalogVersion = CatalogVersion 2
|
2022-02-24 11:13:19 +03:00
|
|
|
|
2022-04-21 10:19:37 +03:00
|
|
|
latestSourceCatalogVersionText :: Text
|
|
|
|
latestSourceCatalogVersionText = tshow latestSourceCatalogVersion
|
|
|
|
|
2022-06-29 11:18:32 +03:00
|
|
|
previousSourceCatalogVersions :: [CatalogVersion]
|
|
|
|
previousSourceCatalogVersions = [CatalogVersion 1 .. pred latestSourceCatalogVersion]
|
|
|
|
|
|
|
|
setSourceCatalogVersion :: MonadMSSQLTx m => CatalogVersion -> m ()
|
2022-07-01 09:50:13 +03:00
|
|
|
setSourceCatalogVersion (CatalogVersion version) = liftMSSQLTx $ unitQueryE HGE.defaultMSSQLTxErrorHandler setSourceCatalogVersionQuery
|
2022-02-24 11:13:19 +03:00
|
|
|
where
|
2022-07-01 09:50:13 +03:00
|
|
|
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
|
|
|
|
|]
|
2022-06-29 11:18:32 +03:00
|
|
|
setSourceCatalogVersion CatalogVersion08 =
|
|
|
|
throw500 "Cannot set the source catalog version to an unstable version."
|
2022-04-21 10:19:37 +03:00
|
|
|
|
2022-06-29 11:18:32 +03:00
|
|
|
getSourceCatalogVersion :: MonadMSSQLTx m => m CatalogVersion
|
2022-04-21 10:19:37 +03:00
|
|
|
getSourceCatalogVersion =
|
2022-06-29 11:18:32 +03:00
|
|
|
CatalogVersion <$> liftMSSQLTx (singleRowQueryE HGE.defaultMSSQLTxErrorHandler [ODBC.sql| SELECT version FROM hdb_catalog.hdb_source_catalog_version |])
|