2019-03-18 19:22:21 +03:00
|
|
|
{-# LANGUAGE TypeApplications #-}
|
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
module Hasura.RQL.DDL.Schema.Table where
|
|
|
|
|
2019-03-22 10:08:42 +03:00
|
|
|
import Hasura.EncJSON
|
2018-11-23 16:02:46 +03:00
|
|
|
import Hasura.GraphQL.RemoteServer
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.DDL.Deps
|
|
|
|
import Hasura.RQL.DDL.Permission
|
|
|
|
import Hasura.RQL.DDL.Permission.Internal
|
|
|
|
import Hasura.RQL.DDL.QueryTemplate
|
|
|
|
import Hasura.RQL.DDL.Relationship
|
2018-11-23 16:02:46 +03:00
|
|
|
import Hasura.RQL.DDL.RemoteSchema
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.RQL.DDL.Schema.Diff
|
2019-01-25 06:31:54 +03:00
|
|
|
import Hasura.RQL.DDL.Schema.Function
|
2019-03-01 12:17:22 +03:00
|
|
|
import Hasura.RQL.DDL.Schema.Rename
|
2019-03-25 20:10:52 +03:00
|
|
|
import Hasura.RQL.DDL.EventTrigger
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.RQL.DDL.Utils
|
|
|
|
import Hasura.RQL.Types
|
2019-01-18 13:45:59 +03:00
|
|
|
import Hasura.Server.Utils (matchRegex)
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.SQL.Types
|
|
|
|
|
|
|
|
import qualified Database.PG.Query as Q
|
2018-11-23 16:02:46 +03:00
|
|
|
import qualified Hasura.GraphQL.Schema as GS
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
import Data.Aeson
|
|
|
|
import Data.Aeson.Casing
|
|
|
|
import Data.Aeson.TH
|
|
|
|
import Instances.TH.Lift ()
|
|
|
|
import Language.Haskell.TH.Syntax (Lift)
|
2018-11-23 16:02:46 +03:00
|
|
|
import Network.URI.Extended ()
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
import qualified Data.HashMap.Strict as M
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Data.Text.Encoding as TE
|
|
|
|
import qualified Database.PostgreSQL.LibPQ as PQ
|
|
|
|
|
|
|
|
delTableFromCatalog :: QualifiedTable -> Q.Tx ()
|
2019-01-25 06:31:54 +03:00
|
|
|
delTableFromCatalog (QualifiedObject sn tn) =
|
2018-06-27 16:11:32 +03:00
|
|
|
Q.unitQ [Q.sql|
|
|
|
|
DELETE FROM "hdb_catalog"."hdb_table"
|
|
|
|
WHERE table_schema = $1 AND table_name = $2
|
|
|
|
|] (sn, tn) False
|
|
|
|
|
|
|
|
saveTableToCatalog :: QualifiedTable -> Q.Tx ()
|
2019-01-25 06:31:54 +03:00
|
|
|
saveTableToCatalog (QualifiedObject sn tn) =
|
2018-06-27 16:11:32 +03:00
|
|
|
Q.unitQ [Q.sql|
|
|
|
|
INSERT INTO "hdb_catalog"."hdb_table" VALUES ($1, $2)
|
|
|
|
|] (sn, tn) False
|
|
|
|
|
|
|
|
-- Build the TableInfo with all its columns
|
2018-06-29 14:00:22 +03:00
|
|
|
getTableInfo :: QualifiedTable -> Bool -> Q.TxE QErr TableInfo
|
2019-01-25 06:31:54 +03:00
|
|
|
getTableInfo qt@(QualifiedObject sn tn) isSystemDefined = do
|
2018-12-19 13:41:06 +03:00
|
|
|
tableData <- Q.catchE defaultTxErrorHandler $
|
|
|
|
Q.listQ $(Q.sqlFromFile "src-rsr/table_info.sql")(sn, tn) True
|
|
|
|
case tableData of
|
|
|
|
[] -> throw400 NotExists $ "no such table/view exists in postgres : " <>> qt
|
2019-03-22 10:08:42 +03:00
|
|
|
[(Q.AltJ cols, Q.AltJ pkeyCols, Q.AltJ cons, Q.AltJ viewInfoM)] ->
|
|
|
|
return $ mkTableInfo qt isSystemDefined cons cols pkeyCols viewInfoM
|
2018-12-19 13:41:06 +03:00
|
|
|
_ -> throw500 $ "more than one row found for: " <>> qt
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
newtype TrackTable
|
|
|
|
= TrackTable
|
|
|
|
{ tName :: QualifiedTable }
|
|
|
|
deriving (Show, Eq, FromJSON, ToJSON, Lift)
|
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
trackExistingTableOrViewP1
|
|
|
|
:: (CacheRM m, UserInfoM m, QErrM m) => TrackTable -> m ()
|
2018-06-27 16:11:32 +03:00
|
|
|
trackExistingTableOrViewP1 (TrackTable vn) = do
|
|
|
|
adminOnly
|
2018-12-13 10:26:15 +03:00
|
|
|
rawSchemaCache <- askSchemaCache
|
2018-06-27 16:11:32 +03:00
|
|
|
when (M.member vn $ scTables rawSchemaCache) $
|
|
|
|
throw400 AlreadyTracked $ "view/table already tracked : " <>> vn
|
|
|
|
|
2018-11-23 16:02:46 +03:00
|
|
|
trackExistingTableOrViewP2Setup
|
|
|
|
:: (QErrM m, CacheRWM m, MonadTx m)
|
|
|
|
=> QualifiedTable -> Bool -> m ()
|
2018-06-29 14:00:22 +03:00
|
|
|
trackExistingTableOrViewP2Setup tn isSystemDefined = do
|
|
|
|
ti <- liftTx $ getTableInfo tn isSystemDefined
|
2018-06-27 16:11:32 +03:00
|
|
|
addTableToCache ti
|
|
|
|
|
2018-06-29 14:00:22 +03:00
|
|
|
trackExistingTableOrViewP2
|
2018-11-23 16:02:46 +03:00
|
|
|
:: (QErrM m, CacheRWM m, MonadTx m, MonadIO m, HasHttpManager m)
|
2019-03-18 19:22:21 +03:00
|
|
|
=> QualifiedTable -> Bool -> m EncJSON
|
2018-06-29 14:00:22 +03:00
|
|
|
trackExistingTableOrViewP2 vn isSystemDefined = do
|
2018-11-23 16:02:46 +03:00
|
|
|
sc <- askSchemaCache
|
|
|
|
let defGCtx = scDefaultRemoteGCtx sc
|
2019-03-01 12:17:22 +03:00
|
|
|
tn = GS.qualObjectToName vn
|
|
|
|
GS.checkConflictingNode defGCtx tn
|
2018-11-23 16:02:46 +03:00
|
|
|
|
2018-06-29 14:00:22 +03:00
|
|
|
trackExistingTableOrViewP2Setup vn isSystemDefined
|
2018-06-27 16:11:32 +03:00
|
|
|
liftTx $ Q.catchE defaultTxErrorHandler $
|
|
|
|
saveTableToCatalog vn
|
2018-11-23 16:02:46 +03:00
|
|
|
|
|
|
|
-- refresh the gCtx in schema cache
|
|
|
|
refreshGCtxMapInSchema
|
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
return successMsg
|
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
runTrackTableQ
|
|
|
|
:: ( QErrM m, CacheRWM m, MonadTx m
|
|
|
|
, MonadIO m, HasHttpManager m, UserInfoM m
|
|
|
|
)
|
2019-03-18 19:22:21 +03:00
|
|
|
=> TrackTable -> m EncJSON
|
2018-12-13 10:26:15 +03:00
|
|
|
runTrackTableQ q = do
|
|
|
|
trackExistingTableOrViewP1 q
|
|
|
|
trackExistingTableOrViewP2 (tName q) False
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
purgeDep :: (CacheRWM m, MonadTx m)
|
2018-06-27 16:11:32 +03:00
|
|
|
=> SchemaObjId -> m ()
|
|
|
|
purgeDep schemaObjId = case schemaObjId of
|
|
|
|
(SOTableObj tn (TOPerm rn pt)) -> do
|
|
|
|
liftTx $ dropPermFromCatalog tn rn pt
|
|
|
|
withPermType pt delPermFromCache rn tn
|
|
|
|
|
|
|
|
(SOTableObj qt (TORel rn)) -> do
|
|
|
|
liftTx $ delRelFromCatalog qt rn
|
2018-11-16 15:40:23 +03:00
|
|
|
delRelFromCache rn qt
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
(SOQTemplate qtn) -> do
|
|
|
|
liftTx $ delQTemplateFromCatalog qtn
|
|
|
|
delQTemplateFromCache qtn
|
|
|
|
|
2019-01-25 06:31:54 +03:00
|
|
|
(SOFunction qf) -> do
|
|
|
|
liftTx $ delFunctionFromCatalog qf
|
|
|
|
delFunctionFromCache qf
|
|
|
|
|
2018-09-05 14:26:46 +03:00
|
|
|
(SOTableObj qt (TOTrigger trn)) -> do
|
|
|
|
liftTx $ delEventTriggerFromCatalog trn
|
|
|
|
delEventTriggerFromCache qt trn
|
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
_ -> throw500 $
|
|
|
|
"unexpected dependent object : " <> reportSchemaObj schemaObjId
|
|
|
|
|
2019-03-01 12:17:22 +03:00
|
|
|
processTableChanges :: (MonadTx m, CacheRWM m)
|
|
|
|
=> TableInfo -> TableDiff -> m Bool
|
2018-06-27 16:11:32 +03:00
|
|
|
processTableChanges ti tableDiff = do
|
2019-03-01 12:17:22 +03:00
|
|
|
-- If table rename occurs then don't replace constraints and
|
|
|
|
-- process dropped/added columns, because schema reload happens eventually
|
2018-06-27 16:11:32 +03:00
|
|
|
sc <- askSchemaCache
|
2019-03-01 12:17:22 +03:00
|
|
|
let tn = tiName ti
|
|
|
|
withOldTabName = do
|
|
|
|
-- replace constraints
|
|
|
|
replaceConstraints tn
|
|
|
|
-- for all the dropped columns
|
|
|
|
procDroppedCols tn
|
|
|
|
-- for all added columns
|
|
|
|
procAddedCols tn
|
|
|
|
-- for all altered columns
|
|
|
|
procAlteredCols sc tn
|
|
|
|
|
|
|
|
withNewTabName newTN = do
|
|
|
|
let tnGQL = GS.qualObjectToName newTN
|
|
|
|
defGCtx = scDefaultRemoteGCtx sc
|
|
|
|
-- check for GraphQL schema conflicts on new name
|
|
|
|
GS.checkConflictingNode defGCtx tnGQL
|
|
|
|
void $ procAlteredCols sc tn
|
|
|
|
-- update new table in catalog
|
|
|
|
renameTableInCatalog newTN tn
|
|
|
|
return True
|
|
|
|
|
|
|
|
maybe withOldTabName withNewTabName mNewName
|
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
where
|
2019-01-03 06:58:12 +03:00
|
|
|
TableDiff mNewName droppedCols addedCols alteredCols _ constraints = tableDiff
|
2019-03-01 12:17:22 +03:00
|
|
|
replaceConstraints tn = flip modTableInCache tn $ \tInfo ->
|
|
|
|
return $ tInfo {tiUniqOrPrimConstraints = constraints}
|
|
|
|
|
|
|
|
procDroppedCols tn =
|
|
|
|
forM_ droppedCols $ \droppedCol ->
|
|
|
|
-- Drop the column from the cache
|
|
|
|
delColFromCache droppedCol tn
|
|
|
|
|
|
|
|
procAddedCols tn =
|
|
|
|
-- In the newly added columns check that there is no conflict with relationships
|
|
|
|
forM_ addedCols $ \pci@(PGColInfo colName _ _) ->
|
|
|
|
case M.lookup (fromPGCol colName) $ tiFieldInfoMap ti of
|
|
|
|
Just (FIRelationship _) ->
|
|
|
|
throw400 AlreadyExists $ "cannot add column " <> colName
|
|
|
|
<<> " in table " <> tn <<>
|
|
|
|
" as a relationship with the name already exists"
|
|
|
|
_ -> addColToCache colName pci tn
|
|
|
|
|
|
|
|
procAlteredCols sc tn = fmap or $
|
|
|
|
forM alteredCols $ \(PGColInfo oColName oColTy _, PGColInfo nColName nColTy _) ->
|
|
|
|
if | oColName /= nColName -> do
|
|
|
|
renameColInCatalog oColName nColName tn ti
|
|
|
|
return True
|
|
|
|
| oColTy /= nColTy -> do
|
|
|
|
let colId = SOTableObj tn $ TOCol oColName
|
|
|
|
depObjs = getDependentObjsWith (== "on_type") sc colId
|
|
|
|
unless (null depObjs) $ throw400 DependencyError $
|
|
|
|
"cannot change type of column " <> oColName <<> " in table "
|
|
|
|
<> tn <<> " because of the following dependencies : " <>
|
|
|
|
reportSchemaObjs depObjs
|
|
|
|
return False
|
|
|
|
| otherwise -> return False
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
delTableAndDirectDeps
|
|
|
|
:: (QErrM m, CacheRWM m, MonadTx m) => QualifiedTable -> m ()
|
2019-01-25 06:31:54 +03:00
|
|
|
delTableAndDirectDeps qtn@(QualifiedObject sn tn) = do
|
2018-11-16 15:40:23 +03:00
|
|
|
liftTx $ Q.catchE defaultTxErrorHandler $ do
|
|
|
|
Q.unitQ [Q.sql|
|
|
|
|
DELETE FROM "hdb_catalog"."hdb_relationship"
|
|
|
|
WHERE table_schema = $1 AND table_name = $2
|
|
|
|
|] (sn, tn) False
|
|
|
|
Q.unitQ [Q.sql|
|
|
|
|
DELETE FROM "hdb_catalog"."hdb_permission"
|
|
|
|
WHERE table_schema = $1 AND table_name = $2
|
|
|
|
|] (sn, tn) False
|
|
|
|
Q.unitQ [Q.sql|
|
|
|
|
DELETE FROM "hdb_catalog"."event_triggers"
|
|
|
|
WHERE schema_name = $1 AND table_name = $2
|
|
|
|
|] (sn, tn) False
|
|
|
|
delTableFromCatalog qtn
|
|
|
|
delTableFromCache qtn
|
|
|
|
|
2019-03-01 12:17:22 +03:00
|
|
|
processSchemaChanges :: (MonadTx m, CacheRWM m) => SchemaDiff -> m Bool
|
2018-06-27 16:11:32 +03:00
|
|
|
processSchemaChanges schemaDiff = do
|
|
|
|
-- Purge the dropped tables
|
2018-11-16 15:40:23 +03:00
|
|
|
mapM_ delTableAndDirectDeps droppedTables
|
2019-03-01 12:17:22 +03:00
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
sc <- askSchemaCache
|
2019-03-01 12:17:22 +03:00
|
|
|
fmap or $ forM alteredTables $ \(oldQtn, tableDiff) -> do
|
2018-06-27 16:11:32 +03:00
|
|
|
ti <- case M.lookup oldQtn $ scTables sc of
|
|
|
|
Just ti -> return ti
|
|
|
|
Nothing -> throw500 $ "old table metadata not found in cache : " <>> oldQtn
|
|
|
|
processTableChanges ti tableDiff
|
|
|
|
where
|
|
|
|
SchemaDiff droppedTables alteredTables = schemaDiff
|
|
|
|
|
|
|
|
data UntrackTable =
|
|
|
|
UntrackTable
|
|
|
|
{ utTable :: !QualifiedTable
|
|
|
|
, utCascade :: !(Maybe Bool)
|
|
|
|
} deriving (Show, Eq, Lift)
|
|
|
|
$(deriveJSON (aesonDrop 2 snakeCase){omitNothingFields=True} ''UntrackTable)
|
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
unTrackExistingTableOrViewP1
|
|
|
|
:: (CacheRM m, UserInfoM m, QErrM m) => UntrackTable -> m ()
|
2018-11-16 15:40:23 +03:00
|
|
|
unTrackExistingTableOrViewP1 (UntrackTable vn _) = do
|
2018-06-27 16:11:32 +03:00
|
|
|
adminOnly
|
2018-12-13 10:26:15 +03:00
|
|
|
rawSchemaCache <- askSchemaCache
|
2018-06-27 16:11:32 +03:00
|
|
|
case M.lookup vn (scTables rawSchemaCache) of
|
2018-11-16 15:40:23 +03:00
|
|
|
Just ti ->
|
2018-06-29 14:00:22 +03:00
|
|
|
-- Check if table/view is system defined
|
|
|
|
when (tiSystemDefined ti) $ throw400 NotSupported $
|
|
|
|
vn <<> " is system defined, cannot untrack"
|
2018-06-27 16:11:32 +03:00
|
|
|
Nothing -> throw400 AlreadyUntracked $
|
|
|
|
"view/table already untracked : " <>> vn
|
|
|
|
|
2018-11-16 15:40:23 +03:00
|
|
|
unTrackExistingTableOrViewP2
|
2018-11-23 16:02:46 +03:00
|
|
|
:: (QErrM m, CacheRWM m, MonadTx m, MonadIO m, HasHttpManager m)
|
2019-03-18 19:22:21 +03:00
|
|
|
=> UntrackTable -> m EncJSON
|
2018-11-16 15:40:23 +03:00
|
|
|
unTrackExistingTableOrViewP2 (UntrackTable qtn cascade) = do
|
2018-06-27 16:11:32 +03:00
|
|
|
sc <- askSchemaCache
|
|
|
|
|
2019-01-25 06:31:54 +03:00
|
|
|
-- Get relational, query template and function dependants
|
2018-11-16 15:40:23 +03:00
|
|
|
let allDeps = getDependentObjs sc (SOTable qtn)
|
|
|
|
indirectDeps = filter (not . isDirectDep) allDeps
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
-- Report bach with an error if cascade is not set
|
2018-11-16 15:40:23 +03:00
|
|
|
when (indirectDeps /= [] && not (or cascade)) $ reportDepsExt indirectDeps []
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
-- Purge all the dependants from state
|
2018-11-16 15:40:23 +03:00
|
|
|
mapM_ purgeDep indirectDeps
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-11-16 15:40:23 +03:00
|
|
|
-- delete the table and its direct dependencies
|
|
|
|
delTableAndDirectDeps qtn
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-11-23 16:02:46 +03:00
|
|
|
-- refresh the gctxmap in schema cache
|
|
|
|
refreshGCtxMapInSchema
|
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
return successMsg
|
|
|
|
where
|
2018-11-16 15:40:23 +03:00
|
|
|
isDirectDep = \case
|
|
|
|
(SOTableObj dtn _) -> qtn == dtn
|
|
|
|
_ -> False
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
runUntrackTableQ
|
|
|
|
:: ( QErrM m, CacheRWM m, MonadTx m
|
|
|
|
, MonadIO m, HasHttpManager m, UserInfoM m
|
|
|
|
)
|
2019-03-18 19:22:21 +03:00
|
|
|
=> UntrackTable -> m EncJSON
|
2018-12-13 10:26:15 +03:00
|
|
|
runUntrackTableQ q = do
|
|
|
|
unTrackExistingTableOrViewP1 q
|
|
|
|
unTrackExistingTableOrViewP2 q
|
|
|
|
|
|
|
|
buildSchemaCache
|
2019-03-01 14:45:04 +03:00
|
|
|
:: (MonadTx m, CacheRWM m, MonadIO m, HasHttpManager m, HasSQLGenCtx m)
|
2018-12-13 10:26:15 +03:00
|
|
|
=> m ()
|
|
|
|
buildSchemaCache = do
|
2019-01-29 13:09:58 +03:00
|
|
|
-- clean hdb_views
|
|
|
|
liftTx $ Q.catchE defaultTxErrorHandler clearHdbViews
|
2018-12-13 10:26:15 +03:00
|
|
|
-- reset the current schemacache
|
|
|
|
writeSchemaCache emptySchemaCache
|
|
|
|
hMgr <- askHttpManager
|
2019-03-01 14:45:04 +03:00
|
|
|
strfyNum <- stringifyNum <$> askSQLGenCtx
|
2018-12-13 10:26:15 +03:00
|
|
|
tables <- liftTx $ Q.catchE defaultTxErrorHandler fetchTables
|
2018-06-29 14:00:22 +03:00
|
|
|
forM_ tables $ \(sn, tn, isSystemDefined) ->
|
2018-06-27 16:11:32 +03:00
|
|
|
modifyErr (\e -> "table " <> tn <<> "; " <> e) $
|
2019-01-25 06:31:54 +03:00
|
|
|
trackExistingTableOrViewP2Setup (QualifiedObject sn tn) isSystemDefined
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
-- Fetch all the relationships
|
2018-12-13 10:26:15 +03:00
|
|
|
relationships <- liftTx $ Q.catchE defaultTxErrorHandler fetchRelationships
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2019-03-25 11:56:29 +03:00
|
|
|
forM_ relationships $ \(sn, tn, rn, rt, Q.AltJ rDef) -> do
|
|
|
|
let qt = QualifiedObject sn tn
|
2018-06-27 16:11:32 +03:00
|
|
|
modifyErr (\e -> "table " <> tn <<> "; rel " <> rn <<> "; " <> e) $ case rt of
|
2019-03-25 11:56:29 +03:00
|
|
|
ObjRel -> do
|
|
|
|
using <- decodeValue rDef
|
|
|
|
let relDef = RelDef rn using Nothing
|
|
|
|
validateObjRel qt relDef
|
|
|
|
objRelP2Setup qt relDef
|
|
|
|
ArrRel -> do
|
|
|
|
using <- decodeValue rDef
|
|
|
|
let relDef = RelDef rn using Nothing
|
|
|
|
validateArrRel qt relDef
|
|
|
|
arrRelP2Setup qt relDef
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
-- Fetch all the permissions
|
2018-12-13 10:26:15 +03:00
|
|
|
permissions <- liftTx $ Q.catchE defaultTxErrorHandler fetchPermissions
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
forM_ permissions $ \(sn, tn, rn, pt, Q.AltJ pDef) ->
|
|
|
|
modifyErr (\e -> "table " <> tn <<> "; role " <> rn <<> "; " <> e) $ case pt of
|
2019-03-01 14:45:04 +03:00
|
|
|
PTInsert -> permHelper strfyNum sn tn rn pDef PAInsert
|
|
|
|
PTSelect -> permHelper strfyNum sn tn rn pDef PASelect
|
|
|
|
PTUpdate -> permHelper strfyNum sn tn rn pDef PAUpdate
|
|
|
|
PTDelete -> permHelper strfyNum sn tn rn pDef PADelete
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
-- Fetch all the query templates
|
2018-12-13 10:26:15 +03:00
|
|
|
qtemplates <- liftTx $ Q.catchE defaultTxErrorHandler fetchQTemplates
|
2018-06-27 16:11:32 +03:00
|
|
|
forM_ qtemplates $ \(qtn, Q.AltJ qtDefVal) -> do
|
|
|
|
qtDef <- decodeValue qtDefVal
|
2019-03-01 14:45:04 +03:00
|
|
|
qCtx <- mkAdminQCtx strfyNum <$> askSchemaCache
|
2018-12-13 10:26:15 +03:00
|
|
|
(qti, deps) <- liftP1WithQCtx qCtx $ createQueryTemplateP1 $
|
2018-06-27 16:11:32 +03:00
|
|
|
CreateQueryTemplate qtn qtDef Nothing
|
2018-11-16 15:40:23 +03:00
|
|
|
addQTemplateToCache qti deps
|
2018-09-05 14:26:46 +03:00
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
eventTriggers <- liftTx $ Q.catchE defaultTxErrorHandler fetchEventTriggers
|
2019-03-25 20:10:52 +03:00
|
|
|
forM_ eventTriggers $ \(sn, tn, trn, Q.AltJ configuration) -> do
|
2018-11-14 10:13:01 +03:00
|
|
|
etc <- decodeValue configuration
|
|
|
|
|
2019-01-25 06:31:54 +03:00
|
|
|
let qt = QualifiedObject sn tn
|
2019-03-25 20:10:52 +03:00
|
|
|
subTableP2Setup qt etc
|
2018-10-25 10:22:51 +03:00
|
|
|
allCols <- getCols . tiFieldInfoMap <$> askTabInfo qt
|
2019-03-25 20:10:52 +03:00
|
|
|
liftTx $ mkTriggerQ trn qt allCols strfyNum (etcDefinition etc)
|
2018-11-23 16:02:46 +03:00
|
|
|
|
2019-01-25 06:31:54 +03:00
|
|
|
functions <- liftTx $ Q.catchE defaultTxErrorHandler fetchFunctions
|
|
|
|
forM_ functions $ \(sn, fn) ->
|
|
|
|
modifyErr (\e -> "function " <> fn <<> "; " <> e) $
|
|
|
|
trackFunctionP2Setup (QualifiedObject sn fn)
|
|
|
|
|
2018-11-23 16:02:46 +03:00
|
|
|
-- remote schemas
|
|
|
|
res <- liftTx fetchRemoteSchemas
|
|
|
|
sc <- askSchemaCache
|
2019-01-25 06:31:54 +03:00
|
|
|
gCtxMap <- GS.mkGCtxMap (scTables sc) (scFunctions sc)
|
2018-11-23 16:02:46 +03:00
|
|
|
|
|
|
|
remoteScConf <- forM res $ \(AddRemoteSchemaQuery n def _) ->
|
|
|
|
(,) n <$> validateRemoteSchemaDef def
|
|
|
|
let rmScMap = M.fromList remoteScConf
|
2018-12-13 10:26:15 +03:00
|
|
|
(mergedGCtxMap, defGCtx) <- mergeSchemas rmScMap gCtxMap hMgr
|
2018-11-23 16:02:46 +03:00
|
|
|
writeRemoteSchemasToCache mergedGCtxMap rmScMap
|
|
|
|
postMergeSc <- askSchemaCache
|
|
|
|
writeSchemaCache postMergeSc { scDefaultRemoteGCtx = defGCtx }
|
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
where
|
2019-03-01 14:45:04 +03:00
|
|
|
permHelper strfyNum sn tn rn pDef pa = do
|
|
|
|
qCtx <- mkAdminQCtx strfyNum <$> askSchemaCache
|
2018-06-27 16:11:32 +03:00
|
|
|
perm <- decodeValue pDef
|
2019-01-25 06:31:54 +03:00
|
|
|
let qt = QualifiedObject sn tn
|
2018-06-27 16:11:32 +03:00
|
|
|
permDef = PermDef rn perm Nothing
|
|
|
|
createPerm = WithTable qt permDef
|
2018-12-13 10:26:15 +03:00
|
|
|
(permInfo, deps) <- liftP1WithQCtx qCtx $ createPermP1 createPerm
|
2018-11-16 15:40:23 +03:00
|
|
|
addPermP2Setup qt permDef permInfo
|
|
|
|
addPermToCache qt rn pa permInfo deps
|
2018-06-27 16:11:32 +03:00
|
|
|
-- p2F qt rn p1Res
|
|
|
|
|
|
|
|
fetchTables =
|
|
|
|
Q.listQ [Q.sql|
|
2018-06-29 14:00:22 +03:00
|
|
|
SELECT table_schema, table_name, is_system_defined
|
|
|
|
FROM hdb_catalog.hdb_table
|
2018-06-27 16:11:32 +03:00
|
|
|
|] () False
|
|
|
|
|
|
|
|
fetchRelationships =
|
|
|
|
Q.listQ [Q.sql|
|
|
|
|
SELECT table_schema, table_name, rel_name, rel_type, rel_def::json
|
|
|
|
FROM hdb_catalog.hdb_relationship
|
|
|
|
|] () False
|
|
|
|
|
|
|
|
fetchPermissions =
|
|
|
|
Q.listQ [Q.sql|
|
|
|
|
SELECT table_schema, table_name, role_name, perm_type, perm_def::json
|
|
|
|
FROM hdb_catalog.hdb_permission
|
|
|
|
|] () False
|
|
|
|
|
|
|
|
fetchQTemplates =
|
|
|
|
Q.listQ [Q.sql|
|
|
|
|
SELECT template_name, template_defn :: json FROM hdb_catalog.hdb_query_template
|
|
|
|
|] () False
|
|
|
|
|
2018-09-05 14:26:46 +03:00
|
|
|
fetchEventTriggers =
|
|
|
|
Q.listQ [Q.sql|
|
2019-03-25 20:10:52 +03:00
|
|
|
SELECT e.schema_name, e.table_name, e.name, e.configuration::json
|
2018-09-05 14:26:46 +03:00
|
|
|
FROM hdb_catalog.event_triggers e
|
|
|
|
|] () False
|
2019-01-25 06:31:54 +03:00
|
|
|
fetchFunctions =
|
|
|
|
Q.listQ [Q.sql|
|
|
|
|
SELECT function_schema, function_name
|
|
|
|
FROM hdb_catalog.hdb_function
|
|
|
|
|] () False
|
2018-09-05 14:26:46 +03:00
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
data RunSQL
|
|
|
|
= RunSQL
|
2019-01-18 13:45:59 +03:00
|
|
|
{ rSql :: T.Text
|
|
|
|
, rCascade :: !(Maybe Bool)
|
|
|
|
, rCheckMetadataConsistency :: !(Maybe Bool)
|
2018-06-27 16:11:32 +03:00
|
|
|
} deriving (Show, Eq, Lift)
|
|
|
|
|
|
|
|
$(deriveJSON (aesonDrop 1 snakeCase){omitNothingFields=True} ''RunSQL)
|
|
|
|
|
|
|
|
data RunSQLRes
|
|
|
|
= RunSQLRes
|
|
|
|
{ rrResultType :: !T.Text
|
|
|
|
, rrResult :: !Value
|
|
|
|
} deriving (Show, Eq)
|
|
|
|
|
|
|
|
$(deriveJSON (aesonDrop 2 snakeCase){omitNothingFields=True} ''RunSQLRes)
|
|
|
|
|
2019-03-18 19:22:21 +03:00
|
|
|
instance Q.FromRes RunSQLRes where
|
|
|
|
fromRes (Q.ResultOkEmpty _) =
|
|
|
|
return $ RunSQLRes "CommandOk" Null
|
|
|
|
fromRes (Q.ResultOkData res) = do
|
|
|
|
csvRows <- resToCSV res
|
|
|
|
return $ RunSQLRes "TuplesOk" $ toJSON csvRows
|
|
|
|
|
|
|
|
execRawSQL :: (MonadTx m) => T.Text -> m EncJSON
|
2019-01-18 13:45:59 +03:00
|
|
|
execRawSQL =
|
2019-03-18 19:22:21 +03:00
|
|
|
fmap (encJFromJValue @RunSQLRes) .
|
2019-01-18 13:45:59 +03:00
|
|
|
liftTx . Q.multiQE rawSqlErrHandler . Q.fromText
|
|
|
|
where
|
|
|
|
rawSqlErrHandler txe =
|
|
|
|
let e = err400 PostgresError "query execution failed"
|
|
|
|
in e {qeInternal = Just $ toJSON txe}
|
|
|
|
|
|
|
|
execWithMDCheck
|
2019-03-01 14:45:04 +03:00
|
|
|
:: (QErrM m, CacheRWM m, MonadTx m, MonadIO m, HasHttpManager m, HasSQLGenCtx m)
|
2019-03-18 19:22:21 +03:00
|
|
|
=> RunSQL -> m EncJSON
|
2019-01-18 13:45:59 +03:00
|
|
|
execWithMDCheck (RunSQL t cascade _) = do
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
-- Drop hdb_views so no interference is caused to the sql query
|
2018-09-05 14:26:46 +03:00
|
|
|
liftTx $ Q.catchE defaultTxErrorHandler clearHdbViews
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
-- Get the metadata before the sql query, everything, need to filter this
|
|
|
|
oldMetaU <- liftTx $ Q.catchE defaultTxErrorHandler fetchTableMeta
|
2019-01-25 06:31:54 +03:00
|
|
|
oldFuncMetaU <-
|
|
|
|
liftTx $ Q.catchE defaultTxErrorHandler fetchFunctionMeta
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
-- Run the SQL
|
2019-01-18 13:45:59 +03:00
|
|
|
res <- execRawSQL t
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
-- Get the metadata after the sql query
|
|
|
|
newMeta <- liftTx $ Q.catchE defaultTxErrorHandler fetchTableMeta
|
2019-01-25 06:31:54 +03:00
|
|
|
newFuncMeta <- liftTx $ Q.catchE defaultTxErrorHandler fetchFunctionMeta
|
2018-06-27 16:11:32 +03:00
|
|
|
sc <- askSchemaCache
|
|
|
|
let existingTables = M.keys $ scTables sc
|
|
|
|
oldMeta = flip filter oldMetaU $ \tm -> tmTable tm `elem` existingTables
|
|
|
|
schemaDiff = getSchemaDiff oldMeta newMeta
|
2019-01-25 06:31:54 +03:00
|
|
|
existingFuncs = M.keys $ scFunctions sc
|
2019-02-05 08:57:03 +03:00
|
|
|
oldFuncMeta = flip filter oldFuncMetaU $ \fm -> funcFromMeta fm `elem` existingFuncs
|
|
|
|
FunctionDiff droppedFuncs alteredFuncs = getFuncDiff oldFuncMeta newFuncMeta
|
2019-02-14 07:05:18 +03:00
|
|
|
overloadedFuncs = getOverloadedFuncs existingFuncs newFuncMeta
|
|
|
|
|
|
|
|
-- Do not allow overloading functions
|
|
|
|
unless (null overloadedFuncs) $
|
|
|
|
throw400 NotSupported $ "the following tracked function(s) cannot be overloaded: "
|
|
|
|
<> reportFuncs overloadedFuncs
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
indirectDeps <- getSchemaChangeDeps schemaDiff
|
|
|
|
|
|
|
|
-- Report back with an error if cascade is not set
|
|
|
|
when (indirectDeps /= [] && not (or cascade)) $ reportDepsExt indirectDeps []
|
|
|
|
|
|
|
|
-- Purge all the indirect dependents from state
|
|
|
|
mapM_ purgeDep indirectDeps
|
|
|
|
|
2019-01-25 06:31:54 +03:00
|
|
|
-- Purge all dropped functions
|
|
|
|
let purgedFuncs = flip mapMaybe indirectDeps $ \dep ->
|
|
|
|
case dep of
|
|
|
|
SOFunction qf -> Just qf
|
|
|
|
_ -> Nothing
|
|
|
|
|
|
|
|
forM_ (droppedFuncs \\ purgedFuncs) $ \qf -> do
|
|
|
|
liftTx $ delFunctionFromCatalog qf
|
|
|
|
delFunctionFromCache qf
|
|
|
|
|
2019-02-05 08:57:03 +03:00
|
|
|
-- Process altered functions
|
|
|
|
forM_ alteredFuncs $ \(qf, newTy) ->
|
|
|
|
when (newTy == FTVOLATILE) $
|
|
|
|
throw400 NotSupported $
|
|
|
|
"type of function " <> qf <<> " is altered to \"VOLATILE\" which is not supported now"
|
|
|
|
|
2019-03-01 12:17:22 +03:00
|
|
|
-- update the schema cache and hdb_catalog with the changes
|
|
|
|
reloadRequired <- processSchemaChanges schemaDiff
|
|
|
|
|
|
|
|
let withReload = buildSchemaCache
|
|
|
|
withoutReload = do
|
|
|
|
postSc <- askSchemaCache
|
|
|
|
-- recreate the insert permission infra
|
|
|
|
forM_ (M.elems $ scTables postSc) $ \ti -> do
|
|
|
|
let tn = tiName ti
|
|
|
|
forM_ (M.elems $ tiRolePermInfoMap ti) $ \rpi ->
|
|
|
|
maybe (return ()) (liftTx . buildInsInfra tn) $ _permIns rpi
|
|
|
|
|
2019-03-01 14:45:04 +03:00
|
|
|
strfyNum <- stringifyNum <$> askSQLGenCtx
|
2019-03-01 12:17:22 +03:00
|
|
|
--recreate triggers
|
|
|
|
forM_ (M.elems $ scTables postSc) $ \ti -> do
|
|
|
|
let tn = tiName ti
|
|
|
|
cols = getCols $ tiFieldInfoMap ti
|
|
|
|
forM_ (M.toList $ tiEventTriggerInfoMap ti) $ \(trn, eti) -> do
|
|
|
|
let opsDef = etiOpsDef eti
|
2019-03-25 20:10:52 +03:00
|
|
|
liftTx $ mkTriggerQ trn tn cols strfyNum opsDef
|
2019-03-01 12:17:22 +03:00
|
|
|
|
|
|
|
bool withoutReload withReload reloadRequired
|
2018-09-05 14:26:46 +03:00
|
|
|
|
2018-11-23 16:02:46 +03:00
|
|
|
-- refresh the gCtxMap in schema cache
|
|
|
|
refreshGCtxMapInSchema
|
|
|
|
|
2019-01-18 13:45:59 +03:00
|
|
|
return res
|
2019-02-14 07:05:18 +03:00
|
|
|
where
|
|
|
|
reportFuncs = T.intercalate ", " . map dquoteTxt
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2019-01-18 13:45:59 +03:00
|
|
|
isAltrDropReplace :: QErrM m => T.Text -> m Bool
|
|
|
|
isAltrDropReplace = either throwErr return . matchRegex regex False
|
2018-06-27 16:11:32 +03:00
|
|
|
where
|
2019-01-18 13:45:59 +03:00
|
|
|
throwErr s = throw500 $ "compiling regex failed: " <> T.pack s
|
2019-02-14 07:05:18 +03:00
|
|
|
regex = "alter|drop|replace|create function"
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
runRunSQL
|
2019-03-01 14:45:04 +03:00
|
|
|
:: (QErrM m, UserInfoM m, CacheRWM m, MonadTx m, MonadIO m, HasHttpManager m, HasSQLGenCtx m)
|
2019-03-18 19:22:21 +03:00
|
|
|
=> RunSQL -> m EncJSON
|
2019-01-18 13:45:59 +03:00
|
|
|
runRunSQL q@(RunSQL t _ mChkMDCnstcy) = do
|
|
|
|
adminOnly
|
|
|
|
isMDChkNeeded <- maybe (isAltrDropReplace t) return mChkMDCnstcy
|
2019-03-18 19:22:21 +03:00
|
|
|
bool (execRawSQL t) (execWithMDCheck q) isMDChkNeeded
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
-- Should be used only after checking the status
|
|
|
|
resToCSV :: PQ.Result -> ExceptT T.Text IO [[T.Text]]
|
|
|
|
resToCSV r = do
|
|
|
|
nr <- liftIO $ PQ.ntuples r
|
|
|
|
nc <- liftIO $ PQ.nfields r
|
|
|
|
|
|
|
|
hdr <- forM [0..pred nc] $ \ic -> do
|
|
|
|
colNameBS <- liftIO $ PQ.fname r ic
|
|
|
|
maybe (return "unknown") decodeBS colNameBS
|
|
|
|
|
|
|
|
rows <- forM [0..pred nr] $ \ir ->
|
|
|
|
forM [0..pred nc] $ \ic -> do
|
|
|
|
cellValBS <- liftIO $ PQ.getvalue r ir ic
|
|
|
|
maybe (return "NULL") decodeBS cellValBS
|
|
|
|
|
|
|
|
return $ hdr:rows
|
|
|
|
|
|
|
|
where
|
|
|
|
decodeBS = either (throwError . T.pack . show) return . TE.decodeUtf8'
|