graphql-engine/server/src-lib/Hasura/RQL/DDL/Schema/Catalog.hs
Rakesh Emmadi a2cf9a53c2 server: move to storing metadata as a json blob (#115)
GitOrigin-RevId: 3d1a7618a4ec086c2d255549a6c15087201e9ab0
2020-12-08 14:23:28 +00:00

34 lines
1.1 KiB
Haskell

-- | Functions for fetching and updating @'Metadata' in the catalog.
module Hasura.RQL.DDL.Schema.Catalog
( fetchMetadataFromCatalog
, setMetadataInCatalog
) where
import Hasura.Prelude
import qualified Database.PG.Query as Q
import Hasura.Backends.Postgres.Connection
import Hasura.RQL.Types.Error
import Hasura.RQL.Types.Metadata
fetchMetadataFromCatalog :: Q.TxE QErr Metadata
fetchMetadataFromCatalog = do
rows <- Q.withQE defaultTxErrorHandler
[Q.sql|
SELECT metadata from hdb_catalog.hdb_metadata where id = 1
|] () True
case rows of
[] -> pure emptyMetadata
[(Identity (Q.AltJ metadata))] -> pure metadata
_ -> throw500 "multiple rows in hdb_metadata table"
setMetadataInCatalog :: Metadata -> Q.TxE QErr ()
setMetadataInCatalog metadata =
Q.unitQE defaultTxErrorHandler
[Q.sql|
INSERT INTO hdb_catalog.hdb_metadata
(id, metadata) VALUES (1, $1::json)
ON CONFLICT (id) DO UPDATE SET metadata = $1::json
|] (Identity $ Q.AltJ metadata) True