2019-08-14 02:34:37 +03:00
|
|
|
-- | Functions for loading and modifying the catalog. See the module documentation for
|
|
|
|
-- "Hasura.RQL.DDL.Schema" for more details.
|
|
|
|
module Hasura.RQL.DDL.Schema.Catalog
|
|
|
|
( fetchCatalogData
|
|
|
|
, saveTableToCatalog
|
|
|
|
, updateTableIsEnumInCatalog
|
2019-09-19 07:47:36 +03:00
|
|
|
, updateTableConfig
|
2019-08-14 02:34:37 +03:00
|
|
|
, deleteTableFromCatalog
|
2019-10-03 10:45:52 +03:00
|
|
|
, getTableConfig
|
2019-12-09 01:17:39 +03:00
|
|
|
, purgeDependentObject
|
2019-08-14 02:34:37 +03:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Hasura.Prelude
|
|
|
|
|
2020-10-27 16:53:49 +03:00
|
|
|
import qualified Database.PG.Query as Q
|
2019-09-19 07:47:36 +03:00
|
|
|
|
|
|
|
import Data.Aeson
|
2019-08-14 02:34:37 +03:00
|
|
|
|
2020-10-27 16:53:49 +03:00
|
|
|
import Hasura.Backends.Postgres.Connection
|
|
|
|
import Hasura.Backends.Postgres.SQL.Types
|
2019-12-09 01:17:39 +03:00
|
|
|
import Hasura.RQL.DDL.ComputedField
|
|
|
|
import Hasura.RQL.DDL.EventTrigger
|
|
|
|
import Hasura.RQL.DDL.Permission.Internal
|
|
|
|
import Hasura.RQL.DDL.Relationship
|
2020-06-17 10:48:31 +03:00
|
|
|
import Hasura.RQL.DDL.RemoteRelationship
|
2019-12-09 01:17:39 +03:00
|
|
|
import Hasura.RQL.DDL.Schema.Function
|
2020-01-14 10:09:10 +03:00
|
|
|
import Hasura.RQL.Types
|
2019-08-14 02:34:37 +03:00
|
|
|
import Hasura.RQL.Types.Catalog
|
|
|
|
|
|
|
|
fetchCatalogData :: (MonadTx m) => m CatalogMetadata
|
2020-02-13 20:38:23 +03:00
|
|
|
fetchCatalogData =
|
|
|
|
liftTx $ Q.getAltJ . runIdentity . Q.getRow <$> Q.withQE defaultTxErrorHandler
|
2020-01-09 02:19:02 +03:00
|
|
|
$(Q.sqlFromFile "src-rsr/catalog_metadata.sql") () True
|
2019-08-14 02:34:37 +03:00
|
|
|
|
2020-01-14 10:09:10 +03:00
|
|
|
purgeDependentObject :: (MonadTx m) => SchemaObjId -> m ()
|
|
|
|
purgeDependentObject = \case
|
|
|
|
SOTableObj tn (TOPerm rn pt) -> liftTx $ dropPermFromCatalog tn rn pt
|
|
|
|
SOTableObj qt (TORel rn) -> liftTx $ delRelFromCatalog qt rn
|
|
|
|
SOFunction qf -> liftTx $ delFunctionFromCatalog qf
|
|
|
|
SOTableObj _ (TOTrigger trn) -> liftTx $ delEventTriggerFromCatalog trn
|
|
|
|
SOTableObj qt (TOComputedField ccn) -> dropComputedFieldFromCatalog qt ccn
|
2020-06-17 10:48:31 +03:00
|
|
|
SOTableObj qt (TORemoteRel remoteRelName) -> liftTx $ delRemoteRelFromCatalog qt remoteRelName
|
2020-01-14 10:09:10 +03:00
|
|
|
schemaObjId -> throw500 $ "unexpected dependent object: " <> reportSchemaObj schemaObjId
|
|
|
|
|
|
|
|
saveTableToCatalog
|
|
|
|
:: (MonadTx m, HasSystemDefined m) => QualifiedTable -> Bool -> TableConfig -> m ()
|
|
|
|
saveTableToCatalog (QualifiedObject sn tn) isEnum config = do
|
|
|
|
systemDefined <- askSystemDefined
|
|
|
|
liftTx $ Q.unitQE defaultTxErrorHandler [Q.sql|
|
2019-09-19 07:47:36 +03:00
|
|
|
INSERT INTO "hdb_catalog"."hdb_table"
|
2019-10-11 08:13:57 +03:00
|
|
|
(table_schema, table_name, is_system_defined, is_enum, configuration)
|
|
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
|
|
|] (sn, tn, systemDefined, isEnum, configVal) False
|
2019-09-19 07:47:36 +03:00
|
|
|
where
|
|
|
|
configVal = Q.AltJ $ toJSON config
|
2019-08-14 02:34:37 +03:00
|
|
|
|
|
|
|
updateTableIsEnumInCatalog :: (MonadTx m) => QualifiedTable -> Bool -> m ()
|
2019-09-19 07:47:36 +03:00
|
|
|
updateTableIsEnumInCatalog (QualifiedObject sn tn) isEnum = liftTx $
|
|
|
|
Q.unitQE defaultTxErrorHandler [Q.sql|
|
2019-08-14 02:34:37 +03:00
|
|
|
UPDATE "hdb_catalog"."hdb_table" SET is_enum = $3
|
|
|
|
WHERE table_schema = $1 AND table_name = $2
|
|
|
|
|] (sn, tn, isEnum) False
|
|
|
|
|
2019-09-19 07:47:36 +03:00
|
|
|
updateTableConfig :: (MonadTx m) => QualifiedTable -> TableConfig -> m ()
|
|
|
|
updateTableConfig (QualifiedObject sn tn) config = liftTx $
|
|
|
|
Q.unitQE defaultTxErrorHandler [Q.sql|
|
|
|
|
UPDATE "hdb_catalog"."hdb_table"
|
|
|
|
SET configuration = $1
|
|
|
|
WHERE table_schema = $2 AND table_name = $3
|
|
|
|
|] (configVal, sn, tn) False
|
|
|
|
where
|
|
|
|
configVal = Q.AltJ $ toJSON config
|
|
|
|
|
2019-08-14 02:34:37 +03:00
|
|
|
deleteTableFromCatalog :: (MonadTx m) => QualifiedTable -> m ()
|
|
|
|
deleteTableFromCatalog (QualifiedObject sn tn) = liftTx $ Q.unitQE defaultTxErrorHandler [Q.sql|
|
|
|
|
DELETE FROM "hdb_catalog"."hdb_table"
|
|
|
|
WHERE table_schema = $1 AND table_name = $2
|
|
|
|
|] (sn, tn) False
|
2019-10-03 10:45:52 +03:00
|
|
|
|
|
|
|
getTableConfig :: MonadTx m => QualifiedTable -> m TableConfig
|
|
|
|
getTableConfig (QualifiedObject sn tn) = liftTx $
|
|
|
|
Q.getAltJ . runIdentity . Q.getRow <$> Q.withQE defaultTxErrorHandler
|
|
|
|
[Q.sql|
|
|
|
|
SELECT configuration::json FROM hdb_catalog.hdb_table
|
|
|
|
WHERE table_schema = $1 AND table_name = $2
|
|
|
|
|] (sn, tn) True
|