2019-11-20 21:21:30 +03:00
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
module Hasura.Server.MigrateSpec (CacheRefT (..), spec) where
|
|
|
|
|
|
|
|
import Control.Concurrent.MVar.Lifted
|
|
|
|
import Control.Monad.Morph
|
|
|
|
import Control.Monad.Trans.Control (MonadBaseControl)
|
|
|
|
import Control.Natural ((:~>) (..))
|
|
|
|
import Data.Aeson (encode)
|
|
|
|
import Data.ByteString.Lazy.UTF8 qualified as LBS
|
|
|
|
import Data.Environment qualified as Env
|
|
|
|
import Data.Time.Clock (getCurrentTime)
|
|
|
|
import Database.PG.Query qualified as Q
|
|
|
|
import Hasura.Backends.Postgres.Connection
|
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.Logging
|
|
|
|
import Hasura.Metadata.Class
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.DDL.Metadata (ClearMetadata (..), runClearMetadata)
|
|
|
|
import Hasura.RQL.DDL.Schema
|
|
|
|
import Hasura.RQL.DDL.Schema.Cache.Common
|
2022-02-08 19:53:30 +03:00
|
|
|
import Hasura.RQL.DDL.Schema.LegacyCatalog (recreateSystemMetadata)
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.Types.Common
|
|
|
|
import Hasura.RQL.Types.SchemaCache
|
|
|
|
import Hasura.RQL.Types.SchemaCache.Build
|
|
|
|
import Hasura.RQL.Types.Source
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Server.API.PGDump
|
|
|
|
import Hasura.Server.Init (DowngradeOptions (..))
|
|
|
|
import Hasura.Server.Migrate
|
2022-04-22 17:50:01 +03:00
|
|
|
import Hasura.Server.Types
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Session
|
|
|
|
import Network.HTTP.Client.Manager qualified as HTTP
|
|
|
|
import Test.Hspec.Core.Spec
|
|
|
|
import Test.Hspec.Expectations.Lifted
|
2019-11-18 21:45:54 +03:00
|
|
|
|
2020-07-06 07:38:26 +03:00
|
|
|
-- -- NOTE: downgrade test disabled for now (see #5273)
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
newtype CacheRefT m a = CacheRefT {runCacheRefT :: MVar RebuildableSchemaCache -> m a}
|
2019-11-20 21:21:30 +03:00
|
|
|
deriving
|
2021-09-24 01:56:37 +03:00
|
|
|
( Functor,
|
|
|
|
Applicative,
|
|
|
|
Monad,
|
|
|
|
MonadIO,
|
|
|
|
MonadError e,
|
|
|
|
MonadBase b,
|
|
|
|
MonadBaseControl b,
|
|
|
|
MonadTx,
|
|
|
|
UserInfoM,
|
|
|
|
HTTP.HasHttpManagerM,
|
|
|
|
HasServerConfigCtx,
|
|
|
|
MonadMetadataStorage,
|
|
|
|
MonadMetadataStorageQueryAPI
|
|
|
|
)
|
2020-12-14 07:30:19 +03:00
|
|
|
via (ReaderT (MVar RebuildableSchemaCache) m)
|
2019-11-20 21:21:30 +03:00
|
|
|
|
|
|
|
instance MonadTrans CacheRefT where
|
|
|
|
lift = CacheRefT . const
|
|
|
|
|
2020-12-28 15:56:00 +03:00
|
|
|
instance MFunctor CacheRefT where
|
|
|
|
hoist f (CacheRefT m) = CacheRefT (f . m)
|
|
|
|
|
|
|
|
-- instance (MonadBase IO m) => TableCoreInfoRM 'Postgres (CacheRefT m)
|
2019-11-20 21:21:30 +03:00
|
|
|
instance (MonadBase IO m) => CacheRM (CacheRefT m) where
|
|
|
|
askSchemaCache = CacheRefT (fmap lastBuiltSchemaCache . readMVar)
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
instance
|
|
|
|
( MonadIO m,
|
|
|
|
MonadBaseControl IO m,
|
|
|
|
MonadError QErr m,
|
|
|
|
HTTP.HasHttpManagerM m,
|
|
|
|
MonadResolveSource m,
|
|
|
|
HasServerConfigCtx m
|
|
|
|
) =>
|
|
|
|
CacheRWM (CacheRefT m)
|
|
|
|
where
|
2021-04-06 06:25:02 +03:00
|
|
|
buildSchemaCacheWithOptions reason invalidations metadata =
|
|
|
|
CacheRefT $ flip modifyMVar \schemaCache -> do
|
|
|
|
((), cache, _) <- runCacheRWT schemaCache (buildSchemaCacheWithOptions reason invalidations metadata)
|
|
|
|
pure (cache, ())
|
|
|
|
|
|
|
|
setMetadataResourceVersionInSchemaCache resourceVersion =
|
|
|
|
CacheRefT $ flip modifyMVar \schemaCache -> do
|
|
|
|
((), cache, _) <- runCacheRWT schemaCache (setMetadataResourceVersionInSchemaCache resourceVersion)
|
|
|
|
pure (cache, ())
|
2019-11-18 21:45:54 +03:00
|
|
|
|
2020-12-08 17:22:31 +03:00
|
|
|
instance Example (MetadataT (CacheRefT m) ()) where
|
|
|
|
type Arg (MetadataT (CacheRefT m) ()) = MetadataT (CacheRefT m) :~> IO
|
2019-11-20 21:21:30 +03:00
|
|
|
evaluateExample m params action = evaluateExample (action ($$ m)) params ($ ())
|
|
|
|
|
2020-12-08 17:22:31 +03:00
|
|
|
type SpecWithCache m = SpecWith (MetadataT (CacheRefT m) :~> IO)
|
2019-11-20 21:21:30 +03:00
|
|
|
|
2020-12-08 17:22:31 +03:00
|
|
|
singleTransaction :: MetadataT (CacheRefT m) () -> MetadataT (CacheRefT m) ()
|
2019-11-20 21:21:30 +03:00
|
|
|
singleTransaction = id
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
spec ::
|
|
|
|
forall m.
|
2021-10-13 19:38:56 +03:00
|
|
|
( MonadIO m,
|
2021-09-24 01:56:37 +03:00
|
|
|
MonadBaseControl IO m,
|
|
|
|
HTTP.HasHttpManagerM m,
|
|
|
|
HasServerConfigCtx m,
|
|
|
|
MonadResolveSource m,
|
|
|
|
MonadMetadataStorageQueryAPI m
|
|
|
|
) =>
|
|
|
|
PostgresConnConfiguration ->
|
|
|
|
PGExecCtx ->
|
|
|
|
Q.ConnInfo ->
|
|
|
|
SpecWithCache m
|
2020-12-28 15:56:00 +03:00
|
|
|
spec srcConfig pgExecCtx pgConnInfo = do
|
2021-11-09 17:21:48 +03:00
|
|
|
let logger :: Logger Hasura = Logger $ \l -> do
|
|
|
|
let (logLevel, logType :: EngineLogType Hasura, logDetail) = toEngineLog l
|
|
|
|
t <- liftIO $ getFormattedTime Nothing
|
|
|
|
liftIO $ putStrLn $ LBS.toString $ encode $ EngineLog t logLevel logType logDetail
|
|
|
|
|
|
|
|
migrateCatalogAndBuildCache env time = do
|
2021-09-15 23:45:49 +03:00
|
|
|
(migrationResult, metadata) <- runTx' pgExecCtx $ migrateCatalog (Just srcConfig) MaintenanceModeDisabled time
|
2021-11-09 17:21:48 +03:00
|
|
|
(,migrationResult) <$> runCacheBuildM (buildRebuildableSchemaCache logger env metadata)
|
2020-12-28 15:56:00 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
dropAndInit env time = lift $
|
|
|
|
CacheRefT $ flip modifyMVar \_ ->
|
|
|
|
(runTx' pgExecCtx dropHdbCatalogSchema) *> (migrateCatalogAndBuildCache env time)
|
|
|
|
downgradeTo v = runTx' pgExecCtx . downgradeCatalog (Just srcConfig) DowngradeOptions {dgoDryRun = False, dgoTargetVersion = v}
|
2020-04-24 10:55:51 +03:00
|
|
|
|
2019-11-18 21:45:54 +03:00
|
|
|
describe "migrateCatalog" $ do
|
2019-11-20 21:21:30 +03:00
|
|
|
it "initializes the catalog" $ singleTransaction do
|
2020-07-14 22:00:58 +03:00
|
|
|
env <- liftIO Env.getEnvironment
|
|
|
|
time <- liftIO getCurrentTime
|
2020-10-28 19:40:33 +03:00
|
|
|
dropAndInit env time `shouldReturn` MRInitialized
|
2019-11-18 21:45:54 +03:00
|
|
|
|
2019-11-20 21:21:30 +03:00
|
|
|
it "is idempotent" \(NT transact) -> do
|
2020-12-28 15:56:00 +03:00
|
|
|
let dumpSchema = execPGDump (PGDumpReqBody defaultSource ["--schema-only"] False) pgConnInfo
|
2020-07-14 22:00:58 +03:00
|
|
|
env <- Env.getEnvironment
|
2019-11-18 21:45:54 +03:00
|
|
|
time <- getCurrentTime
|
2020-07-14 22:00:58 +03:00
|
|
|
transact (dropAndInit env time) `shouldReturn` MRInitialized
|
2020-02-07 14:03:12 +03:00
|
|
|
firstDump <- transact dumpSchema
|
2020-07-14 22:00:58 +03:00
|
|
|
transact (dropAndInit env time) `shouldReturn` MRInitialized
|
2020-02-07 14:03:12 +03:00
|
|
|
secondDump <- transact dumpSchema
|
2019-11-18 21:45:54 +03:00
|
|
|
secondDump `shouldBe` firstDump
|
2020-04-24 10:55:51 +03:00
|
|
|
|
2020-02-07 14:03:12 +03:00
|
|
|
it "supports upgrades after downgrade to version 12" \(NT transact) -> do
|
2021-09-24 01:56:37 +03:00
|
|
|
let upgradeToLatest env time = lift $
|
|
|
|
CacheRefT $ flip modifyMVar \_ ->
|
|
|
|
migrateCatalogAndBuildCache env time
|
2020-07-14 22:00:58 +03:00
|
|
|
env <- Env.getEnvironment
|
2020-02-07 14:03:12 +03:00
|
|
|
time <- getCurrentTime
|
2020-07-14 22:00:58 +03:00
|
|
|
transact (dropAndInit env time) `shouldReturn` MRInitialized
|
2020-02-07 14:03:12 +03:00
|
|
|
downgradeResult <- (transact . lift) (downgradeTo "12" time)
|
|
|
|
downgradeResult `shouldSatisfy` \case
|
2021-09-24 01:56:37 +03:00
|
|
|
MRMigrated {} -> True
|
|
|
|
_ -> False
|
2020-07-14 22:00:58 +03:00
|
|
|
transact (upgradeToLatest env time) `shouldReturn` MRMigrated "12"
|
2020-04-24 10:55:51 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
-- -- NOTE: this has been problematic in CI and we're not quite sure how to
|
|
|
|
-- -- make this work reliably given the way we do releases and create
|
|
|
|
-- -- beta tags and so on. Phil and Alexis are okay just commenting
|
|
|
|
-- -- this until we need revisit. See #5273:
|
|
|
|
-- it "supports downgrades for every Git tag" $ singleTransaction do
|
|
|
|
-- gitOutput <- liftIO $ readProcess "git" ["log", "--no-walk", "--tags", "--pretty=%D"] ""
|
|
|
|
-- let filterOldest = filter (not . isPrefixOf "v1.0.0-alpha")
|
|
|
|
-- extractTagName = Safe.headMay . splitOn ", " <=< stripPrefix "tag: "
|
|
|
|
-- supportedDowngrades = sort (map fst downgradeShortcuts)
|
|
|
|
-- gitTags = (sort . filterOldest . mapMaybe extractTagName . tail . lines) gitOutput
|
|
|
|
-- for_ gitTags \t ->
|
|
|
|
-- t `shouldSatisfy` (`elem` supportedDowngrades)
|
2019-11-18 21:45:54 +03:00
|
|
|
|
|
|
|
describe "recreateSystemMetadata" $ do
|
2020-12-28 15:56:00 +03:00
|
|
|
let dumpMetadata = execPGDump (PGDumpReqBody defaultSource ["--schema=hdb_catalog"] False) pgConnInfo
|
2019-11-20 21:21:30 +03:00
|
|
|
|
|
|
|
it "is idempotent" \(NT transact) -> do
|
2020-07-14 22:00:58 +03:00
|
|
|
env <- Env.getEnvironment
|
|
|
|
time <- getCurrentTime
|
2020-10-28 19:40:33 +03:00
|
|
|
transact (dropAndInit env time) `shouldReturn` MRInitialized
|
2020-12-08 17:22:31 +03:00
|
|
|
-- Downgrade to catalog version before metadata separation
|
|
|
|
downgradeResult <- (transact . lift) (downgradeTo "42" time)
|
|
|
|
downgradeResult `shouldSatisfy` \case
|
2021-09-24 01:56:37 +03:00
|
|
|
MRMigrated {} -> True
|
|
|
|
_ -> False
|
2019-11-20 21:21:30 +03:00
|
|
|
firstDump <- transact dumpMetadata
|
2021-09-15 23:45:49 +03:00
|
|
|
transact (runTx' pgExecCtx recreateSystemMetadata)
|
2019-11-20 21:21:30 +03:00
|
|
|
secondDump <- transact dumpMetadata
|
2019-11-18 21:45:54 +03:00
|
|
|
secondDump `shouldBe` firstDump
|
|
|
|
|
2019-11-20 21:21:30 +03:00
|
|
|
it "does not create any objects affected by ClearMetadata" \(NT transact) -> do
|
2020-07-14 22:00:58 +03:00
|
|
|
env <- Env.getEnvironment
|
|
|
|
time <- getCurrentTime
|
2020-10-28 19:40:33 +03:00
|
|
|
transact (dropAndInit env time) `shouldReturn` MRInitialized
|
2019-11-20 21:21:30 +03:00
|
|
|
firstDump <- transact dumpMetadata
|
2021-09-09 14:54:19 +03:00
|
|
|
transact (flip runReaderT logger $ runClearMetadata ClearMetadata) `shouldReturn` successMsg
|
2019-11-20 21:21:30 +03:00
|
|
|
secondDump <- transact dumpMetadata
|
2019-11-18 21:45:54 +03:00
|
|
|
secondDump `shouldBe` firstDump
|
2020-12-28 15:56:00 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
runTx' ::
|
|
|
|
(MonadError QErr m, MonadIO m, MonadBaseControl IO m) =>
|
|
|
|
PGExecCtx ->
|
|
|
|
Q.TxET QErr m a ->
|
|
|
|
m a
|
2021-09-15 23:45:49 +03:00
|
|
|
runTx' pgExecCtx = liftEitherM . runExceptT . runTx pgExecCtx Q.ReadWrite
|