2019-11-20 21:21:30 +03:00
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
|
|
|
|
|
|
|
module Hasura.Server.MigrateSpec (CacheRefT(..), spec) where
|
2019-11-18 21:45:54 +03:00
|
|
|
|
|
|
|
import Hasura.Prelude
|
|
|
|
|
2019-11-27 01:49:42 +03:00
|
|
|
import Control.Concurrent.MVar.Lifted
|
2020-12-08 17:22:31 +03:00
|
|
|
import Control.Monad.Trans.Control (MonadBaseControl)
|
2019-11-27 01:49:42 +03:00
|
|
|
import Control.Monad.Unique
|
2020-12-08 17:22:31 +03:00
|
|
|
import Control.Natural ((:~>) (..))
|
|
|
|
import Data.Time.Clock (getCurrentTime)
|
|
|
|
import Data.Tuple (swap)
|
2019-11-20 21:21:30 +03:00
|
|
|
import Test.Hspec.Core.Spec
|
|
|
|
import Test.Hspec.Expectations.Lifted
|
2019-11-18 21:45:54 +03:00
|
|
|
|
2020-12-08 17:22:31 +03:00
|
|
|
import qualified Data.Environment as Env
|
|
|
|
import qualified Database.PG.Query as Q
|
2019-11-18 21:45:54 +03:00
|
|
|
|
2020-12-08 17:22:31 +03:00
|
|
|
import Hasura.RQL.DDL.Metadata (ClearMetadata (..), runClearMetadata)
|
2019-11-20 21:21:30 +03:00
|
|
|
import Hasura.RQL.DDL.Schema
|
2020-12-08 17:22:31 +03:00
|
|
|
import Hasura.RQL.DDL.Schema.LegacyCatalog
|
2019-11-27 01:49:42 +03:00
|
|
|
import Hasura.RQL.Types
|
2020-04-24 10:55:51 +03:00
|
|
|
import Hasura.Server.API.PGDump
|
2020-12-08 17:22:31 +03:00
|
|
|
import Hasura.Server.Init (DowngradeOptions (..))
|
2019-11-18 21:45:54 +03:00
|
|
|
import Hasura.Server.Migrate
|
2020-12-08 17:22:31 +03:00
|
|
|
import Hasura.Server.Version (HasVersion)
|
2019-11-18 21:45:54 +03:00
|
|
|
|
2020-07-06 07:38:26 +03:00
|
|
|
-- -- NOTE: downgrade test disabled for now (see #5273)
|
|
|
|
|
2019-11-20 21:21:30 +03:00
|
|
|
newtype CacheRefT m a
|
2020-12-14 07:30:19 +03:00
|
|
|
= CacheRefT { runCacheRefT :: MVar RebuildableSchemaCache -> m a }
|
2019-11-20 21:21:30 +03:00
|
|
|
deriving
|
|
|
|
( Functor, Applicative, Monad, MonadIO, MonadError e, MonadBase b, MonadBaseControl b
|
|
|
|
, MonadTx, MonadUnique, UserInfoM, HasHttpManager, HasSQLGenCtx )
|
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-17 14:37:16 +03:00
|
|
|
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)
|
|
|
|
|
2020-12-14 07:30:19 +03:00
|
|
|
instance (MonadIO m, MonadBaseControl IO m, MonadTx m, HasHttpManager m, HasSQLGenCtx m) => CacheRWM (CacheRefT m) where
|
2020-12-08 17:22:31 +03:00
|
|
|
buildSchemaCacheWithOptions reason invalidations metadata = CacheRefT $ flip modifyMVar \schemaCache -> do
|
|
|
|
((), cache, _) <- runCacheRWT schemaCache (buildSchemaCacheWithOptions reason invalidations metadata)
|
2020-01-30 02:03:49 +03:00
|
|
|
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
|
|
|
|
|
|
|
|
spec
|
2020-01-23 00:55:55 +03:00
|
|
|
:: ( HasVersion
|
|
|
|
, MonadIO m
|
2019-11-20 21:21:30 +03:00
|
|
|
, MonadBaseControl IO m
|
|
|
|
, MonadTx m
|
|
|
|
, HasHttpManager m
|
|
|
|
, HasSQLGenCtx m
|
|
|
|
)
|
|
|
|
=> Q.ConnInfo -> SpecWithCache m
|
|
|
|
spec pgConnInfo = do
|
2020-12-08 17:22:31 +03:00
|
|
|
let dropAndInit env time = lift $ CacheRefT $ flip modifyMVar \_ ->
|
2020-07-14 22:00:58 +03:00
|
|
|
dropCatalog *> (swap <$> migrateCatalog env time)
|
2020-12-08 17:22:31 +03:00
|
|
|
downgradeTo v = downgradeCatalog 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-02-07 14:03:12 +03:00
|
|
|
let dumpSchema = execPGDump (PGDumpReqBody ["--schema-only"] (Just 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
|
2020-12-08 17:22:31 +03:00
|
|
|
let upgradeToLatest env time = lift $ CacheRefT $ flip modifyMVar \_ ->
|
2020-07-14 22:00:58 +03:00
|
|
|
swap <$> migrateCatalog env time
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2020-07-06 07:38:26 +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
|
2019-11-20 21:21:30 +03:00
|
|
|
let dumpMetadata = execPGDump (PGDumpReqBody ["--schema=hdb_catalog"] (Just False)) pgConnInfo
|
|
|
|
|
|
|
|
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
|
|
|
|
MRMigrated{} -> True
|
|
|
|
_ -> False
|
2019-11-20 21:21:30 +03:00
|
|
|
firstDump <- transact dumpMetadata
|
|
|
|
transact recreateSystemMetadata
|
|
|
|
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
|
|
|
|
transact (runClearMetadata ClearMetadata) `shouldReturn` successMsg
|
|
|
|
secondDump <- transact dumpMetadata
|
2019-11-18 21:45:54 +03:00
|
|
|
secondDump `shouldBe` firstDump
|