2019-10-21 19:01:05 +03:00
|
|
|
|
-- | Migrations for the Hasura catalog.
|
|
|
|
|
--
|
|
|
|
|
-- To add a new migration:
|
|
|
|
|
--
|
2020-02-24 17:33:56 +03:00
|
|
|
|
-- 1. Bump the catalog version number in @src-rsr/catalog_version.txt@.
|
2019-10-21 19:01:05 +03:00
|
|
|
|
-- 2. Add a migration script in the @src-rsr/migrations/@ directory with the name
|
|
|
|
|
-- @<old version>_to_<new version>.sql@.
|
2020-02-07 14:03:12 +03:00
|
|
|
|
-- 3. Create a downgrade script in the @src-rsr/migrations/@ directory with the name
|
|
|
|
|
-- @<new version>_to_<old version>.sql@.
|
|
|
|
|
-- 4. If making a new release, add the mapping from application version to catalog
|
2020-03-26 23:42:33 +03:00
|
|
|
|
-- schema version in @src-rsr/catalog_versions.txt@.
|
2020-03-27 07:50:45 +03:00
|
|
|
|
-- 5. If appropriate, add the change to @server/src-rsr/initialise.sql@ for fresh installations
|
|
|
|
|
-- of hasura.
|
2019-10-21 19:01:05 +03:00
|
|
|
|
--
|
|
|
|
|
-- The Template Haskell code in this module will automatically compile the new migration script into
|
|
|
|
|
-- the @graphql-engine@ executable.
|
|
|
|
|
module Hasura.Server.Migrate
|
2021-09-24 01:56:37 +03:00
|
|
|
|
( MigrationResult (..),
|
|
|
|
|
getMigratedFrom,
|
|
|
|
|
migrateCatalog,
|
|
|
|
|
latestCatalogVersion,
|
|
|
|
|
downgradeCatalog,
|
|
|
|
|
)
|
|
|
|
|
where
|
2019-10-21 19:01:05 +03:00
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
|
import Control.Monad.Trans.Control (MonadBaseControl)
|
|
|
|
|
import Data.Aeson qualified as A
|
|
|
|
|
import Data.FileEmbed (makeRelativeToProject)
|
|
|
|
|
import Data.HashMap.Strict.InsOrd qualified as OMap
|
|
|
|
|
import Data.Text qualified as T
|
|
|
|
|
import Data.Text.IO qualified as TIO
|
|
|
|
|
import Data.Time.Clock (UTCTime)
|
|
|
|
|
import Database.PG.Query qualified as Q
|
|
|
|
|
import Hasura.Backends.Postgres.SQL.Types
|
|
|
|
|
import Hasura.Base.Error
|
|
|
|
|
import Hasura.Logging (Hasura, LogLevel (..), ToEngineLog (..))
|
|
|
|
|
import Hasura.Prelude
|
|
|
|
|
import Hasura.RQL.DDL.Schema
|
|
|
|
|
import Hasura.RQL.DDL.Schema.LegacyCatalog
|
|
|
|
|
import Hasura.RQL.Types
|
|
|
|
|
import Hasura.SQL.AnyBackend qualified as AB
|
|
|
|
|
import Hasura.Server.Init (DowngradeOptions (..), databaseUrlEnv)
|
|
|
|
|
import Hasura.Server.Logging (StartupLog (..))
|
|
|
|
|
import Hasura.Server.Migrate.Internal
|
|
|
|
|
import Hasura.Server.Migrate.Version
|
|
|
|
|
( latestCatalogVersion,
|
|
|
|
|
latestCatalogVersionString,
|
|
|
|
|
)
|
|
|
|
|
import Hasura.Server.Types (MaintenanceMode (..))
|
|
|
|
|
import Language.Haskell.TH.Lib qualified as TH
|
|
|
|
|
import Language.Haskell.TH.Syntax qualified as TH
|
|
|
|
|
import System.Directory (doesFileExist)
|
2019-10-21 19:01:05 +03:00
|
|
|
|
|
|
|
|
|
data MigrationResult
|
|
|
|
|
= MRNothingToDo
|
|
|
|
|
| MRInitialized
|
2021-09-24 01:56:37 +03:00
|
|
|
|
| -- | old catalog version
|
|
|
|
|
MRMigrated Text
|
2021-02-18 19:46:14 +03:00
|
|
|
|
| MRMaintanenceMode
|
2019-10-21 19:01:05 +03:00
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
|
instance ToEngineLog MigrationResult Hasura where
|
2021-09-24 01:56:37 +03:00
|
|
|
|
toEngineLog result =
|
|
|
|
|
toEngineLog $
|
|
|
|
|
StartupLog
|
|
|
|
|
{ slLogLevel = LevelInfo,
|
|
|
|
|
slKind = "catalog_migrate",
|
|
|
|
|
slInfo = A.toJSON $ case result of
|
|
|
|
|
MRNothingToDo ->
|
|
|
|
|
"Already at the latest catalog version (" <> latestCatalogVersionString
|
|
|
|
|
<> "); nothing to do."
|
|
|
|
|
MRInitialized ->
|
|
|
|
|
"Successfully initialized the catalog (at version " <> latestCatalogVersionString <> ")."
|
|
|
|
|
MRMigrated oldVersion ->
|
|
|
|
|
"Successfully migrated from catalog version " <> oldVersion <> " to version "
|
|
|
|
|
<> latestCatalogVersionString
|
|
|
|
|
<> "."
|
|
|
|
|
MRMaintanenceMode ->
|
|
|
|
|
"Catalog migrations are skipped because the graphql-engine is in maintenance mode"
|
|
|
|
|
}
|
2019-10-21 19:01:05 +03:00
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
|
getMigratedFrom ::
|
|
|
|
|
MigrationResult ->
|
|
|
|
|
-- | We have version 0.8 as non integral catalog version
|
|
|
|
|
Maybe Float
|
2021-01-07 12:04:22 +03:00
|
|
|
|
getMigratedFrom = \case
|
2021-09-24 01:56:37 +03:00
|
|
|
|
MRNothingToDo -> Nothing
|
|
|
|
|
MRInitialized -> Nothing
|
|
|
|
|
MRMigrated t -> readMaybe (T.unpack t)
|
2021-02-18 19:46:14 +03:00
|
|
|
|
MRMaintanenceMode -> Nothing
|
2021-01-07 12:04:22 +03:00
|
|
|
|
|
2020-02-07 14:03:12 +03:00
|
|
|
|
-- A migration and (hopefully) also its inverse if we have it.
|
2020-05-13 15:33:16 +03:00
|
|
|
|
-- Polymorphic because `m` can be any `MonadTx`, `MonadIO` when
|
2020-02-07 14:03:12 +03:00
|
|
|
|
-- used in the `migrations` function below.
|
|
|
|
|
data MigrationPair m = MigrationPair
|
2021-09-24 01:56:37 +03:00
|
|
|
|
{ mpMigrate :: m (),
|
|
|
|
|
mpDown :: Maybe (m ())
|
2020-02-07 14:03:12 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
|
migrateCatalog ::
|
|
|
|
|
forall m.
|
|
|
|
|
( MonadTx m,
|
|
|
|
|
MonadIO m,
|
|
|
|
|
MonadBaseControl IO m
|
|
|
|
|
) =>
|
|
|
|
|
Maybe (SourceConnConfiguration ('Postgres 'Vanilla)) ->
|
|
|
|
|
MaintenanceMode ->
|
|
|
|
|
UTCTime ->
|
|
|
|
|
m (MigrationResult, Metadata)
|
2021-02-18 19:46:14 +03:00
|
|
|
|
migrateCatalog maybeDefaultSourceConfig maintenanceMode migrationTime = do
|
|
|
|
|
catalogSchemaExists <- doesSchemaExist (SchemaName "hdb_catalog")
|
|
|
|
|
versionTableExists <- doesTableExist (SchemaName "hdb_catalog") (TableName "hdb_version")
|
2021-04-21 13:55:18 +03:00
|
|
|
|
metadataTableExists <- doesTableExist (SchemaName "hdb_catalog") (TableName "hdb_metadata")
|
2021-02-18 19:46:14 +03:00
|
|
|
|
migrationResult <-
|
2021-09-24 01:56:37 +03:00
|
|
|
|
if
|
|
|
|
|
| maintenanceMode == MaintenanceModeEnabled -> do
|
|
|
|
|
if
|
|
|
|
|
| not catalogSchemaExists ->
|
|
|
|
|
throw500 "unexpected: hdb_catalog schema not found in maintenance mode"
|
2021-02-18 19:46:14 +03:00
|
|
|
|
| not versionTableExists ->
|
2021-09-24 01:56:37 +03:00
|
|
|
|
throw500 "unexpected: hdb_catalog.hdb_version table not found in maintenance mode"
|
2021-04-21 13:55:18 +03:00
|
|
|
|
| not metadataTableExists ->
|
2021-09-24 01:56:37 +03:00
|
|
|
|
throw500 $
|
|
|
|
|
"the \"hdb_catalog.hdb_metadata\" table is expected to exist and contain"
|
|
|
|
|
<> " the metadata of the graphql-engine"
|
2021-02-18 19:46:14 +03:00
|
|
|
|
| otherwise -> pure MRMaintanenceMode
|
2021-09-24 01:56:37 +03:00
|
|
|
|
| otherwise -> case catalogSchemaExists of
|
|
|
|
|
False -> initialize True
|
|
|
|
|
True -> case versionTableExists of
|
|
|
|
|
False -> initialize False
|
|
|
|
|
True -> migrateFrom =<< liftTx getCatalogVersion
|
2021-02-19 11:46:12 +03:00
|
|
|
|
metadata <- liftTx fetchMetadataFromCatalog
|
2020-12-28 15:56:00 +03:00
|
|
|
|
pure (migrationResult, metadata)
|
2019-10-21 19:01:05 +03:00
|
|
|
|
where
|
|
|
|
|
-- initializes the catalog, creating the schema if necessary
|
2020-12-14 07:30:19 +03:00
|
|
|
|
initialize :: Bool -> m MigrationResult
|
2021-09-24 01:56:37 +03:00
|
|
|
|
initialize createSchema = do
|
|
|
|
|
liftTx $
|
|
|
|
|
Q.catchE defaultTxErrorHandler $
|
|
|
|
|
when createSchema $ Q.unitQ "CREATE SCHEMA hdb_catalog" () False
|
2020-12-28 15:56:00 +03:00
|
|
|
|
enablePgcryptoExtension
|
2021-09-15 23:45:49 +03:00
|
|
|
|
multiQ $(makeRelativeToProject "src-rsr/initialise.sql" >>= Q.sqlFromFile)
|
2019-10-21 19:01:05 +03:00
|
|
|
|
updateCatalogVersion
|
2021-01-07 12:04:22 +03:00
|
|
|
|
|
|
|
|
|
let emptyMetadata' = case maybeDefaultSourceConfig of
|
|
|
|
|
Nothing -> emptyMetadata
|
|
|
|
|
Just defaultSourceConfig ->
|
|
|
|
|
-- insert metadata with default source
|
2021-09-24 01:56:37 +03:00
|
|
|
|
let defaultSourceMetadata =
|
|
|
|
|
AB.mkAnyBackend $
|
|
|
|
|
SourceMetadata @('Postgres 'Vanilla) defaultSource mempty mempty defaultSourceConfig Nothing
|
2021-01-07 12:04:22 +03:00
|
|
|
|
sources = OMap.singleton defaultSource defaultSourceMetadata
|
2021-09-24 01:56:37 +03:00
|
|
|
|
in emptyMetadata {_metaSources = sources}
|
2021-01-07 12:04:22 +03:00
|
|
|
|
|
2021-02-19 11:46:12 +03:00
|
|
|
|
liftTx $ insertMetadataInCatalog emptyMetadata'
|
2020-12-14 07:30:19 +03:00
|
|
|
|
pure MRInitialized
|
2019-10-21 19:01:05 +03:00
|
|
|
|
|
|
|
|
|
-- migrates an existing catalog to the latest version from an existing verion
|
2020-12-14 07:30:19 +03:00
|
|
|
|
migrateFrom :: Text -> m MigrationResult
|
2019-10-21 19:01:05 +03:00
|
|
|
|
migrateFrom previousVersion
|
2020-12-14 07:30:19 +03:00
|
|
|
|
| previousVersion == latestCatalogVersionString = pure MRNothingToDo
|
2020-02-07 14:03:12 +03:00
|
|
|
|
| [] <- neededMigrations =
|
2021-09-24 01:56:37 +03:00
|
|
|
|
throw400 NotSupported $
|
|
|
|
|
"Cannot use database previously used with a newer version of graphql-engine (expected"
|
|
|
|
|
<> " a catalog version <="
|
|
|
|
|
<> latestCatalogVersionString
|
|
|
|
|
<> ", but the current version"
|
|
|
|
|
<> " is "
|
|
|
|
|
<> previousVersion
|
|
|
|
|
<> ")."
|
2019-11-20 21:21:30 +03:00
|
|
|
|
| otherwise = do
|
2021-09-24 01:56:37 +03:00
|
|
|
|
traverse_ (mpMigrate . snd) neededMigrations
|
|
|
|
|
updateCatalogVersion
|
|
|
|
|
pure $ MRMigrated previousVersion
|
2019-10-21 19:01:05 +03:00
|
|
|
|
where
|
2020-12-08 17:22:31 +03:00
|
|
|
|
neededMigrations =
|
2021-02-18 19:46:14 +03:00
|
|
|
|
dropWhile ((/= previousVersion) . fst) (migrations maybeDefaultSourceConfig False maintenanceMode)
|
2020-05-13 15:33:16 +03:00
|
|
|
|
|
2020-06-19 09:42:32 +03:00
|
|
|
|
updateCatalogVersion = setCatalogVersion latestCatalogVersionString migrationTime
|
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
|
downgradeCatalog ::
|
|
|
|
|
forall m.
|
|
|
|
|
(MonadIO m, MonadTx m) =>
|
|
|
|
|
Maybe (SourceConnConfiguration ('Postgres 'Vanilla)) ->
|
|
|
|
|
DowngradeOptions ->
|
|
|
|
|
UTCTime ->
|
|
|
|
|
m MigrationResult
|
2020-12-28 15:56:00 +03:00
|
|
|
|
downgradeCatalog defaultSourceConfig opts time = do
|
2021-09-24 01:56:37 +03:00
|
|
|
|
downgradeFrom =<< liftTx getCatalogVersion
|
2020-02-07 14:03:12 +03:00
|
|
|
|
where
|
|
|
|
|
-- downgrades an existing catalog to the specified version
|
2020-10-27 16:53:49 +03:00
|
|
|
|
downgradeFrom :: Text -> m MigrationResult
|
2020-02-07 14:03:12 +03:00
|
|
|
|
downgradeFrom previousVersion
|
2021-09-24 01:56:37 +03:00
|
|
|
|
| previousVersion == dgoTargetVersion opts = do
|
|
|
|
|
pure MRNothingToDo
|
|
|
|
|
| otherwise =
|
|
|
|
|
case neededDownMigrations (dgoTargetVersion opts) of
|
|
|
|
|
Left reason ->
|
|
|
|
|
throw400 NotSupported $
|
|
|
|
|
"This downgrade path (from "
|
|
|
|
|
<> previousVersion
|
|
|
|
|
<> " to "
|
|
|
|
|
<> dgoTargetVersion opts
|
|
|
|
|
<> ") is not supported, because "
|
|
|
|
|
<> reason
|
|
|
|
|
Right path -> do
|
|
|
|
|
sequence_ path
|
|
|
|
|
unless (dgoDryRun opts) do
|
|
|
|
|
setCatalogVersion (dgoTargetVersion opts) time
|
|
|
|
|
pure (MRMigrated previousVersion)
|
2020-02-07 14:03:12 +03:00
|
|
|
|
where
|
2020-05-13 15:33:16 +03:00
|
|
|
|
neededDownMigrations newVersion =
|
2021-09-24 01:56:37 +03:00
|
|
|
|
downgrade
|
|
|
|
|
previousVersion
|
|
|
|
|
newVersion
|
2021-02-18 19:46:14 +03:00
|
|
|
|
(reverse (migrations defaultSourceConfig (dgoDryRun opts) MaintenanceModeDisabled))
|
2020-02-07 14:03:12 +03:00
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
|
downgrade ::
|
|
|
|
|
Text ->
|
|
|
|
|
Text ->
|
|
|
|
|
[(Text, MigrationPair m)] ->
|
|
|
|
|
Either Text [m ()]
|
|
|
|
|
downgrade lower upper = skipFutureDowngrades
|
|
|
|
|
where
|
|
|
|
|
-- We find the list of downgrade scripts to run by first
|
|
|
|
|
-- dropping any downgrades which correspond to newer versions
|
|
|
|
|
-- of the schema than the one we're running currently.
|
|
|
|
|
-- Then we take migrations as needed until we reach the target
|
|
|
|
|
-- version, dropping any remaining migrations from the end of the
|
|
|
|
|
-- (reversed) list.
|
|
|
|
|
skipFutureDowngrades, dropOlderDowngrades :: [(Text, MigrationPair m)] -> Either Text [m ()]
|
|
|
|
|
skipFutureDowngrades xs | previousVersion == lower = dropOlderDowngrades xs
|
|
|
|
|
skipFutureDowngrades [] = Left "the starting version is unrecognized."
|
|
|
|
|
skipFutureDowngrades ((x, _) : xs)
|
|
|
|
|
| x == lower = dropOlderDowngrades xs
|
|
|
|
|
| otherwise = skipFutureDowngrades xs
|
2020-02-07 14:03:12 +03:00
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
|
dropOlderDowngrades [] = Left "the target version is unrecognized."
|
|
|
|
|
dropOlderDowngrades ((x, MigrationPair {mpDown = Nothing}) : _) =
|
|
|
|
|
Left $ "there is no available migration back to version " <> x <> "."
|
|
|
|
|
dropOlderDowngrades ((x, MigrationPair {mpDown = Just y}) : xs)
|
|
|
|
|
| x == upper = Right [y]
|
|
|
|
|
| otherwise = (y :) <$> dropOlderDowngrades xs
|
2020-02-07 14:03:12 +03:00
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
|
migrations ::
|
|
|
|
|
forall m.
|
|
|
|
|
(MonadIO m, MonadTx m) =>
|
|
|
|
|
Maybe (SourceConnConfiguration ('Postgres 'Vanilla)) ->
|
|
|
|
|
Bool ->
|
|
|
|
|
MaintenanceMode ->
|
|
|
|
|
[(Text, MigrationPair m)]
|
2021-02-18 19:46:14 +03:00
|
|
|
|
migrations maybeDefaultSourceConfig dryRun maintenanceMode =
|
2021-09-24 01:56:37 +03:00
|
|
|
|
-- We need to build the list of migrations at compile-time so that we can compile the SQL
|
|
|
|
|
-- directly into the executable using `Q.sqlFromFile`. The GHC stage restriction makes
|
|
|
|
|
-- doing this a little bit awkward (we can’t use any definitions in this module at
|
|
|
|
|
-- compile-time), but putting a `let` inside the splice itself is allowed.
|
|
|
|
|
$( let migrationFromFile from to =
|
|
|
|
|
let path = "src-rsr/migrations/" <> from <> "_to_" <> to <> ".sql"
|
|
|
|
|
in [|runTxOrPrint $(makeRelativeToProject path >>= Q.sqlFromFile)|]
|
|
|
|
|
migrationFromFileMaybe from to = do
|
|
|
|
|
path <- makeRelativeToProject $ "src-rsr/migrations/" <> from <> "_to_" <> to <> ".sql"
|
|
|
|
|
exists <- TH.runIO (doesFileExist path)
|
|
|
|
|
if exists
|
|
|
|
|
then [|Just (runTxOrPrint $(Q.sqlFromFile path))|]
|
|
|
|
|
else [|Nothing|]
|
2020-05-13 15:33:16 +03:00
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
|
migrationsFromFile = map $ \(to :: Integer) ->
|
|
|
|
|
let from = to - 1
|
|
|
|
|
in [|
|
|
|
|
|
( $(TH.lift $ tshow from),
|
|
|
|
|
MigrationPair
|
|
|
|
|
$(migrationFromFile (show from) (show to))
|
|
|
|
|
$(migrationFromFileMaybe (show to) (show from))
|
|
|
|
|
)
|
|
|
|
|
|]
|
2020-02-07 14:03:12 +03:00
|
|
|
|
in TH.listE
|
2021-09-24 01:56:37 +03:00
|
|
|
|
-- version 0.8 is the only non-integral catalog version
|
|
|
|
|
$
|
|
|
|
|
[|("0.8", MigrationPair $(migrationFromFile "08" "1") Nothing)|] :
|
|
|
|
|
migrationsFromFile [2 .. 3]
|
|
|
|
|
++ [|("3", MigrationPair from3To4 Nothing)|] :
|
|
|
|
|
migrationsFromFile [5 .. 42]
|
|
|
|
|
++ [|("42", MigrationPair from42To43 (Just from43To42))|] :
|
|
|
|
|
migrationsFromFile [44 .. latestCatalogVersion]
|
|
|
|
|
)
|
2020-02-07 14:03:12 +03:00
|
|
|
|
where
|
|
|
|
|
runTxOrPrint :: Q.Query -> m ()
|
|
|
|
|
runTxOrPrint
|
2020-05-13 15:33:16 +03:00
|
|
|
|
| dryRun =
|
2021-09-24 01:56:37 +03:00
|
|
|
|
liftIO . TIO.putStrLn . Q.getQueryText
|
2021-09-15 23:45:49 +03:00
|
|
|
|
| otherwise = multiQ
|
2020-02-07 14:03:12 +03:00
|
|
|
|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
from42To43 = do
|
2021-02-18 19:46:14 +03:00
|
|
|
|
when (maintenanceMode == MaintenanceModeEnabled) $
|
|
|
|
|
throw500 "cannot migrate to catalog version 43 in maintenance mode"
|
2021-09-24 01:56:37 +03:00
|
|
|
|
let query = $(makeRelativeToProject "src-rsr/migrations/42_to_43.sql" >>= Q.sqlFromFile)
|
|
|
|
|
if dryRun
|
|
|
|
|
then (liftIO . TIO.putStrLn . Q.getQueryText) query
|
2020-12-08 17:22:31 +03:00
|
|
|
|
else do
|
2021-09-24 01:56:37 +03:00
|
|
|
|
metadataV2 <- fetchMetadataFromHdbTables
|
|
|
|
|
multiQ query
|
|
|
|
|
defaultSourceConfig <-
|
|
|
|
|
onNothing maybeDefaultSourceConfig $
|
|
|
|
|
throw400 NotSupported $
|
|
|
|
|
"cannot migrate to catalog version 43 without --database-url or env var " <> tshow (fst databaseUrlEnv)
|
|
|
|
|
let metadataV3 =
|
|
|
|
|
let MetadataNoSources {..} = metadataV2
|
|
|
|
|
defaultSourceMetadata =
|
|
|
|
|
AB.mkAnyBackend $
|
|
|
|
|
SourceMetadata defaultSource _mnsTables _mnsFunctions defaultSourceConfig Nothing
|
|
|
|
|
in Metadata
|
|
|
|
|
(OMap.singleton defaultSource defaultSourceMetadata)
|
|
|
|
|
_mnsRemoteSchemas
|
|
|
|
|
_mnsQueryCollections
|
|
|
|
|
_mnsAllowlist
|
|
|
|
|
_mnsCustomTypes
|
|
|
|
|
_mnsActions
|
|
|
|
|
_mnsCronTriggers
|
|
|
|
|
mempty
|
|
|
|
|
emptyApiLimit
|
|
|
|
|
emptyMetricsConfig
|
|
|
|
|
mempty
|
|
|
|
|
mempty
|
|
|
|
|
emptyNetwork
|
|
|
|
|
liftTx $ insertMetadataInCatalog metadataV3
|
2020-01-14 10:09:10 +03:00
|
|
|
|
|
2020-12-08 17:22:31 +03:00
|
|
|
|
from43To42 = do
|
2021-03-16 20:35:35 +03:00
|
|
|
|
let query = $(makeRelativeToProject "src-rsr/migrations/43_to_42.sql" >>= Q.sqlFromFile)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
if dryRun
|
|
|
|
|
then (liftIO . TIO.putStrLn . Q.getQueryText) query
|
2020-12-08 17:22:31 +03:00
|
|
|
|
else do
|
2021-09-24 01:56:37 +03:00
|
|
|
|
Metadata {..} <- liftTx fetchMetadataFromCatalog
|
|
|
|
|
multiQ query
|
|
|
|
|
let emptyMetadataNoSources =
|
|
|
|
|
MetadataNoSources mempty mempty mempty mempty mempty emptyCustomTypes mempty mempty
|
|
|
|
|
metadataV2 <- case OMap.toList _metaSources of
|
|
|
|
|
[] -> pure emptyMetadataNoSources
|
|
|
|
|
[(_, exists)] ->
|
|
|
|
|
pure $ case AB.unpackAnyBackend exists of
|
|
|
|
|
Nothing -> emptyMetadataNoSources
|
|
|
|
|
Just SourceMetadata {..} ->
|
|
|
|
|
MetadataNoSources
|
|
|
|
|
_smTables
|
|
|
|
|
_smFunctions
|
|
|
|
|
_metaRemoteSchemas
|
|
|
|
|
_metaQueryCollections
|
|
|
|
|
_metaAllowlist
|
|
|
|
|
_metaCustomTypes
|
|
|
|
|
_metaActions
|
|
|
|
|
_metaCronTriggers
|
|
|
|
|
_ -> throw400 NotSupported "Cannot downgrade since there are more than one source"
|
|
|
|
|
liftTx $ do
|
|
|
|
|
runHasSystemDefinedT (SystemDefined False) $ saveMetadataToHdbTables metadataV2
|
|
|
|
|
-- when the graphql-engine is migrated from v1 to v2, we drop the foreign key
|
|
|
|
|
-- constraint of the `hdb_catalog.hdb_cron_event` table because the cron triggers
|
|
|
|
|
-- in v2 are saved in the `hdb_catalog.hdb_metadata` table. So, when a downgrade
|
|
|
|
|
-- happens, we need to delay adding the foreign key constraint until the
|
|
|
|
|
-- cron triggers are added in the `hdb_catalog.hdb_cron_triggers`
|
|
|
|
|
addCronTriggerForeignKeyConstraint
|
|
|
|
|
recreateSystemMetadata
|
2021-09-15 23:45:49 +03:00
|
|
|
|
|
|
|
|
|
multiQ :: (MonadTx m) => Q.Query -> m ()
|
|
|
|
|
multiQ = liftTx . Q.multiQE defaultTxErrorHandler
|