2021-02-22 17:59:13 +03:00
|
|
|
module Hasura.Server.Migrate.Internal
|
2021-09-24 01:56:37 +03:00
|
|
|
( getCatalogVersion,
|
|
|
|
from3To4,
|
|
|
|
setCatalogVersion,
|
|
|
|
)
|
|
|
|
where
|
2021-05-11 18:18:31 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
import Data.Aeson qualified as A
|
|
|
|
import Data.Time.Clock (UTCTime)
|
|
|
|
import Database.PG.Query qualified as Q
|
|
|
|
import Hasura.Backends.Postgres.Connection
|
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.Types.Backend (Backend)
|
|
|
|
import Hasura.RQL.Types.Common (InputWebhook)
|
|
|
|
import Hasura.RQL.Types.EventTrigger
|
|
|
|
import Hasura.SQL.Backend
|
2021-05-11 18:18:31 +03:00
|
|
|
|
2021-02-22 17:59:13 +03:00
|
|
|
-- | The old 0.8 catalog version is non-integral, so we store it in the database as a
|
|
|
|
-- string.
|
2021-04-21 13:55:18 +03:00
|
|
|
getCatalogVersion :: Q.TxE QErr Text
|
2021-09-24 01:56:37 +03:00
|
|
|
getCatalogVersion =
|
|
|
|
runIdentity . Q.getRow
|
|
|
|
<$> Q.withQE
|
|
|
|
defaultTxErrorHandler
|
|
|
|
[Q.sql| SELECT version FROM hdb_catalog.hdb_version |]
|
|
|
|
()
|
|
|
|
False
|
2021-02-22 17:59:13 +03:00
|
|
|
|
2021-09-06 14:15:36 +03:00
|
|
|
from3To4 :: forall m. (Backend ('Postgres 'Vanilla), MonadTx m) => m ()
|
2021-09-24 01:56:37 +03:00
|
|
|
from3To4 = liftTx $
|
|
|
|
Q.catchE defaultTxErrorHandler $ do
|
|
|
|
Q.unitQ
|
|
|
|
[Q.sql|
|
2021-02-22 17:59:13 +03:00
|
|
|
ALTER TABLE hdb_catalog.event_triggers
|
2021-09-24 01:56:37 +03:00
|
|
|
ADD COLUMN configuration JSON |]
|
|
|
|
()
|
|
|
|
False
|
|
|
|
eventTriggers <-
|
|
|
|
map uncurryEventTrigger
|
|
|
|
<$> Q.listQ
|
|
|
|
[Q.sql|
|
2021-02-22 17:59:13 +03:00
|
|
|
SELECT e.name, e.definition::json, e.webhook, e.num_retries, e.retry_interval, e.headers::json
|
2021-09-24 01:56:37 +03:00
|
|
|
FROM hdb_catalog.event_triggers e |]
|
|
|
|
()
|
|
|
|
False
|
2021-02-22 17:59:13 +03:00
|
|
|
forM_ eventTriggers updateEventTrigger3To4
|
2021-09-24 01:56:37 +03:00
|
|
|
Q.unitQ
|
|
|
|
[Q.sql|
|
2021-02-22 17:59:13 +03:00
|
|
|
ALTER TABLE hdb_catalog.event_triggers
|
|
|
|
DROP COLUMN definition,
|
|
|
|
DROP COLUMN query,
|
|
|
|
DROP COLUMN webhook,
|
|
|
|
DROP COLUMN num_retries,
|
|
|
|
DROP COLUMN retry_interval,
|
2021-09-16 14:03:01 +03:00
|
|
|
DROP COLUMN headers,
|
2021-09-24 01:56:37 +03:00
|
|
|
DROP COLUMN metadataTransform|]
|
|
|
|
()
|
|
|
|
False
|
|
|
|
where
|
|
|
|
uncurryEventTrigger ::
|
|
|
|
( TriggerName,
|
|
|
|
Q.AltJ (TriggerOpsDef ('Postgres 'Vanilla)),
|
|
|
|
InputWebhook,
|
|
|
|
Int,
|
|
|
|
Int,
|
|
|
|
Q.AltJ (Maybe [HeaderConf])
|
|
|
|
) ->
|
|
|
|
EventTriggerConf ('Postgres 'Vanilla)
|
|
|
|
uncurryEventTrigger (trn, Q.AltJ tDef, w, nr, rint, Q.AltJ headers) =
|
|
|
|
EventTriggerConf trn tDef (Just w) Nothing (RetryConf nr rint Nothing) headers Nothing
|
|
|
|
updateEventTrigger3To4 etc@(EventTriggerConf name _ _ _ _ _ _) =
|
|
|
|
Q.unitQ
|
|
|
|
[Q.sql|
|
2021-02-22 17:59:13 +03:00
|
|
|
UPDATE hdb_catalog.event_triggers
|
|
|
|
SET
|
|
|
|
configuration = $1
|
|
|
|
WHERE name = $2
|
2021-09-24 01:56:37 +03:00
|
|
|
|]
|
|
|
|
(Q.AltJ $ A.toJSON etc, name)
|
|
|
|
True
|
2021-05-31 16:54:08 +03:00
|
|
|
|
|
|
|
setCatalogVersion :: MonadTx m => Text -> UTCTime -> m ()
|
2021-09-24 01:56:37 +03:00
|
|
|
setCatalogVersion ver time =
|
|
|
|
liftTx $
|
|
|
|
Q.unitQE
|
|
|
|
defaultTxErrorHandler
|
|
|
|
[Q.sql|
|
2021-05-31 16:54:08 +03:00
|
|
|
INSERT INTO hdb_catalog.hdb_version (version, upgraded_on) VALUES ($1, $2)
|
|
|
|
ON CONFLICT ((version IS NOT NULL))
|
|
|
|
DO UPDATE SET version = $1, upgraded_on = $2
|
2021-09-24 01:56:37 +03:00
|
|
|
|]
|
|
|
|
(ver, time)
|
|
|
|
False
|