这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion server/src-lib/Hasura/RQL/DDL/Metadata.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import qualified Data.Text as T

import Hasura.GraphQL.Utils
import Hasura.Prelude
import Hasura.RQL.DDL.Utils
import Hasura.RQL.Types
import Hasura.SQL.Types

Expand Down
61 changes: 44 additions & 17 deletions server/src-lib/Hasura/RQL/DDL/Schema/Diff.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ module Hasura.RQL.DDL.Schema.Diff
, getSchemaChangeDeps

, FunctionMeta(..)
, funcFromMeta
, fetchFunctionMeta
, getDroppedFuncs
, FunctionDiff(..)
, getFuncDiff
) where

import Hasura.Prelude
Expand Down Expand Up @@ -226,25 +228,50 @@ getSchemaChangeDeps schemaDiff = do

data FunctionMeta
= FunctionMeta
{ fmOid :: !Int
, fmFunction :: !QualifiedFunction
{ fmOid :: !Int
, fmSchema :: !SchemaName
, fmName :: !FunctionName
, fmType :: !FunctionType
} deriving (Show, Eq)
$(deriveJSON (aesonDrop 2 snakeCase) ''FunctionMeta)

funcFromMeta :: FunctionMeta -> QualifiedFunction
funcFromMeta fm = QualifiedObject (fmSchema fm) (fmName fm)

fetchFunctionMeta :: Q.Tx [FunctionMeta]
fetchFunctionMeta = do
res <- Q.listQ [Q.sql|
map (Q.getAltJ . runIdentity) <$> Q.listQ [Q.sql|
SELECT
f.function_schema,
f.function_name,
p.oid
FROM hdb_catalog.hdb_function_agg f
JOIN pg_catalog.pg_proc p ON (p.proname = f.function_name)
json_build_object(
'oid', p.oid :: integer,
'schema', f.function_schema,
'name', f.function_name,
'type', f.function_type
) AS function_meta
FROM
hdb_catalog.hdb_function_agg f
JOIN pg_catalog.pg_proc p ON (p.proname = f.function_name)
JOIN pg_catalog.pg_namespace pn ON (
pn.oid = p.pronamespace
AND pn.nspname = f.function_schema
)
WHERE
f.function_schema <> 'hdb_catalog'
|] () False
forM res $ \(sn, fn, foid) ->
return $ FunctionMeta foid $ QualifiedObject sn fn

getDroppedFuncs :: [FunctionMeta] -> [FunctionMeta] -> [QualifiedFunction]
getDroppedFuncs oldMeta newMeta =
map fmFunction $ getDifference fmOid oldMeta newMeta
f.function_schema <> 'hdb_catalog'
|] () False

data FunctionDiff
= FunctionDiff
{ fdDropped :: ![QualifiedFunction]
, fdAltered :: ![(QualifiedFunction, FunctionType)]
} deriving (Show, Eq)

getFuncDiff :: [FunctionMeta] -> [FunctionMeta] -> FunctionDiff
getFuncDiff oldMeta newMeta =
FunctionDiff droppedFuncs alteredFuncs
where
droppedFuncs = map funcFromMeta $ getDifference fmOid oldMeta newMeta
alteredFuncs = mapMaybe mkAltered $ getOverlap fmOid oldMeta newMeta
mkAltered (oldfm, newfm) =
let isTypeAltered = fmType oldfm /= fmType newfm
alteredFunc = (funcFromMeta oldfm, fmType newfm)
in bool Nothing (Just alteredFunc) isTypeAltered
10 changes: 8 additions & 2 deletions server/src-lib/Hasura/RQL/DDL/Schema/Table.hs
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,8 @@ execWithMDCheck (RunSQL t cascade _) = do
oldMeta = flip filter oldMetaU $ \tm -> tmTable tm `elem` existingTables
schemaDiff = getSchemaDiff oldMeta newMeta
existingFuncs = M.keys $ scFunctions sc
oldFuncMeta = flip filter oldFuncMetaU $ \fm -> fmFunction fm `elem` existingFuncs
droppedFuncs = getDroppedFuncs oldFuncMeta newFuncMeta
oldFuncMeta = flip filter oldFuncMetaU $ \fm -> funcFromMeta fm `elem` existingFuncs
FunctionDiff droppedFuncs alteredFuncs = getFuncDiff oldFuncMeta newFuncMeta

indirectDeps <- getSchemaChangeDeps schemaDiff

Expand All @@ -461,6 +461,12 @@ execWithMDCheck (RunSQL t cascade _) = do
liftTx $ delFunctionFromCatalog qf
delFunctionFromCache qf

-- 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"

-- update the schema cache with the changes
processSchemaChanges schemaDiff

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
description: Alter SQL function to type VOLATILE (error)
url: /v1/query
status: 400
reponse:
path: "$.args"
error: type of function "search_posts" is altered to "VOLATILE" which is not supported
now
code: not-supported
query:
type: run_sql
args:
sql: |
create or replace function search_posts(search text)
returns setof post as $$
insert into post (title, content) values (search, search)
returning *
$$ language sql volatile;
3 changes: 3 additions & 0 deletions server/tests-py/test_graphql_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ def test_search_posts(self, hge_ctx):
def test_search_posts_aggregate(self, hge_ctx):
check_query_f(hge_ctx, self.dir() + "/query_search_posts_aggregate.yaml")

def test_alter_function_error(self, hge_ctx):
check_query_f(hge_ctx, self.dir() + '/alter_function_error.yaml')

@classmethod
def dir(cls):
return 'queries/graphql_query/functions'