quote function and trigger names, allow hyphen in trigger name (#1012)

This commit is contained in:
Tirumarai Selvan 2018-11-13 11:28:55 +05:30 committed by Vamshi Surabhi
parent 8c1700e76f
commit d4d31838cb
5 changed files with 23 additions and 15 deletions

View File

@ -130,7 +130,7 @@ library
, auto-update
-- regex related
, regex-compat
, regex-tdfa >= 1.2
exposed-modules: Hasura.Server.App
, Hasura.Server.Auth

View File

@ -18,14 +18,15 @@ module Hasura.GraphQL.Utils
, isValidName
) where
import Hasura.RQL.Types
import Hasura.Prelude
import Hasura.RQL.Types
import qualified Data.ByteString.Lazy as LBS
import qualified Data.HashMap.Strict as Map
import qualified Data.List.NonEmpty as NE
import qualified Data.Text as T
import qualified Language.GraphQL.Draft.Syntax as G
import qualified Text.Regex as R
import qualified Text.Regex.TDFA as TDFA
showName :: G.Name -> Text
showName name = "\"" <> G.unName name <> "\""
@ -93,6 +94,6 @@ showNames names =
-- Ref: http://facebook.github.io/graphql/June2018/#sec-Names
isValidName :: G.Name -> Bool
isValidName =
isJust . R.matchRegex regex . T.unpack . G.unName
TDFA.match compiledRegex . T.unpack . G.unName
where
regex = R.mkRegex "^[_a-zA-Z][_a-zA-Z0-9]*$"
compiledRegex = TDFA.makeRegex ("^[_a-zA-Z][_a-zA-Z0-9]*$" ::LBS.ByteString) :: TDFA.Regex

View File

@ -37,9 +37,14 @@ triggerTmplt = case parseGingerTmplt $(FE.embedStringFile "src-rsr/trigger.sql.j
Left _ -> Nothing
Right tmplt -> Just tmplt
pgIdenTrigger:: Ops -> TriggerName -> T.Text
pgIdenTrigger op trn = pgFmtIden (qualifyTriggerName op trn)
where
qualifyTriggerName op' trn' = "notify_hasura_" <> trn' <> "_" <> T.pack (show op')
getDropFuncSql :: Ops -> TriggerName -> T.Text
getDropFuncSql op trn = "DROP FUNCTION IF EXISTS"
<> " hdb_views.notify_hasura_" <> trn <> "_" <> T.pack (show op) <> "()"
<> " hdb_views." <> pgIdenTrigger op trn <> "()"
<> " CASCADE"
getTriggerSql
@ -54,6 +59,7 @@ getTriggerSql op trid trn qt allCols spec =
let globalCtx = HashMap.fromList
[ (T.pack "ID", trid)
, (T.pack "NAME", trn)
, (T.pack "QUALIFIED_TRIGGER_NAME", pgIdenTrigger op trn)
, (T.pack "QUALIFIED_TABLE", toSQLTxt qt)
]
opCtx = maybe HashMap.empty (createOpCtx op) spec

View File

@ -28,9 +28,10 @@ import Data.Aeson.TH
import Hasura.Prelude
import Hasura.SQL.Types
import Language.Haskell.TH.Syntax (Lift)
import Text.Regex (matchRegex, mkRegex)
import qualified Data.ByteString.Lazy as LBS
import qualified Data.Text as T
import qualified Text.Regex.TDFA as TDFA
type TriggerName = T.Text
type TriggerId = T.Text
@ -122,11 +123,11 @@ instance FromJSON CreateEventTriggerQuery where
webhook <- o .: "webhook"
headers <- o .:? "headers"
replace <- o .:? "replace" .!= False
let regex = mkRegex "^\\w+$"
mName = matchRegex regex (T.unpack name)
case mName of
Just _ -> return ()
Nothing -> fail "only alphanumeric and underscore allowed for name"
let regex = "^[A-Za-z]+[A-Za-z0-9_\\-]*$" :: LBS.ByteString
compiledRegex = TDFA.makeRegex regex :: TDFA.Regex
isMatch = TDFA.match compiledRegex (T.unpack name)
if isMatch then return ()
else fail "only alphanumeric and underscore allowed for name"
case insert <|> update <|> delete of
Just _ -> return ()
Nothing -> fail "must provide operation spec(s)"

View File

@ -1,4 +1,4 @@
CREATE OR REPLACE function hdb_views.notify_hasura_{{NAME}}_{{OPERATION}}() RETURNS trigger
CREATE OR REPLACE function hdb_views.{{QUALIFIED_TRIGGER_NAME}}() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
@ -34,5 +34,5 @@ CREATE OR REPLACE function hdb_views.notify_hasura_{{NAME}}_{{OPERATION}}() RETU
RETURN NULL;
END;
$$;
DROP TRIGGER IF EXISTS notify_hasura_{{NAME}}_{{OPERATION}} ON {{QUALIFIED_TABLE}};
CREATE TRIGGER notify_hasura_{{NAME}}_{{OPERATION}} AFTER {{OPERATION}} ON {{QUALIFIED_TABLE}} FOR EACH ROW EXECUTE PROCEDURE hdb_views.notify_hasura_{{NAME}}_{{OPERATION}}();
DROP TRIGGER IF EXISTS {{QUALIFIED_TRIGGER_NAME}} ON {{QUALIFIED_TABLE}};
CREATE TRIGGER {{QUALIFIED_TRIGGER_NAME}} AFTER {{OPERATION}} ON {{QUALIFIED_TABLE}} FOR EACH ROW EXECUTE PROCEDURE hdb_views.{{QUALIFIED_TRIGGER_NAME}}();