graphql-engine/server/src-lib/Hasura/Server/Migrate/Internal.hs
Auke Booij 4c8ea8e865 Import pg-client-hs as PG
Result of executing the following commands:
```shell
# replace "as Q" imports with "as PG" (in retrospect this didn't need a regex)
git grep -lE 'as Q($|[^a-zA-Z])' -- '*.hs' | xargs sed -i -E 's/as Q($|[^a-zA-Z])/as PG\1/'
# replace " Q." with " PG."
git grep -lE ' Q\.' -- '*.hs' | xargs sed -i 's/ Q\./ PG./g'
# replace "(Q." with "(PG."
git grep -lE '\(Q\.' -- '*.hs' | xargs sed -i 's/(Q\./(PG./g'
# ditto, but for [, |, { and !
git grep -lE '\[Q\.' -- '*.hs' | xargs sed -i 's/\[Q\./\[PG./g'
git grep -l '|Q\.' -- '*.hs' | xargs sed -i 's/|Q\./|PG./g'
git grep -l '{Q\.' -- '*.hs' | xargs sed -i 's/{Q\./{PG./g'
git grep -l '!Q\.' -- '*.hs' | xargs sed -i 's/!Q\./!PG./g'
```
(Doing the `grep -l` before the `sed`, instead of `sed` on the entire codebase, reduces the number of `mtime` updates, and so reduces how many times a file gets recompiled while checking intermediate results.)

Finally, I manually removed a broken and unused `Arbitrary` instance in `Hasura.RQL.Network`. (It used an `import Test.QuickCheck.Arbitrary as Q` statement, which was erroneously caught by the first find-replace command.)

After this PR, `Q` is no longer used as an import qualifier. That was not the goal of this PR, but perhaps it's a useful fact for future efforts.

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5933
GitOrigin-RevId: 8c84c59d57789111d40f5d3322c5a885dcfbf40e
2022-09-20 19:55:51 +00:00

102 lines
3.1 KiB
Haskell

{-# LANGUAGE QuasiQuotes #-}
module Hasura.Server.Migrate.Internal
( getCatalogVersion,
from3To4,
setCatalogVersion,
)
where
import Data.Aeson qualified as A
import Data.Text qualified as T
import Data.Time.Clock (UTCTime)
import Database.PG.Query qualified as PG
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
import Hasura.Server.Migrate.Version
-- | The old 0.8 catalog version is non-integral, so the version has always been
-- stored as a string.
getCatalogVersion :: PG.TxE QErr MetadataCatalogVersion
getCatalogVersion = do
versionText <-
runIdentity . PG.getRow
<$> PG.withQE
defaultTxErrorHandler
[PG.sql| SELECT version FROM hdb_catalog.hdb_version |]
()
False
onLeft (readEither $ T.unpack versionText) $
\err -> throw500 $ "Unexpected: couldn't convert read catalog version " <> versionText <> ", err:" <> tshow err
from3To4 :: forall m. (Backend ('Postgres 'Vanilla), MonadTx m) => m ()
from3To4 = liftTx $
PG.catchE defaultTxErrorHandler $ do
PG.unitQ
[PG.sql|
ALTER TABLE hdb_catalog.event_triggers
ADD COLUMN configuration JSON |]
()
False
eventTriggers <-
map uncurryEventTrigger
<$> PG.listQ
[PG.sql|
SELECT e.name, e.definition::json, e.webhook, e.num_retries, e.retry_interval, e.headers::json
FROM hdb_catalog.event_triggers e |]
()
False
forM_ eventTriggers updateEventTrigger3To4
PG.unitQ
[PG.sql|
ALTER TABLE hdb_catalog.event_triggers
DROP COLUMN definition,
DROP COLUMN query,
DROP COLUMN webhook,
DROP COLUMN num_retries,
DROP COLUMN retry_interval,
DROP COLUMN headers,
DROP COLUMN metadataTransform|]
()
False
where
uncurryEventTrigger ::
( TriggerName,
PG.AltJ (TriggerOpsDef ('Postgres 'Vanilla)),
InputWebhook,
Int,
Int,
PG.AltJ (Maybe [HeaderConf])
) ->
EventTriggerConf ('Postgres 'Vanilla)
uncurryEventTrigger (trn, PG.AltJ tDef, w, nr, rint, PG.AltJ headers) =
EventTriggerConf trn tDef (Just w) Nothing (RetryConf nr rint Nothing) headers Nothing Nothing Nothing
updateEventTrigger3To4 etc@(EventTriggerConf name _ _ _ _ _ _ _ _) =
PG.unitQ
[PG.sql|
UPDATE hdb_catalog.event_triggers
SET
configuration = $1
WHERE name = $2
|]
(PG.AltJ $ A.toJSON etc, name)
True
setCatalogVersion :: MonadTx m => Text -> UTCTime -> m ()
setCatalogVersion ver time =
liftTx $
PG.unitQE
defaultTxErrorHandler
[PG.sql|
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
|]
(ver, time)
False