1
+ {-# LANGUAGE RecordWildCards #-}
1
2
module Hasura.RQL.DDL.Metadata
2
3
( runReplaceMetadata
3
4
, runExportMetadata
@@ -26,10 +27,10 @@ import Hasura.RQL.DDL.ComputedField (dropComputedFieldFromCatalo
26
27
import Hasura.RQL.DDL.EventTrigger (delEventTriggerFromCatalog , subTableP2 )
27
28
import Hasura.RQL.DDL.Metadata.Types
28
29
import Hasura.RQL.DDL.Permission.Internal (dropPermFromCatalog )
29
- import Hasura.RQL.DDL.RemoteSchema (addRemoteSchemaP2 , fetchRemoteSchemas ,
30
+ import Hasura.RQL.DDL.RemoteSchema (addRemoteSchemaToCatalog , fetchRemoteSchemas ,
30
31
removeRemoteSchemaFromCatalog )
32
+ import Hasura.RQL.DDL.Schema.Catalog (saveTableToCatalog )
31
33
import Hasura.RQL.Types
32
- import Hasura.Server.Version (HasVersion )
33
34
import Hasura.SQL.Types
34
35
35
36
import qualified Database.PG.Query as Q
@@ -41,8 +42,9 @@ import qualified Hasura.RQL.DDL.QueryCollection as Collection
41
42
import qualified Hasura.RQL.DDL.Relationship as Relationship
42
43
import qualified Hasura.RQL.DDL.Schema as Schema
43
44
44
- clearMetadata :: Q. TxE QErr ()
45
- clearMetadata = Q. catchE defaultTxErrorHandler $ do
45
+ -- | Purge all user-defined metadata; metadata with is_system_defined = false
46
+ clearUserMetadata :: MonadTx m => m ()
47
+ clearUserMetadata = liftTx $ Q. catchE defaultTxErrorHandler $ do
46
48
Q. unitQ " DELETE FROM hdb_catalog.hdb_function WHERE is_system_defined <> 'true'" () False
47
49
Q. unitQ " DELETE FROM hdb_catalog.hdb_permission WHERE is_system_defined <> 'true'" () False
48
50
Q. unitQ " DELETE FROM hdb_catalog.hdb_relationship WHERE is_system_defined <> 'true'" () False
@@ -60,7 +62,7 @@ runClearMetadata
60
62
:: (MonadTx m , CacheRWM m )
61
63
=> ClearMetadata -> m EncJSON
62
64
runClearMetadata _ = do
63
- liftTx clearMetadata
65
+ clearUserMetadata
64
66
buildSchemaCacheStrict
65
67
return successMsg
66
68
@@ -124,69 +126,53 @@ applyQP1 (ReplaceMetadata _ tables functionsMeta schemas collections
124
126
getDups l =
125
127
l L. \\ HS. toList (HS. fromList l)
126
128
127
- applyQP2
128
- :: ( HasVersion
129
- , MonadIO m
130
- , MonadTx m
131
- , CacheRWM m
132
- , HasSystemDefined m
133
- , HasHttpManager m
134
- )
135
- => ReplaceMetadata
136
- -> m EncJSON
137
- applyQP2 (ReplaceMetadata _ tables functionsMeta
138
- schemas collections allowlist customTypes actions) = do
139
-
140
- liftTx clearMetadata
129
+ applyQP2 :: (CacheRWM m , MonadTx m , HasSystemDefined m ) => ReplaceMetadata -> m EncJSON
130
+ applyQP2 replaceMetadata = do
131
+ clearUserMetadata
132
+ saveMetadata replaceMetadata
141
133
buildSchemaCacheStrict
134
+ pure successMsg
135
+
136
+ saveMetadata :: (MonadTx m , HasSystemDefined m ) => ReplaceMetadata -> m ()
137
+ saveMetadata (ReplaceMetadata _ tables functionsMeta
138
+ schemas collections allowlist customTypes actions) = do
142
139
143
140
withPathK " tables" $ do
144
- -- tables and views
145
- indexedForM_ tables $ \ tableMeta -> do
146
- let tableName = tableMeta ^. tmTable
147
- isEnum = tableMeta ^. tmIsEnum
148
- config = tableMeta ^. tmConfiguration
149
- void $ Schema. trackExistingTableOrViewP2 tableName isEnum config
150
-
151
- indexedForM_ tables $ \ table -> do
141
+ indexedForM_ tables $ \ TableMeta {.. } -> do
142
+ -- Save table
143
+ saveTableToCatalog _tmTable _tmIsEnum _tmConfiguration
144
+
152
145
-- Relationships
153
146
withPathK " object_relationships" $
154
- indexedForM_ (table ^. tmObjectRelationships) $ \ objRel ->
155
- Relationship. insertRelationshipToCatalog (table ^. tmTable) ObjRel objRel
147
+ indexedForM_ _tmObjectRelationships $ \ objRel ->
148
+ Relationship. insertRelationshipToCatalog _tmTable ObjRel objRel
156
149
withPathK " array_relationships" $
157
- indexedForM_ (table ^. tmArrayRelationships) $ \ arrRel ->
158
- Relationship. insertRelationshipToCatalog (table ^. tmTable) ArrRel arrRel
150
+ indexedForM_ _tmArrayRelationships $ \ arrRel ->
151
+ Relationship. insertRelationshipToCatalog _tmTable ArrRel arrRel
152
+
159
153
-- Computed Fields
160
154
withPathK " computed_fields" $
161
- indexedForM_ (table ^. tmComputedFields) $
155
+ indexedForM_ _tmComputedFields $
162
156
\ (ComputedFieldMeta name definition comment) ->
163
157
ComputedField. addComputedFieldToCatalog $
164
- ComputedField. AddComputedField (table ^. tmTable) name definition comment
165
-
166
- -- Permissions
167
- indexedForM_ tables $ \ table -> do
168
- let tableName = table ^. tmTable
169
- tabInfo <- modifyErrAndSet500 (" apply " <> ) $ askTableCoreInfo tableName
170
- withPathK " insert_permissions" $ processPerms tabInfo $
171
- table ^. tmInsertPermissions
172
- withPathK " select_permissions" $ processPerms tabInfo $
173
- table ^. tmSelectPermissions
174
- withPathK " update_permissions" $ processPerms tabInfo $
175
- table ^. tmUpdatePermissions
176
- withPathK " delete_permissions" $ processPerms tabInfo $
177
- table ^. tmDeletePermissions
178
-
179
- indexedForM_ tables $ \ table ->
158
+ ComputedField. AddComputedField _tmTable name definition comment
159
+
160
+ -- Permissions
161
+ withPathK " insert_permissions" $ processPerms _tmTable _tmInsertPermissions
162
+ withPathK " select_permissions" $ processPerms _tmTable _tmSelectPermissions
163
+ withPathK " update_permissions" $ processPerms _tmTable _tmUpdatePermissions
164
+ withPathK " delete_permissions" $ processPerms _tmTable _tmDeletePermissions
165
+
166
+ -- Event triggers
180
167
withPathK " event_triggers" $
181
- indexedForM_ (table ^. tmEventTriggers) $ \ etc ->
182
- subTableP2 (table ^. tmTable) False etc
168
+ indexedForM_ _tmEventTriggers $ \ etc -> subTableP2 _tmTable False etc
183
169
184
170
-- sql functions
185
171
withPathK " functions" $ case functionsMeta of
186
- FMVersion1 qualifiedFunctions -> indexedForM_ qualifiedFunctions $
187
- \ qf -> void $ Schema. trackFunctionP2 qf Schema. emptyFunctionConfig
188
- FMVersion2 functionsV2 -> indexedForM_ functionsV2 $
189
- \ (Schema. TrackFunctionV2 function config) -> void $ Schema. trackFunctionP2 function config
172
+ FMVersion1 qualifiedFunctions -> indexedForM_ qualifiedFunctions $
173
+ \ qf -> Schema. saveFunctionToCatalog qf Schema. emptyFunctionConfig
174
+ FMVersion2 functionsV2 -> indexedForM_ functionsV2 $
175
+ \ (Schema. TrackFunctionV2 function config) -> Schema. saveFunctionToCatalog function config
190
176
191
177
-- query collections
192
178
systemDefined <- askSystemDefined
@@ -200,32 +186,30 @@ applyQP2 (ReplaceMetadata _ tables functionsMeta
200
186
201
187
-- remote schemas
202
188
withPathK " remote_schemas" $
203
- indexedMapM_ (void . addRemoteSchemaP2 ) schemas
189
+ indexedMapM_ (liftTx . addRemoteSchemaToCatalog ) schemas
204
190
205
- CustomTypes. persistCustomTypes customTypes
206
-
207
- for_ actions $ \ action -> do
208
- let createAction =
209
- CreateAction (_amName action) (_amDefinition action) (_amComment action)
210
- Action. persistCreateAction createAction
211
- for_ (_amPermissions action) $ \ permission -> do
212
- let createActionPermission = CreateActionPermission (_amName action)
213
- (_apmRole permission) Nothing (_apmComment permission)
214
- Action. persistCreateActionPermission createActionPermission
215
-
216
- buildSchemaCacheStrict
217
- return successMsg
191
+ -- custom types
192
+ withPathK " custom_types" $
193
+ CustomTypes. persistCustomTypes customTypes
218
194
195
+ -- actions
196
+ withPathK " actions" $
197
+ indexedForM_ actions $ \ action -> do
198
+ let createAction =
199
+ CreateAction (_amName action) (_amDefinition action) (_amComment action)
200
+ Action. persistCreateAction createAction
201
+ withPathK " permissions" $
202
+ indexedForM_ (_amPermissions action) $ \ permission -> do
203
+ let createActionPermission = CreateActionPermission (_amName action)
204
+ (_apmRole permission) Nothing (_apmComment permission)
205
+ Action. persistCreateActionPermission createActionPermission
219
206
where
220
- processPerms tabInfo perms = indexedForM_ perms $ Permission. addPermP2 (_tciName tabInfo)
207
+ processPerms tableName perms = indexedForM_ perms $ Permission. addPermP2 tableName
221
208
222
209
runReplaceMetadata
223
- :: ( HasVersion
224
- , MonadIO m
225
- , MonadTx m
210
+ :: ( MonadTx m
226
211
, CacheRWM m
227
212
, HasSystemDefined m
228
- , HasHttpManager m
229
213
)
230
214
=> ReplaceMetadata -> m EncJSON
231
215
runReplaceMetadata q = do
0 commit comments