2022-03-16 03:39:21 +03:00
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
|
2022-02-08 12:24:34 +03:00
|
|
|
-- | Postgres DDL Source Version
|
|
|
|
--
|
|
|
|
-- Deals with catalog version - used by 'Hasura.Backends.Postgres.DDL.Source'.
|
2021-11-04 19:08:33 +03:00
|
|
|
module Hasura.Backends.Postgres.DDL.Source.Version
|
2022-07-04 12:30:53 +03:00
|
|
|
( SourceCatalogVersion,
|
|
|
|
initialSourceCatalogVersion,
|
|
|
|
latestSourceCatalogVersion,
|
2022-06-29 11:18:32 +03:00
|
|
|
previousSourceCatalogVersions,
|
|
|
|
getSourceCatalogVersion,
|
2021-11-04 19:08:33 +03:00
|
|
|
setSourceCatalogVersion,
|
|
|
|
)
|
|
|
|
where
|
2021-06-01 20:33:25 +03:00
|
|
|
|
2022-07-04 12:30:53 +03:00
|
|
|
import Data.Text qualified as T
|
2021-09-24 01:56:37 +03:00
|
|
|
import Database.PG.Query qualified as Q
|
|
|
|
import Hasura.Backends.Postgres.Connection
|
2022-07-04 12:30:53 +03:00
|
|
|
import Hasura.Base.Error
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Prelude
|
2022-07-04 12:30:53 +03:00
|
|
|
import Hasura.SQL.Backend (BackendType (Postgres), PostgresKind)
|
|
|
|
import Hasura.Server.Migrate.Version qualified as Version
|
2021-06-01 20:33:25 +03:00
|
|
|
|
2022-07-04 12:30:53 +03:00
|
|
|
type SourceCatalogVersion (pgKind :: PostgresKind) = Version.SourceCatalogVersion ('Postgres pgKind)
|
2021-06-01 20:33:25 +03:00
|
|
|
|
2022-07-04 12:30:53 +03:00
|
|
|
initialSourceCatalogVersion :: SourceCatalogVersion pgKind
|
|
|
|
initialSourceCatalogVersion = Version.SourceCatalogVersion 0
|
2021-06-01 20:33:25 +03:00
|
|
|
|
2022-07-04 12:30:53 +03:00
|
|
|
latestSourceCatalogVersion :: SourceCatalogVersion pgKind
|
|
|
|
latestSourceCatalogVersion = Version.SourceCatalogVersion 2
|
|
|
|
|
|
|
|
previousSourceCatalogVersions :: [SourceCatalogVersion pgKind]
|
|
|
|
previousSourceCatalogVersions = [initialSourceCatalogVersion .. pred latestSourceCatalogVersion]
|
2022-06-29 11:18:32 +03:00
|
|
|
|
2021-06-01 20:33:25 +03:00
|
|
|
setSourceCatalogVersion :: MonadTx m => m ()
|
2021-09-24 01:56:37 +03:00
|
|
|
setSourceCatalogVersion =
|
|
|
|
liftTx $
|
|
|
|
Q.unitQE
|
|
|
|
defaultTxErrorHandler
|
|
|
|
[Q.sql|
|
2022-07-04 12:30:53 +03:00
|
|
|
INSERT INTO hdb_catalog.hdb_source_catalog_version(version, upgraded_on)
|
|
|
|
VALUES ($1, NOW())
|
|
|
|
ON CONFLICT ((version IS NOT NULL))
|
|
|
|
DO UPDATE SET version = $1, upgraded_on = NOW()
|
|
|
|
|]
|
|
|
|
(Identity (tshow latestSourceCatalogVersion))
|
2021-09-24 01:56:37 +03:00
|
|
|
False
|
2021-06-01 20:33:25 +03:00
|
|
|
|
2022-07-04 12:30:53 +03:00
|
|
|
getSourceCatalogVersion :: MonadTx m => m (SourceCatalogVersion postgres)
|
|
|
|
getSourceCatalogVersion = do
|
|
|
|
versionText <-
|
|
|
|
liftTx $
|
|
|
|
runIdentity . Q.getRow
|
|
|
|
<$> Q.withQE
|
|
|
|
defaultTxErrorHandler
|
|
|
|
[Q.sql| SELECT version FROM hdb_catalog.hdb_source_catalog_version |]
|
|
|
|
()
|
|
|
|
False
|
|
|
|
readEither (T.unpack versionText) `onLeft` (throw500 . (("Invalid source catalog version in the metadata: " <>) . T.pack))
|