Skip to content

Commit

Permalink
Avoid hdb_views schema for storing event trigger procedures (hasura…
Browse files Browse the repository at this point in the history
  • Loading branch information
rakeshkky authored Nov 5, 2020
1 parent 81fd1ec commit 8bf84df
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 54 deletions.
2 changes: 1 addition & 1 deletion scripts/edit-pg-dump/edit-pg-dump.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ done <<< $lines

# delete notify triggers

sed -i -E '/^CREATE TRIGGER "?notify_hasura_.+"? AFTER \w+ ON .+ FOR EACH ROW EXECUTE PROCEDURE "?hdb_views"?\."?notify_hasura_.+"?\(\);$/d' $filename
sed -i -E '/^CREATE TRIGGER "?notify_hasura_.+"? AFTER \w+ ON .+ FOR EACH ROW EXECUTE PROCEDURE "?hdb_catalog"?\."?notify_hasura_.+"?\(\);$/d' $filename

# delete empty lines

Expand Down
1 change: 0 additions & 1 deletion server/graphql-engine.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,6 @@ library
, Hasura.RQL.DDL.Schema.Function
, Hasura.RQL.DDL.Schema.Rename
, Hasura.RQL.DDL.Schema.Table
, Hasura.RQL.DDL.Utils
, Hasura.RQL.DDL.EventTrigger
, Hasura.RQL.DDL.ScheduledTrigger
, Hasura.RQL.DML.Count
Expand Down
20 changes: 11 additions & 9 deletions server/src-lib/Hasura/RQL/DDL/EventTrigger.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,12 @@ import Hasura.SQL.Types
data OpVar = OLD | NEW deriving (Show)

-- pgIdenTrigger is a method used to construct the name of the pg function
-- used for event triggers which are present in the hdb_views schema.
-- used for event triggers which are present in the hdb_catalog schema.
pgIdenTrigger:: Ops -> TriggerName -> Text
pgIdenTrigger op trn = pgFmtIdentifier . qualifyTriggerName op $ triggerNameToTxt trn
where
qualifyTriggerName op' trn' = "notify_hasura_" <> trn' <> "_" <> T.pack (show op')

getDropFuncSql :: Ops -> TriggerName -> Text
getDropFuncSql op trn = "DROP FUNCTION IF EXISTS"
<> " hdb_views." <> pgIdenTrigger op trn <> "()"
<> " CASCADE"

mkAllTriggersQ
:: (MonadTx m, HasSQLGenCtx m)
=> TriggerName
Expand Down Expand Up @@ -121,9 +116,16 @@ mkTriggerQ trn qt allCols op (SubscribeOpSpec columns payload) = do
opToTxt = T.pack . show

delTriggerQ :: TriggerName -> Q.TxE QErr ()
delTriggerQ trn = mapM_ (\op -> Q.unitQE
defaultTxErrorHandler
(Q.fromText $ getDropFuncSql op trn) () False) [INSERT, UPDATE, DELETE]
delTriggerQ trn =
mapM_ (\op -> Q.unitQE
defaultTxErrorHandler
(Q.fromText $ getDropFuncSql op) () False) [INSERT, UPDATE, DELETE]
where
getDropFuncSql :: Ops -> T.Text
getDropFuncSql op =
"DROP FUNCTION IF EXISTS"
<> " hdb_catalog." <> pgIdenTrigger op trn <> "()"
<> " CASCADE"

addEventTriggerToCatalog
:: QualifiedTable
Expand Down
10 changes: 6 additions & 4 deletions server/src-lib/Hasura/RQL/DDL/Schema/Cache.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import Hasura.RQL.DDL.Schema.Catalog
import Hasura.RQL.DDL.Schema.Diff
import Hasura.RQL.DDL.Schema.Function
import Hasura.RQL.DDL.Schema.Table
import Hasura.RQL.DDL.Utils (clearHdbViews)
import Hasura.RQL.Types hiding (fmFunction, tmTable)
import Hasura.RQL.Types.Catalog
import Hasura.Server.Version (HasVersion)
Expand Down Expand Up @@ -409,8 +408,11 @@ buildSchemaCacheRule env = proc (catalogMetadata, invalidationKeys) -> do
-- if not, incorporates them into the schema cache.
withMetadataCheck :: (MonadTx m, CacheRWM m, HasSQLGenCtx m) => Bool -> m a -> m a
withMetadataCheck cascade action = do
-- Drop hdb_views so no interference is caused to the sql query
liftTx $ Q.catchE defaultTxErrorHandler clearHdbViews
-- Drop event triggers so no interference is caused to the sql query
preActionTables <- scTables <$> askSchemaCache
forM_ (M.elems preActionTables) $ \tableInfo -> do
let eventTriggers = _tiEventTriggerInfoMap tableInfo
forM_ (M.keys eventTriggers) (liftTx . delTriggerQ)

-- Get the metadata before the sql query, everything, need to filter this
oldMetaU <- liftTx $ Q.catchE defaultTxErrorHandler fetchTableMeta
Expand Down Expand Up @@ -465,7 +467,7 @@ withMetadataCheck cascade action = do
buildSchemaCache
postSc <- askSchemaCache

