2022-03-16 03:39:21 +03:00
|
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
-- | Funtions related to @hdb_catalog@ schema prior to metadata separation (catalog version < 43).
|
|
|
|
|
module Hasura.RQL.DDL.Schema.LegacyCatalog
|
2021-09-24 01:56:37 +03:00
|
|
|
|
( saveMetadataToHdbTables,
|
|
|
|
|
fetchMetadataFromHdbTables,
|
|
|
|
|
recreateSystemMetadata,
|
|
|
|
|
addCronTriggerForeignKeyConstraint,
|
|
|
|
|
)
|
|
|
|
|
where
|
|
|
|
|
|
|
|
|
|
import Control.Lens hiding ((.=))
|
|
|
|
|
import Data.Aeson
|
|
|
|
|
import Data.FileEmbed (makeRelativeToProject)
|
|
|
|
|
import Data.HashMap.Strict qualified as HM
|
|
|
|
|
import Data.HashMap.Strict.InsOrd qualified as OMap
|
2022-02-08 19:53:30 +03:00
|
|
|
|
import Data.Text.Extended ((<<>))
|
2021-09-24 01:56:37 +03:00
|
|
|
|
import Data.Text.NonEmpty
|
|
|
|
|
import Data.Time.Clock qualified as C
|
|
|
|
|
import Database.PG.Query qualified as Q
|
|
|
|
|
import Hasura.Backends.Postgres.Connection
|
|
|
|
|
import Hasura.Backends.Postgres.SQL.Types
|
|
|
|
|
import Hasura.Base.Error
|
|
|
|
|
import Hasura.Eventing.ScheduledTrigger
|
|
|
|
|
import Hasura.Prelude
|
|
|
|
|
import Hasura.RQL.DDL.Action
|
|
|
|
|
import Hasura.RQL.DDL.ComputedField
|
|
|
|
|
import Hasura.RQL.DDL.Permission
|
2021-12-01 07:53:34 +03:00
|
|
|
|
import Hasura.RQL.DDL.RemoteRelationship
|
2021-09-24 01:56:37 +03:00
|
|
|
|
import Hasura.RQL.Types
|
|
|
|
|
|
|
|
|
|
saveMetadataToHdbTables ::
|
|
|
|
|
(MonadTx m, HasSystemDefined m) => MetadataNoSources -> m ()
|
2020-12-28 15:56:00 +03:00
|
|
|
|
saveMetadataToHdbTables
|
2021-09-24 01:56:37 +03:00
|
|
|
|
( MetadataNoSources
|
|
|
|
|
tables
|
|
|
|
|
functions
|
|
|
|
|
schemas
|
|
|
|
|
collections
|
|
|
|
|
allowlist
|
|
|
|
|
customTypes
|
|
|
|
|
actions
|
|
|
|
|
cronTriggers
|
|
|
|
|
) = do
|
|
|
|
|
withPathK "tables" $ do
|
|
|
|
|
indexedForM_ tables $ \TableMetadata {..} -> do
|
|
|
|
|
-- Save table
|
|
|
|
|
saveTableToCatalog _tmTable _tmIsEnum _tmConfiguration
|
|
|
|
|
|
|
|
|
|
-- Relationships
|
|
|
|
|
withPathK "object_relationships" $
|
|
|
|
|
indexedForM_ _tmObjectRelationships $ \objRel ->
|
|
|
|
|
insertRelationshipToCatalog _tmTable ObjRel objRel
|
|
|
|
|
withPathK "array_relationships" $
|
|
|
|
|
indexedForM_ _tmArrayRelationships $ \arrRel ->
|
|
|
|
|
insertRelationshipToCatalog _tmTable ArrRel arrRel
|
|
|
|
|
|
|
|
|
|
-- Computed Fields
|
|
|
|
|
withPathK "computed_fields" $
|
|
|
|
|
indexedForM_ _tmComputedFields $
|
|
|
|
|
\(ComputedFieldMetadata name definition comment) ->
|
|
|
|
|
addComputedFieldToCatalog $
|
|
|
|
|
AddComputedField defaultSource _tmTable name definition comment
|
|
|
|
|
|
|
|
|
|
-- Remote Relationships
|
|
|
|
|
withPathK "remote_relationships" $
|
|
|
|
|
indexedForM_ _tmRemoteRelationships $
|
2021-12-14 09:45:13 +03:00
|
|
|
|
\RemoteRelationship {..} -> do
|
2021-09-24 01:56:37 +03:00
|
|
|
|
addRemoteRelationshipToCatalog $
|
2021-12-14 09:45:13 +03:00
|
|
|
|
CreateFromSourceRelationship defaultSource _tmTable _rrName _rrDefinition
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|
|
|
|
|
-- Permissions
|
|
|
|
|
withPathK "insert_permissions" $ processPerms _tmTable _tmInsertPermissions
|
|
|
|
|
withPathK "select_permissions" $ processPerms _tmTable _tmSelectPermissions
|
|
|
|
|
withPathK "update_permissions" $ processPerms _tmTable _tmUpdatePermissions
|
|
|
|
|
withPathK "delete_permissions" $ processPerms _tmTable _tmDeletePermissions
|
|
|
|
|
|
|
|
|
|
-- Event triggers
|
|
|
|
|
withPathK "event_triggers" $
|
|
|
|
|
indexedForM_ _tmEventTriggers $ \etc -> addEventTriggerToCatalog _tmTable etc
|
|
|
|
|
|
|
|
|
|
-- sql functions
|
|
|
|
|
withPathK "functions" $
|
|
|
|
|
indexedForM_ functions $
|
2021-09-24 12:18:40 +03:00
|
|
|
|
\(FunctionMetadata function config _ _) -> addFunctionToCatalog function config
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|
|
|
|
|
-- query collections
|
|
|
|
|
systemDefined <- askSystemDefined
|
|
|
|
|
withPathK "query_collections" $
|
|
|
|
|
indexedForM_ collections $ \c -> liftTx $ addCollectionToCatalog c systemDefined
|
|
|
|
|
|
|
|
|
|
-- allow list
|
|
|
|
|
withPathK "allowlist" $ do
|
2022-02-08 19:53:30 +03:00
|
|
|
|
indexedForM_ allowlist $ \(AllowlistEntry collectionName scope) -> do
|
|
|
|
|
unless (scope == AllowlistScopeGlobal) $
|
|
|
|
|
throw400 NotSupported $
|
|
|
|
|
"cannot downgrade to v1 because the "
|
|
|
|
|
<> collectionName
|
|
|
|
|
<<> " added to the allowlist is a role based allowlist"
|
|
|
|
|
liftTx $ addCollectionToAllowlistCatalog collectionName
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|
|
|
|
|
-- remote schemas
|
|
|
|
|
withPathK "remote_schemas" $
|
|
|
|
|
indexedMapM_ (liftTx . addRemoteSchemaToCatalog) schemas
|
|
|
|
|
|
|
|
|
|
-- custom types
|
|
|
|
|
withPathK "custom_types" $ setCustomTypesInCatalog customTypes
|
|
|
|
|
|
|
|
|
|
-- cron triggers
|
|
|
|
|
withPathK "cron_triggers" $
|
|
|
|
|
indexedForM_ cronTriggers $ \ct -> liftTx $ do
|
|
|
|
|
addCronTriggerToCatalog ct
|
|
|
|
|
|
|
|
|
|
-- actions
|
|
|
|
|
withPathK "actions" $
|
|
|
|
|
indexedForM_ actions $ \action -> do
|
|
|
|
|
let createAction =
|
2021-10-29 07:12:27 +03:00
|
|
|
|
CreateAction (_amName action) (_amDefinition action) (_amComment action)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
addActionToCatalog createAction
|
|
|
|
|
withPathK "permissions" $
|
|
|
|
|
indexedForM_ (_amPermissions action) $ \permission -> do
|
|
|
|
|
let createActionPermission =
|
|
|
|
|
CreateActionPermission
|
|
|
|
|
(_amName action)
|
|
|
|
|
(_apmRole permission)
|
|
|
|
|
Nothing
|
|
|
|
|
(_apmComment permission)
|
|
|
|
|
addActionPermissionToCatalog createActionPermission
|
|
|
|
|
where
|
|
|
|
|
processPerms tableName perms = indexedForM_ perms $ \perm -> do
|
|
|
|
|
systemDefined <- askSystemDefined
|
2022-04-06 15:47:35 +03:00
|
|
|
|
liftTx $ addPermissionToCatalog tableName perm systemDefined
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|
|
|
|
|
saveTableToCatalog ::
|
|
|
|
|
(MonadTx m, HasSystemDefined m) => QualifiedTable -> Bool -> TableConfig ('Postgres 'Vanilla) -> m ()
|
2020-12-08 17:22:31 +03:00
|
|
|
|
saveTableToCatalog (QualifiedObject sn tn) isEnum config = do
|
|
|
|
|
systemDefined <- askSystemDefined
|
2021-09-24 01:56:37 +03:00
|
|
|
|
liftTx $
|
|
|
|
|
Q.unitQE
|
|
|
|
|
defaultTxErrorHandler
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
INSERT INTO "hdb_catalog"."hdb_table"
|
|
|
|
|
(table_schema, table_name, is_system_defined, is_enum, configuration)
|
|
|
|
|
VALUES ($1, $2, $3, $4, $5)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
|
|
|
|
(sn, tn, systemDefined, isEnum, configVal)
|
|
|
|
|
False
|
2020-12-08 17:22:31 +03:00
|
|
|
|
where
|
|
|
|
|
configVal = Q.AltJ $ toJSON config
|
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
|
insertRelationshipToCatalog ::
|
|
|
|
|
(MonadTx m, HasSystemDefined m, ToJSON a) =>
|
|
|
|
|
QualifiedTable ->
|
|
|
|
|
RelType ->
|
|
|
|
|
RelDef a ->
|
|
|
|
|
m ()
|
2020-12-08 17:22:31 +03:00
|
|
|
|
insertRelationshipToCatalog (QualifiedObject schema table) relType (RelDef name using comment) = do
|
|
|
|
|
systemDefined <- askSystemDefined
|
|
|
|
|
let args = (schema, table, name, relTypeToTxt relType, Q.AltJ using, comment, systemDefined)
|
|
|
|
|
liftTx $ Q.unitQE defaultTxErrorHandler query args True
|
|
|
|
|
where
|
2021-09-24 01:56:37 +03:00
|
|
|
|
query =
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
INSERT INTO
|
|
|
|
|
hdb_catalog.hdb_relationship
|
|
|
|
|
(table_schema, table_name, rel_name, rel_type, rel_def, comment, is_system_defined)
|
|
|
|
|
VALUES ($1, $2, $3, $4, $5 :: jsonb, $6, $7) |]
|
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
|
addEventTriggerToCatalog ::
|
|
|
|
|
(MonadTx m, Backend ('Postgres pgKind)) =>
|
|
|
|
|
QualifiedTable ->
|
|
|
|
|
EventTriggerConf ('Postgres pgKind) ->
|
|
|
|
|
m ()
|
2020-12-08 17:22:31 +03:00
|
|
|
|
addEventTriggerToCatalog qt etc = liftTx do
|
2021-09-24 01:56:37 +03:00
|
|
|
|
Q.unitQE
|
|
|
|
|
defaultTxErrorHandler
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
INSERT into hdb_catalog.event_triggers
|
|
|
|
|
(name, type, schema_name, table_name, configuration)
|
|
|
|
|
VALUES ($1, 'table', $2, $3, $4)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
|
|
|
|
(name, sn, tn, Q.AltJ $ toJSON etc)
|
|
|
|
|
False
|
2020-12-08 17:22:31 +03:00
|
|
|
|
where
|
|
|
|
|
QualifiedObject sn tn = qt
|
2022-01-19 07:46:42 +03:00
|
|
|
|
(EventTriggerConf name _ _ _ _ _ _ _) = etc
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
|
addComputedFieldToCatalog ::
|
|
|
|
|
MonadTx m =>
|
|
|
|
|
AddComputedField ('Postgres 'Vanilla) ->
|
|
|
|
|
m ()
|
2020-12-08 17:22:31 +03:00
|
|
|
|
addComputedFieldToCatalog q =
|
2021-09-24 01:56:37 +03:00
|
|
|
|
liftTx $
|
|
|
|
|
Q.withQE
|
|
|
|
|
defaultTxErrorHandler
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
INSERT INTO hdb_catalog.hdb_computed_field
|
2022-02-16 02:16:34 +03:00
|
|
|
|
(table_schema, table_name, computed_field_name, definition, commentText)
|
2020-12-08 17:22:31 +03:00
|
|
|
|
VALUES ($1, $2, $3, $4, $5)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
2022-02-16 02:16:34 +03:00
|
|
|
|
(schemaName, tableName, computedField, Q.AltJ definition, commentText)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
True
|
2020-12-08 17:22:31 +03:00
|
|
|
|
where
|
2022-02-16 02:16:34 +03:00
|
|
|
|
commentText = commentToMaybeText comment
|
2020-12-08 17:22:31 +03:00
|
|
|
|
QualifiedObject schemaName tableName = table
|
2020-12-28 15:56:00 +03:00
|
|
|
|
AddComputedField _ table computedField definition comment = q
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
2021-12-01 07:53:34 +03:00
|
|
|
|
addRemoteRelationshipToCatalog :: MonadTx m => CreateFromSourceRelationship ('Postgres 'Vanilla) -> m ()
|
2021-12-14 09:45:13 +03:00
|
|
|
|
addRemoteRelationshipToCatalog CreateFromSourceRelationship {..} =
|
2021-09-24 01:56:37 +03:00
|
|
|
|
liftTx $
|
|
|
|
|
Q.unitQE
|
|
|
|
|
defaultTxErrorHandler
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
INSERT INTO hdb_catalog.hdb_remote_relationship
|
|
|
|
|
(remote_relationship_name, table_schema, table_name, definition)
|
|
|
|
|
VALUES ($1, $2, $3, $4::jsonb)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
2021-12-14 09:45:13 +03:00
|
|
|
|
(_crrName, schemaName, tableName, Q.AltJ _crrDefinition)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
True
|
2020-12-08 17:22:31 +03:00
|
|
|
|
where
|
2021-12-14 09:45:13 +03:00
|
|
|
|
QualifiedObject schemaName tableName = _crrTable
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
|
addFunctionToCatalog ::
|
|
|
|
|
(MonadTx m, HasSystemDefined m) =>
|
|
|
|
|
QualifiedFunction ->
|
|
|
|
|
FunctionConfig ->
|
|
|
|
|
m ()
|
2020-12-08 17:22:31 +03:00
|
|
|
|
addFunctionToCatalog (QualifiedObject sn fn) config = do
|
|
|
|
|
systemDefined <- askSystemDefined
|
2021-09-24 01:56:37 +03:00
|
|
|
|
liftTx $
|
|
|
|
|
Q.unitQE
|
|
|
|
|
defaultTxErrorHandler
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
INSERT INTO "hdb_catalog"."hdb_function"
|
|
|
|
|
(function_schema, function_name, configuration, is_system_defined)
|
|
|
|
|
VALUES ($1, $2, $3, $4)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
|
|
|
|
(sn, fn, Q.AltJ config, systemDefined)
|
|
|
|
|
False
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
|
addRemoteSchemaToCatalog ::
|
|
|
|
|
RemoteSchemaMetadata ->
|
|
|
|
|
Q.TxE QErr ()
|
2021-12-01 07:53:34 +03:00
|
|
|
|
addRemoteSchemaToCatalog (RemoteSchemaMetadata name def comment _ _) =
|
2021-09-24 01:56:37 +03:00
|
|
|
|
Q.unitQE
|
|
|
|
|
defaultTxErrorHandler
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
INSERT into hdb_catalog.remote_schemas
|
|
|
|
|
(name, definition, comment)
|
|
|
|
|
VALUES ($1, $2, $3)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
|
|
|
|
(name, Q.AltJ $ toJSON def, comment)
|
|
|
|
|
True
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
|
addCollectionToCatalog ::
|
|
|
|
|
MonadTx m => CreateCollection -> SystemDefined -> m ()
|
|
|
|
|
addCollectionToCatalog (CreateCollection name defn mComment) systemDefined =
|
|
|
|
|
liftTx $
|
|
|
|
|
Q.unitQE
|
|
|
|
|
defaultTxErrorHandler
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
INSERT INTO hdb_catalog.hdb_query_collection
|
|
|
|
|
(collection_name, collection_defn, comment, is_system_defined)
|
|
|
|
|
VALUES ($1, $2, $3, $4)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
|
|
|
|
(name, Q.AltJ defn, mComment, systemDefined)
|
|
|
|
|
True
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
|
|
|
|
addCollectionToAllowlistCatalog :: MonadTx m => CollectionName -> m ()
|
2021-09-24 01:56:37 +03:00
|
|
|
|
addCollectionToAllowlistCatalog collName =
|
|
|
|
|
liftTx $
|
|
|
|
|
Q.unitQE
|
|
|
|
|
defaultTxErrorHandler
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
INSERT INTO hdb_catalog.hdb_allowlist
|
|
|
|
|
(collection_name)
|
|
|
|
|
VALUES ($1)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
|
|
|
|
(Identity collName)
|
|
|
|
|
True
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
|
|
|
|
setCustomTypesInCatalog :: MonadTx m => CustomTypes -> m ()
|
|
|
|
|
setCustomTypesInCatalog customTypes = liftTx do
|
|
|
|
|
clearCustomTypes
|
2021-09-24 01:56:37 +03:00
|
|
|
|
Q.unitQE
|
|
|
|
|
defaultTxErrorHandler
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
INSERT into hdb_catalog.hdb_custom_types
|
|
|
|
|
(custom_types)
|
|
|
|
|
VALUES ($1)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
|
|
|
|
(Identity $ Q.AltJ customTypes)
|
|
|
|
|
False
|
2020-12-08 17:22:31 +03:00
|
|
|
|
where
|
|
|
|
|
clearCustomTypes = do
|
2021-09-24 01:56:37 +03:00
|
|
|
|
Q.unitQE
|
|
|
|
|
defaultTxErrorHandler
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
DELETE FROM hdb_catalog.hdb_custom_types
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
|
|
|
|
()
|
|
|
|
|
False
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
|
addActionToCatalog :: (MonadTx m) => CreateAction -> m ()
|
2021-10-29 07:12:27 +03:00
|
|
|
|
addActionToCatalog (CreateAction actionName actionDefinition comment) = do
|
2021-09-24 01:56:37 +03:00
|
|
|
|
liftTx $
|
|
|
|
|
Q.unitQE
|
|
|
|
|
defaultTxErrorHandler
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
INSERT into hdb_catalog.hdb_action
|
|
|
|
|
(action_name, action_defn, comment)
|
|
|
|
|
VALUES ($1, $2, $3)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
|
|
|
|
(actionName, Q.AltJ actionDefinition, comment)
|
|
|
|
|
True
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
|
|
|
|
addActionPermissionToCatalog :: (MonadTx m) => CreateActionPermission -> m ()
|
2021-09-24 01:56:37 +03:00
|
|
|
|
addActionPermissionToCatalog CreateActionPermission {..} = do
|
|
|
|
|
liftTx $
|
|
|
|
|
Q.unitQE
|
|
|
|
|
defaultTxErrorHandler
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
INSERT into hdb_catalog.hdb_action_permission
|
|
|
|
|
(action_name, role_name, comment)
|
|
|
|
|
VALUES ($1, $2, $3)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
|
|
|
|
(_capAction, _capRole, _capComment)
|
|
|
|
|
True
|
|
|
|
|
|
|
|
|
|
addPermissionToCatalog ::
|
2022-04-06 15:47:35 +03:00
|
|
|
|
(MonadTx m, Backend b) =>
|
2021-09-24 01:56:37 +03:00
|
|
|
|
QualifiedTable ->
|
2022-04-06 15:47:35 +03:00
|
|
|
|
PermDef b a ->
|
2021-09-24 01:56:37 +03:00
|
|
|
|
SystemDefined ->
|
|
|
|
|
m ()
|
2022-04-06 15:47:35 +03:00
|
|
|
|
addPermissionToCatalog (QualifiedObject sn tn) (PermDef rn qdef mComment) systemDefined =
|
2021-09-24 01:56:37 +03:00
|
|
|
|
liftTx $
|
|
|
|
|
Q.unitQE
|
|
|
|
|
defaultTxErrorHandler
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
INSERT INTO
|
|
|
|
|
hdb_catalog.hdb_permission
|
|
|
|
|
(table_schema, table_name, role_name, perm_type, perm_def, comment, is_system_defined)
|
|
|
|
|
VALUES ($1, $2, $3, $4, $5 :: jsonb, $6, $7)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
2022-04-06 15:47:35 +03:00
|
|
|
|
(sn, tn, rn, permTypeToCode (reflectPermDefPermission qdef), Q.AltJ qdef, mComment, systemDefined)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
True
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
|
addCronTriggerToCatalog :: (MonadTx m) => CronTriggerMetadata -> m ()
|
2020-12-08 17:22:31 +03:00
|
|
|
|
addCronTriggerToCatalog CronTriggerMetadata {..} = liftTx $ do
|
2021-09-24 01:56:37 +03:00
|
|
|
|
Q.unitQE
|
|
|
|
|
defaultTxErrorHandler
|
2020-12-08 17:22:31 +03:00
|
|
|
|
[Q.sql|
|
|
|
|
|
INSERT into hdb_catalog.hdb_cron_triggers
|
|
|
|
|
(name, webhook_conf, cron_schedule, payload, retry_conf, header_conf, include_in_metadata, comment)
|
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
|
|
|
|
( ctName,
|
|
|
|
|
Q.AltJ ctWebhook,
|
|
|
|
|
ctSchedule,
|
|
|
|
|
Q.AltJ <$> ctPayload,
|
|
|
|
|
Q.AltJ ctRetryConf,
|
|
|
|
|
Q.AltJ ctHeaders,
|
|
|
|
|
ctIncludeInMetadata,
|
|
|
|
|
ctComment
|
|
|
|
|
)
|
|
|
|
|
False
|
2020-12-08 17:22:31 +03:00
|
|
|
|
currentTime <- liftIO C.getCurrentTime
|
|
|
|
|
let scheduleTimes = generateScheduleTimes currentTime 100 ctSchedule -- generate next 100 events
|
2021-09-13 21:00:53 +03:00
|
|
|
|
insertCronEventsTx $ map (CronEventSeed ctName) scheduleTimes
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
2020-12-28 15:56:00 +03:00
|
|
|
|
fetchMetadataFromHdbTables :: MonadTx m => m MetadataNoSources
|
2020-12-08 17:22:31 +03:00
|
|
|
|
fetchMetadataFromHdbTables = liftTx do
|
|
|
|
|
tables <- Q.catchE defaultTxErrorHandler fetchTables
|
|
|
|
|
let tableMetaMap = OMap.fromList . flip map tables $
|
|
|
|
|
\(schema, name, isEnum, maybeConfig) ->
|
|
|
|
|
let qualifiedName = QualifiedObject schema name
|
|
|
|
|
configuration = maybe emptyTableConfig Q.getAltJ maybeConfig
|
2021-09-24 01:56:37 +03:00
|
|
|
|
in (qualifiedName, mkTableMeta qualifiedName isEnum configuration)
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
|
|
|
|
-- Fetch all the relationships
|
|
|
|
|
relationships <- Q.catchE defaultTxErrorHandler fetchRelationships
|
|
|
|
|
|
|
|
|
|
objRelDefs <- mkRelDefs ObjRel relationships
|
|
|
|
|
arrRelDefs <- mkRelDefs ArrRel relationships
|
|
|
|
|
|
|
|
|
|
-- Fetch all the permissions
|
|
|
|
|
permissions <- Q.catchE defaultTxErrorHandler fetchPermissions
|
|
|
|
|
|
|
|
|
|
-- Parse all the permissions
|
|
|
|
|
insPermDefs <- mkPermDefs PTInsert permissions
|
|
|
|
|
selPermDefs <- mkPermDefs PTSelect permissions
|
|
|
|
|
updPermDefs <- mkPermDefs PTUpdate permissions
|
|
|
|
|
delPermDefs <- mkPermDefs PTDelete permissions
|
|
|
|
|
|
|
|
|
|
-- Fetch all event triggers
|
|
|
|
|
eventTriggers <- Q.catchE defaultTxErrorHandler fetchEventTriggers
|
|
|
|
|
triggerMetaDefs <- mkTriggerMetaDefs eventTriggers
|
|
|
|
|
|
|
|
|
|
-- Fetch all computed fields
|
|
|
|
|
computedFields <- fetchComputedFields
|
|
|
|
|
|
|
|
|
|
-- Fetch all remote relationships
|
|
|
|
|
remoteRelationships <- Q.catchE defaultTxErrorHandler fetchRemoteRelationships
|
|
|
|
|
|
|
|
|
|
let (_, fullTableMetaMap) = flip runState tableMetaMap $ do
|
|
|
|
|
modMetaMap tmObjectRelationships _rdName objRelDefs
|
|
|
|
|
modMetaMap tmArrayRelationships _rdName arrRelDefs
|
|
|
|
|
modMetaMap tmInsertPermissions _pdRole insPermDefs
|
|
|
|
|
modMetaMap tmSelectPermissions _pdRole selPermDefs
|
|
|
|
|
modMetaMap tmUpdatePermissions _pdRole updPermDefs
|
|
|
|
|
modMetaMap tmDeletePermissions _pdRole delPermDefs
|
|
|
|
|
modMetaMap tmEventTriggers etcName triggerMetaDefs
|
|
|
|
|
modMetaMap tmComputedFields _cfmName computedFields
|
2021-12-01 07:53:34 +03:00
|
|
|
|
modMetaMap tmRemoteRelationships _rrName remoteRelationships
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
|
|
|
|
functions <- Q.catchE defaultTxErrorHandler fetchFunctions
|
2020-12-21 12:11:37 +03:00
|
|
|
|
remoteSchemas <- oMapFromL _rsmName <$> fetchRemoteSchemas
|
2020-12-08 17:22:31 +03:00
|
|
|
|
collections <- oMapFromL _ccName <$> fetchCollections
|
2022-02-08 19:53:30 +03:00
|
|
|
|
allowlist <- oMapFromL aeCollection <$> fetchAllowlist
|
2020-12-08 17:22:31 +03:00
|
|
|
|
customTypes <- fetchCustomTypes
|
|
|
|
|
actions <- oMapFromL _amName <$> fetchActions
|
2022-02-08 19:53:30 +03:00
|
|
|
|
cronTriggers <- fetchCronTriggers
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
2022-02-08 19:53:30 +03:00
|
|
|
|
pure $
|
|
|
|
|
MetadataNoSources
|
|
|
|
|
fullTableMetaMap
|
|
|
|
|
functions
|
|
|
|
|
remoteSchemas
|
|
|
|
|
collections
|
|
|
|
|
allowlist
|
|
|
|
|
customTypes
|
|
|
|
|
actions
|
|
|
|
|
cronTriggers
|
2020-12-08 17:22:31 +03:00
|
|
|
|
where
|
|
|
|
|
modMetaMap l f xs = do
|
|
|
|
|
st <- get
|
2021-09-24 01:56:37 +03:00
|
|
|
|
put $ foldl' (\b (qt, dfn) -> b & at qt . _Just . l %~ OMap.insert (f dfn) dfn) st xs
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
|
|
|
|
mkPermDefs pt = mapM permRowToDef . filter (\pr -> pr ^. _4 == pt)
|
|
|
|
|
|
|
|
|
|
permRowToDef (sn, tn, rn, _, Q.AltJ pDef, mComment) = do
|
|
|
|
|
perm <- decodeValue pDef
|
2021-09-24 01:56:37 +03:00
|
|
|
|
return (QualifiedObject sn tn, PermDef rn perm mComment)
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
|
|
|
|
mkRelDefs rt = mapM relRowToDef . filter (\rr -> rr ^. _4 == rt)
|
|
|
|
|
|
|
|
|
|
relRowToDef (sn, tn, rn, _, Q.AltJ rDef, mComment) = do
|
|
|
|
|
using <- decodeValue rDef
|
|
|
|
|
return (QualifiedObject sn tn, RelDef rn using mComment)
|
|
|
|
|
|
|
|
|
|
mkTriggerMetaDefs = mapM trigRowToDef
|
|
|
|
|
|
|
|
|
|
trigRowToDef (sn, tn, Q.AltJ configuration) = do
|
2021-09-06 14:15:36 +03:00
|
|
|
|
conf :: EventTriggerConf ('Postgres pgKind) <- decodeValue configuration
|
|
|
|
|
return (QualifiedObject sn tn, conf)
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
|
|
|
|
fetchTables =
|
2021-09-24 01:56:37 +03:00
|
|
|
|
Q.listQ
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
SELECT table_schema, table_name, is_enum, configuration::json
|
|
|
|
|
FROM hdb_catalog.hdb_table
|
|
|
|
|
WHERE is_system_defined = 'false'
|
|
|
|
|
ORDER BY table_schema ASC, table_name ASC
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
|
|
|
|
()
|
|
|
|
|
False
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
|
|
|
|
fetchRelationships =
|
2021-09-24 01:56:37 +03:00
|
|
|
|
Q.listQ
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
SELECT table_schema, table_name, rel_name, rel_type, rel_def::json, comment
|
|
|
|
|
FROM hdb_catalog.hdb_relationship
|
|
|
|
|
WHERE is_system_defined = 'false'
|
|
|
|
|
ORDER BY table_schema ASC, table_name ASC, rel_name ASC
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
|
|
|
|
()
|
|
|
|
|
False
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
|
|
|
|
fetchPermissions =
|
2021-09-24 01:56:37 +03:00
|
|
|
|
Q.listQ
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
SELECT table_schema, table_name, role_name, perm_type, perm_def::json, comment
|
|
|
|
|
FROM hdb_catalog.hdb_permission
|
|
|
|
|
WHERE is_system_defined = 'false'
|
|
|
|
|
ORDER BY table_schema ASC, table_name ASC, role_name ASC, perm_type ASC
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
|
|
|
|
()
|
|
|
|
|
False
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
|
|
|
|
fetchEventTriggers =
|
2021-09-24 01:56:37 +03:00
|
|
|
|
Q.listQ
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
SELECT e.schema_name, e.table_name, e.configuration::json
|
|
|
|
|
FROM hdb_catalog.event_triggers e
|
|
|
|
|
ORDER BY e.schema_name ASC, e.table_name ASC, e.name ASC
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
|
|
|
|
()
|
|
|
|
|
False
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
|
|
|
|
fetchFunctions = do
|
2021-09-24 01:56:37 +03:00
|
|
|
|
l <-
|
|
|
|
|
Q.listQ
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
SELECT function_schema, function_name, configuration::json
|
|
|
|
|
FROM hdb_catalog.hdb_function
|
|
|
|
|
WHERE is_system_defined = 'false'
|
|
|
|
|
ORDER BY function_schema ASC, function_name ASC
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
|
|
|
|
()
|
|
|
|
|
False
|
|
|
|
|
pure $
|
|
|
|
|
oMapFromL _fmFunction $
|
|
|
|
|
flip map l $ \(sn, fn, Q.AltJ config) ->
|
|
|
|
|
-- function permissions were only introduced post 43rd
|
|
|
|
|
-- migration, so it's impossible we get any permissions
|
|
|
|
|
-- here
|
2021-09-24 12:18:40 +03:00
|
|
|
|
FunctionMetadata (QualifiedObject sn fn) config [] Nothing
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
|
|
|
|
fetchRemoteSchemas =
|
2021-09-24 01:56:37 +03:00
|
|
|
|
map fromRow
|
|
|
|
|
<$> Q.listQE
|
|
|
|
|
defaultTxErrorHandler
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
SELECT name, definition, comment
|
|
|
|
|
FROM hdb_catalog.remote_schemas
|
|
|
|
|
ORDER BY name ASC
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
|
|
|
|
()
|
|
|
|
|
True
|
2020-12-08 17:22:31 +03:00
|
|
|
|
where
|
|
|
|
|
fromRow (name, Q.AltJ def, comment) =
|
2021-12-01 07:53:34 +03:00
|
|
|
|
RemoteSchemaMetadata name def comment mempty mempty
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
|
|
|
|
fetchCollections =
|
2021-09-24 01:56:37 +03:00
|
|
|
|
map fromRow
|
|
|
|
|
<$> Q.listQE
|
|
|
|
|
defaultTxErrorHandler
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
SELECT collection_name, collection_defn::json, comment
|
|
|
|
|
FROM hdb_catalog.hdb_query_collection
|
|
|
|
|
WHERE is_system_defined = 'false'
|
|
|
|
|
ORDER BY collection_name ASC
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
|
|
|
|
()
|
|
|
|
|
False
|
2020-12-08 17:22:31 +03:00
|
|
|
|
where
|
|
|
|
|
fromRow (name, Q.AltJ defn, mComment) =
|
|
|
|
|
CreateCollection name defn mComment
|
|
|
|
|
|
2022-02-08 19:53:30 +03:00
|
|
|
|
fetchAllowlist =
|
|
|
|
|
map fromRow
|
2021-09-24 01:56:37 +03:00
|
|
|
|
<$> Q.listQE
|
|
|
|
|
defaultTxErrorHandler
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
SELECT collection_name
|
|
|
|
|
FROM hdb_catalog.hdb_allowlist
|
|
|
|
|
ORDER BY collection_name ASC
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
|
|
|
|
()
|
|
|
|
|
False
|
2022-02-08 19:53:30 +03:00
|
|
|
|
where
|
|
|
|
|
fromRow (Identity name) = AllowlistEntry name AllowlistScopeGlobal
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
|
|
|
|
fetchComputedFields = do
|
2021-09-24 01:56:37 +03:00
|
|
|
|
r <-
|
|
|
|
|
Q.listQE
|
|
|
|
|
defaultTxErrorHandler
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
SELECT table_schema, table_name, computed_field_name,
|
|
|
|
|
definition::json, comment
|
|
|
|
|
FROM hdb_catalog.hdb_computed_field
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
|
|
|
|
()
|
|
|
|
|
False
|
|
|
|
|
pure $
|
|
|
|
|
flip map r $ \(schema, table, name, Q.AltJ definition, comment) ->
|
|
|
|
|
( QualifiedObject schema table,
|
2022-02-16 02:16:34 +03:00
|
|
|
|
ComputedFieldMetadata name definition (commentFromMaybeText comment)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
)
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
|
|
|
|
fetchCronTriggers =
|
2021-05-21 05:46:58 +03:00
|
|
|
|
oMapFromL ctName . map uncurryCronTrigger
|
2021-09-24 01:56:37 +03:00
|
|
|
|
<$> Q.listQE
|
|
|
|
|
defaultTxErrorHandler
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
SELECT ct.name, ct.webhook_conf, ct.cron_schedule, ct.payload,
|
|
|
|
|
ct.retry_conf, ct.header_conf, ct.include_in_metadata, ct.comment
|
|
|
|
|
FROM hdb_catalog.hdb_cron_triggers ct
|
|
|
|
|
WHERE include_in_metadata
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
|
|
|
|
()
|
|
|
|
|
False
|
2020-12-08 17:22:31 +03:00
|
|
|
|
where
|
|
|
|
|
uncurryCronTrigger
|
|
|
|
|
(name, webhook, schedule, payload, retryConfig, headerConfig, includeMetadata, comment) =
|
2021-09-24 01:56:37 +03:00
|
|
|
|
CronTriggerMetadata
|
|
|
|
|
{ ctName = name,
|
|
|
|
|
ctWebhook = Q.getAltJ webhook,
|
|
|
|
|
ctSchedule = schedule,
|
|
|
|
|
ctPayload = Q.getAltJ <$> payload,
|
|
|
|
|
ctRetryConf = Q.getAltJ retryConfig,
|
|
|
|
|
ctHeaders = Q.getAltJ headerConfig,
|
|
|
|
|
ctIncludeInMetadata = includeMetadata,
|
2022-01-19 07:46:42 +03:00
|
|
|
|
ctComment = comment,
|
|
|
|
|
ctRequestTransform = Nothing,
|
|
|
|
|
ctResponseTransform = Nothing
|
2021-09-24 01:56:37 +03:00
|
|
|
|
}
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
|
|
|
|
fetchCustomTypes :: Q.TxE QErr CustomTypes
|
|
|
|
|
fetchCustomTypes =
|
2021-09-24 01:56:37 +03:00
|
|
|
|
Q.getAltJ . runIdentity . Q.getRow
|
|
|
|
|
<$> Q.rawQE
|
|
|
|
|
defaultTxErrorHandler
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
select coalesce((select custom_types::json from hdb_catalog.hdb_custom_types), '{}'::json)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
|
|
|
|
[]
|
|
|
|
|
False
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
|
|
|
|
fetchActions =
|
2021-09-24 01:56:37 +03:00
|
|
|
|
Q.getAltJ . runIdentity . Q.getRow
|
|
|
|
|
<$> Q.rawQE
|
|
|
|
|
defaultTxErrorHandler
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
select
|
|
|
|
|
coalesce(
|
|
|
|
|
json_agg(
|
|
|
|
|
json_build_object(
|
|
|
|
|
'name', a.action_name,
|
|
|
|
|
'definition', a.action_defn,
|
|
|
|
|
'comment', a.comment,
|
|
|
|
|
'permissions', ap.permissions
|
|
|
|
|
) order by a.action_name asc
|
|
|
|
|
),
|
|
|
|
|
'[]'
|
|
|
|
|
)
|
|
|
|
|
from
|
|
|
|
|
hdb_catalog.hdb_action as a
|
|
|
|
|
left outer join lateral (
|
|
|
|
|
select
|
|
|
|
|
coalesce(
|
|
|
|
|
json_agg(
|
|
|
|
|
json_build_object(
|
|
|
|
|
'role', ap.role_name,
|
|
|
|
|
'comment', ap.comment
|
|
|
|
|
) order by ap.role_name asc
|
|
|
|
|
),
|
|
|
|
|
'[]'
|
|
|
|
|
) as permissions
|
|
|
|
|
from
|
|
|
|
|
hdb_catalog.hdb_action_permission ap
|
|
|
|
|
where
|
|
|
|
|
ap.action_name = a.action_name
|
|
|
|
|
) ap on true;
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
|
|
|
|
[]
|
|
|
|
|
False
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
|
|
|
|
fetchRemoteRelationships = do
|
2021-09-24 01:56:37 +03:00
|
|
|
|
r <-
|
|
|
|
|
Q.listQ
|
|
|
|
|
[Q.sql|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
SELECT table_schema, table_name,
|
|
|
|
|
remote_relationship_name, definition::json
|
|
|
|
|
FROM hdb_catalog.hdb_remote_relationship
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
|
|
|
|
()
|
|
|
|
|
False
|
|
|
|
|
pure $
|
|
|
|
|
flip map r $ \(schema, table, name, Q.AltJ definition) ->
|
|
|
|
|
( QualifiedObject schema table,
|
2021-12-01 07:53:34 +03:00
|
|
|
|
RemoteRelationship name $ RelationshipToSchema RRFOldDBToRemoteSchema definition
|
2021-09-24 01:56:37 +03:00
|
|
|
|
)
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
2021-08-17 10:01:14 +03:00
|
|
|
|
addCronTriggerForeignKeyConstraint :: MonadTx m => m ()
|
|
|
|
|
addCronTriggerForeignKeyConstraint =
|
|
|
|
|
liftTx $
|
2021-09-24 01:56:37 +03:00
|
|
|
|
Q.unitQE
|
|
|
|
|
defaultTxErrorHandler
|
|
|
|
|
[Q.sql|
|
2021-08-17 10:01:14 +03:00
|
|
|
|
ALTER TABLE hdb_catalog.hdb_cron_events ADD CONSTRAINT
|
|
|
|
|
hdb_cron_events_trigger_name_fkey FOREIGN KEY (trigger_name)
|
|
|
|
|
REFERENCES hdb_catalog.hdb_cron_triggers(name)
|
|
|
|
|
ON UPDATE CASCADE ON DELETE CASCADE;
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|]
|
|
|
|
|
()
|
|
|
|
|
False
|
2021-08-17 10:01:14 +03:00
|
|
|
|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
-- | Drops and recreates all “system-defined” metadata, aka metadata for tables and views in the
|
|
|
|
|
-- @information_schema@ and @hdb_catalog@ schemas. These tables and views are tracked to expose them
|
|
|
|
|
-- to the console, which allows us to reuse the same functionality we use to implement user-defined
|
|
|
|
|
-- APIs to expose the catalog.
|
|
|
|
|
--
|
|
|
|
|
-- This process has a long and storied history.
|
|
|
|
|
--
|
|
|
|
|
-- In the past, we reused the same machinery we use for CLI migrations to define our own internal
|
|
|
|
|
-- metadata migrations. This caused trouble, however, as we’d have to run those migrations in
|
|
|
|
|
-- lockstep with our SQL migrations to ensure the two didn’t get out of sync. This in turn caused
|
|
|
|
|
-- trouble because those migrations would hit code paths inside @graphql-engine@ to add or remove
|
|
|
|
|
-- things from the @pg_catalog@ tables, and /that/ in turn would fail because we hadn’t finished
|
|
|
|
|
-- running the SQL migrations, so we were running a new version of the code against an old version
|
|
|
|
|
-- of the schema! That caused #2826.
|
|
|
|
|
--
|
|
|
|
|
-- To fix that, #2379 switched to the approach of just dropping and recreating all system metadata
|
|
|
|
|
-- every time we run any SQL migrations. But /that/ in turn caused trouble due to the way we were
|
|
|
|
|
-- constantly rebuilding the schema cache (#3354), causing us to switch to incremental schema cache
|
|
|
|
|
-- construction (#3394). However, although that mostly resolved the problem, we still weren’t
|
|
|
|
|
-- totally out of the woods, as the incremental construction was still too slow on slow Postgres
|
|
|
|
|
-- instances (#3654).
|
|
|
|
|
--
|
|
|
|
|
-- To sidestep the whole issue, as of #3686 we now just create all the system metadata in code here,
|
|
|
|
|
-- and we only rebuild the schema cache once, at the very end. This is a little unsatisfying, since
|
|
|
|
|
-- it means our internal migrations are “blessed” compared to user-defined CLI migrations. If we
|
|
|
|
|
-- improve CLI migrations further in the future, maybe we can switch back to using that approach,
|
|
|
|
|
-- instead.
|
|
|
|
|
recreateSystemMetadata :: (MonadTx m) => m ()
|
|
|
|
|
recreateSystemMetadata = do
|
2021-03-16 20:35:35 +03:00
|
|
|
|
() <- liftTx $ Q.multiQE defaultTxErrorHandler $(makeRelativeToProject "src-rsr/clear_system_metadata.sql" >>= Q.sqlFromFile)
|
2020-12-08 17:22:31 +03:00
|
|
|
|
runHasSystemDefinedT (SystemDefined True) $ for_ systemMetadata \(tableName, tableRels) -> do
|
|
|
|
|
saveTableToCatalog tableName False emptyTableConfig
|
|
|
|
|
for_ tableRels \case
|
2021-09-24 01:56:37 +03:00
|
|
|
|
Left relDef -> insertRelationshipToCatalog tableName ObjRel relDef
|
2020-12-08 17:22:31 +03:00
|
|
|
|
Right relDef -> insertRelationshipToCatalog tableName ArrRel relDef
|
|
|
|
|
where
|
2021-04-22 00:44:37 +03:00
|
|
|
|
systemMetadata :: [(QualifiedTable, [Either (ObjRelDef ('Postgres 'Vanilla)) (ArrRelDef ('Postgres 'Vanilla))])]
|
2020-12-08 17:22:31 +03:00
|
|
|
|
systemMetadata =
|
2021-09-24 01:56:37 +03:00
|
|
|
|
[ table "information_schema" "tables" [],
|
|
|
|
|
table "information_schema" "schemata" [],
|
|
|
|
|
table "information_schema" "views" [],
|
|
|
|
|
table "information_schema" "columns" [],
|
|
|
|
|
table
|
|
|
|
|
"hdb_catalog"
|
|
|
|
|
"hdb_table"
|
|
|
|
|
[ objectRel $$(nonEmptyText "detail") $
|
|
|
|
|
manualConfig "information_schema" "tables" tableNameMapping,
|
|
|
|
|
objectRel $$(nonEmptyText "primary_key") $
|
|
|
|
|
manualConfig "hdb_catalog" "hdb_primary_key" tableNameMapping,
|
|
|
|
|
arrayRel $$(nonEmptyText "columns") $
|
|
|
|
|
manualConfig "information_schema" "columns" tableNameMapping,
|
|
|
|
|
arrayRel $$(nonEmptyText "foreign_key_constraints") $
|
|
|
|
|
manualConfig "hdb_catalog" "hdb_foreign_key_constraint" tableNameMapping,
|
|
|
|
|
arrayRel $$(nonEmptyText "relationships") $
|
|
|
|
|
manualConfig "hdb_catalog" "hdb_relationship" tableNameMapping,
|
|
|
|
|
arrayRel $$(nonEmptyText "permissions") $
|
|
|
|
|
manualConfig "hdb_catalog" "hdb_permission_agg" tableNameMapping,
|
|
|
|
|
arrayRel $$(nonEmptyText "computed_fields") $
|
|
|
|
|
manualConfig "hdb_catalog" "hdb_computed_field" tableNameMapping,
|
|
|
|
|
arrayRel $$(nonEmptyText "check_constraints") $
|
|
|
|
|
manualConfig "hdb_catalog" "hdb_check_constraint" tableNameMapping,
|
|
|
|
|
arrayRel $$(nonEmptyText "unique_constraints") $
|
|
|
|
|
manualConfig "hdb_catalog" "hdb_unique_constraint" tableNameMapping
|
|
|
|
|
],
|
|
|
|
|
table "hdb_catalog" "hdb_primary_key" [],
|
|
|
|
|
table "hdb_catalog" "hdb_foreign_key_constraint" [],
|
|
|
|
|
table "hdb_catalog" "hdb_relationship" [],
|
|
|
|
|
table "hdb_catalog" "hdb_permission_agg" [],
|
|
|
|
|
table "hdb_catalog" "hdb_computed_field" [],
|
|
|
|
|
table "hdb_catalog" "hdb_check_constraint" [],
|
|
|
|
|
table "hdb_catalog" "hdb_unique_constraint" [],
|
|
|
|
|
table "hdb_catalog" "hdb_remote_relationship" [],
|
|
|
|
|
table
|
|
|
|
|
"hdb_catalog"
|
|
|
|
|
"event_triggers"
|
|
|
|
|
[ arrayRel $$(nonEmptyText "events") $
|
|
|
|
|
manualConfig "hdb_catalog" "event_log" [("name", "trigger_name")]
|
|
|
|
|
],
|
|
|
|
|
table
|
|
|
|
|
"hdb_catalog"
|
|
|
|
|
"event_log"
|
|
|
|
|
[ objectRel $$(nonEmptyText "trigger") $
|
|
|
|
|
manualConfig "hdb_catalog" "event_triggers" [("trigger_name", "name")],
|
|
|
|
|
arrayRel $$(nonEmptyText "logs") $
|
|
|
|
|
RUFKeyOn $
|
|
|
|
|
ArrRelUsingFKeyOn (QualifiedObject "hdb_catalog" "event_invocation_logs") (pure "event_id")
|
|
|
|
|
],
|
|
|
|
|
table
|
|
|
|
|
"hdb_catalog"
|
|
|
|
|
"event_invocation_logs"
|
|
|
|
|
[objectRel $$(nonEmptyText "event") $ RUFKeyOn $ SameTable (pure "event_id")],
|
|
|
|
|
table "hdb_catalog" "hdb_function" [],
|
|
|
|
|
table
|
|
|
|
|
"hdb_catalog"
|
|
|
|
|
"hdb_function_agg"
|
|
|
|
|
[ objectRel $$(nonEmptyText "return_table_info") $
|
|
|
|
|
manualConfig
|
|
|
|
|
"hdb_catalog"
|
|
|
|
|
"hdb_table"
|
|
|
|
|
[ ("return_type_schema", "table_schema"),
|
|
|
|
|
("return_type_name", "table_name")
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
table "hdb_catalog" "remote_schemas" [],
|
|
|
|
|
table "hdb_catalog" "hdb_version" [],
|
|
|
|
|
table "hdb_catalog" "hdb_query_collection" [],
|
|
|
|
|
table "hdb_catalog" "hdb_allowlist" [],
|
|
|
|
|
table "hdb_catalog" "hdb_custom_types" [],
|
|
|
|
|
table "hdb_catalog" "hdb_action_permission" [],
|
|
|
|
|
table
|
|
|
|
|
"hdb_catalog"
|
|
|
|
|
"hdb_action"
|
|
|
|
|
[ arrayRel $$(nonEmptyText "permissions") $
|
|
|
|
|
manualConfig
|
|
|
|
|
"hdb_catalog"
|
|
|
|
|
"hdb_action_permission"
|
|
|
|
|
[("action_name", "action_name")]
|
|
|
|
|
],
|
|
|
|
|
table "hdb_catalog" "hdb_action_log" [],
|
|
|
|
|
table
|
|
|
|
|
"hdb_catalog"
|
|
|
|
|
"hdb_role"
|
|
|
|
|
[ arrayRel $$(nonEmptyText "action_permissions") $
|
|
|
|
|
manualConfig
|
|
|
|
|
"hdb_catalog"
|
|
|
|
|
"hdb_action_permission"
|
|
|
|
|
[("role_name", "role_name")],
|
|
|
|
|
arrayRel $$(nonEmptyText "permissions") $
|
|
|
|
|
manualConfig
|
|
|
|
|
"hdb_catalog"
|
|
|
|
|
"hdb_permission_agg"
|
|
|
|
|
[("role_name", "role_name")]
|
|
|
|
|
],
|
|
|
|
|
table
|
|
|
|
|
"hdb_catalog"
|
|
|
|
|
"hdb_cron_triggers"
|
|
|
|
|
[ arrayRel $$(nonEmptyText "cron_events") $
|
|
|
|
|
RUFKeyOn $
|
|
|
|
|
ArrRelUsingFKeyOn (QualifiedObject "hdb_catalog" "hdb_cron_events") (pure "trigger_name")
|
|
|
|
|
],
|
|
|
|
|
table
|
|
|
|
|
"hdb_catalog"
|
|
|
|
|
"hdb_cron_events"
|
|
|
|
|
[ objectRel $$(nonEmptyText "cron_trigger") $ RUFKeyOn $ SameTable (pure "trigger_name"),
|
|
|
|
|
arrayRel $$(nonEmptyText "cron_event_logs") $
|
|
|
|
|
RUFKeyOn $
|
|
|
|
|
ArrRelUsingFKeyOn (QualifiedObject "hdb_catalog" "hdb_cron_event_invocation_logs") (pure "event_id")
|
|
|
|
|
],
|
|
|
|
|
table
|
|
|
|
|
"hdb_catalog"
|
|
|
|
|
"hdb_cron_event_invocation_logs"
|
|
|
|
|
[ objectRel $$(nonEmptyText "cron_event") $ RUFKeyOn $ SameTable (pure "event_id")
|
|
|
|
|
],
|
|
|
|
|
table
|
|
|
|
|
"hdb_catalog"
|
|
|
|
|
"hdb_scheduled_events"
|
|
|
|
|
[ arrayRel $$(nonEmptyText "scheduled_event_logs") $
|
|
|
|
|
RUFKeyOn $
|
|
|
|
|
ArrRelUsingFKeyOn (QualifiedObject "hdb_catalog" "hdb_scheduled_event_invocation_logs") (pure "event_id")
|
|
|
|
|
],
|
|
|
|
|
table
|
|
|
|
|
"hdb_catalog"
|
|
|
|
|
"hdb_scheduled_event_invocation_logs"
|
|
|
|
|
[ objectRel $$(nonEmptyText "scheduled_event") $ RUFKeyOn $ SameTable (pure "event_id")
|
|
|
|
|
]
|
2020-12-08 17:22:31 +03:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
tableNameMapping =
|
2021-09-24 01:56:37 +03:00
|
|
|
|
[ ("table_schema", "table_schema"),
|
|
|
|
|
("table_name", "table_name")
|
|
|
|
|
]
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
|
|
|
|
table schemaName tableName relationships = (QualifiedObject schemaName tableName, relationships)
|
|
|
|
|
objectRel name using = Left $ RelDef (RelName name) using Nothing
|
|
|
|
|
arrayRel name using = Right $ RelDef (RelName name) using Nothing
|
|
|
|
|
manualConfig schemaName tableName columns =
|
2021-03-03 16:02:00 +03:00
|
|
|
|
RUManual $ RelManualConfig (QualifiedObject schemaName tableName) (HM.fromList columns) Nothing
|