-- Recreate event triggers in hdb_views
-- Recreate event triggers in hdb_catalog
forM_ (M.elems $ scTables postSc) $ \(TableInfo coreInfo _ eventTriggers) -> do
let table = _tciName coreInfo
columns = getCols $ _tciFieldInfoMap coreInfo
Expand Down
20 changes: 0 additions & 20 deletions server/src-lib/Hasura/RQL/DDL/Utils.hs

This file was deleted.

6 changes: 3 additions & 3 deletions server/src-lib/Hasura/RQL/Types/SchemaCache/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ class (CacheRM m) => CacheRWM m where
data BuildReason
-- | The build was triggered by an update this instance made to the catalog (in the
-- currently-active transaction), so information in Postgres that needs to be kept in sync with
-- the catalog (i.e. anything in the @hdb_views@ schema) should be updated.
-- the catalog (i.e. table event triggers in @hdb_catalog@ schema) should be updated.
= CatalogUpdate
-- | The build was triggered by a notification that some other currently-running Hasura instance
-- updated the catalog. Since that instance already updated @hdb_views@, this build should be
-- read-only.
-- updated the catalog. Since that instance already updated table event triggers in @hdb_catalog@,
-- this build should be read-only.
| CatalogSync
deriving (Show, Eq)

Expand Down
12 changes: 6 additions & 6 deletions server/src-lib/Hasura/Server/API/PGDump.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ module Hasura.Server.API.PGDump
import Control.Exception (IOException, try)
import Data.Aeson.Casing
import Data.Aeson.TH
import qualified Data.ByteString.Lazy as BL
import Data.Char (isSpace)
import qualified Data.List as L
import qualified Data.Text as T
import qualified Data.ByteString.Lazy as BL
import Data.Char (isSpace)
import qualified Data.List as L
import qualified Data.Text as T
import Data.Text.Conversions
import qualified Database.PG.Query as Q
import qualified Database.PG.Query as Q
import Hasura.Prelude
import qualified Hasura.RQL.Types.Error as RTE
import System.Exit
Expand Down Expand Up @@ -91,5 +91,5 @@ execPGDump b ci = do
-- These changes are also documented on the method pgIdenTrigger
"^CREATE TRIGGER \"?notify_hasura_.+\"? AFTER [[:alnum:]]+ "
<> "ON .+ FOR EACH ROW EXECUTE (FUNCTION|PROCEDURE) "
<> "\"?hdb_views\"?\\.\"?notify_hasura_.+\"?\\(\\);$"
<> "\"?hdb_catalog\"?\\.\"?notify_hasura_.+\"?\\(\\);$"
in TDFA.makeRegex regexStr :: TDFA.Regex
11 changes: 3 additions & 8 deletions server/src-lib/Hasura/Server/Migrate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import qualified Database.PG.Query.Connection as Q
import qualified Language.Haskell.TH.Lib as TH
import qualified Language.Haskell.TH.Syntax as TH

import Control.Lens (_2, view)
import Control.Lens (view, _2)
import Control.Monad.Unique
import Data.Text.NonEmpty
import Data.Time.Clock (UTCTime)
Expand All @@ -53,9 +53,7 @@ import Hasura.Server.Migrate.Version (latestCatalogVersion,
import Hasura.Server.Version

dropCatalog :: (MonadTx m) => m ()
dropCatalog = liftTx $ Q.catchE defaultTxErrorHandler $ do
-- This is where the generated views and triggers are stored
Q.unitQ "DROP SCHEMA IF EXISTS hdb_views CASCADE" () False
dropCatalog = liftTx $ Q.catchE defaultTxErrorHandler $
Q.unitQ "DROP SCHEMA IF EXISTS hdb_catalog CASCADE" () False

data MigrationResult
Expand Down Expand Up @@ -110,10 +108,7 @@ migrateCatalog env migrationTime = do
initialize :: Bool -> m (MigrationResult, RebuildableSchemaCache m)
initialize createSchema = do
liftTx $ Q.catchE defaultTxErrorHandler $
when createSchema $ do
Q.unitQ "CREATE SCHEMA hdb_catalog" () False
-- This is where the generated views and triggers are stored
Q.unitQ "CREATE SCHEMA hdb_views" () False
when createSchema $ Q.unitQ "CREATE SCHEMA hdb_catalog" () False

isExtensionAvailable "pgcrypto" >>= \case
-- only if we created the schema, create the extension
Expand Down
4 changes: 2 additions & 2 deletions server/src-rsr/trigger.sql.shakespeare
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE OR REPLACE function hdb_views.#{qualifiedTriggerName}() RETURNS trigger
CREATE OR REPLACE function hdb_catalog.#{qualifiedTriggerName}() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
Expand Down Expand Up @@ -32,4 +32,4 @@ CREATE OR REPLACE function hdb_views.#{qualifiedTriggerName}() RETURNS trigger
END;
$$;
DROP TRIGGER IF EXISTS #{qualifiedTriggerName} ON #{qualifiedTable};
CREATE TRIGGER #{qualifiedTriggerName} AFTER #{operation} ON #{qualifiedTable} FOR EACH ROW EXECUTE PROCEDURE hdb_views.#{qualifiedTriggerName}();
CREATE TRIGGER #{qualifiedTriggerName} AFTER #{operation} ON #{qualifiedTable} FOR EACH ROW EXECUTE PROCEDURE hdb_catalog.#{qualifiedTriggerName}();

0 comments on commit 8bf84df

Please sign in to comment.