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
|
|
|
|
|
|
|
|
import Data.Time.Clock (getCurrentTime)
|
2019-11-20 21:21:30 +03:00
|
|
|
import Test.Hspec.Core.Spec
|
|
|
|
import Test.Hspec.Expectations.Lifted
|
|
|
|
import Control.Concurrent.MVar.Lifted
|
|
|
|
import Control.Monad.Unique
|
|
|
|
import Control.Monad.Trans.Control (MonadBaseControl)
|
|
|
|
import Data.Tuple (swap)
|
|
|
|
import Control.Natural ((:~>)(..))
|
2019-11-18 21:45:54 +03:00
|
|
|
|
|
|
|
import qualified Database.PG.Query as Q
|
|
|
|
|
|
|
|
import Hasura.RQL.DDL.Metadata (ClearMetadata (..), runClearMetadata)
|
2019-11-20 21:21:30 +03:00
|
|
|
import Hasura.RQL.Types
|
|
|
|
import Hasura.RQL.DDL.Schema
|
2019-11-18 21:45:54 +03:00
|
|
|
import Hasura.Server.Migrate
|
|
|
|
import Hasura.Server.PGDump
|
|
|
|
|
2019-11-20 21:21:30 +03:00
|
|
|
newtype CacheRefT m a
|
|
|
|
= CacheRefT { runCacheRefT :: MVar (RebuildableSchemaCache m) -> m a }
|
|
|
|
deriving
|
|
|
|
( Functor, Applicative, Monad, MonadIO, MonadError e, MonadBase b, MonadBaseControl b
|
|
|
|
, MonadTx, MonadUnique, UserInfoM, HasHttpManager, HasSQLGenCtx )
|
|
|
|
via (ReaderT (MVar (RebuildableSchemaCache m)) m)
|
|
|
|
|
|
|
|
instance MonadTrans CacheRefT where
|
|
|
|
lift = CacheRefT . const
|
|
|
|
|
|
|
|
instance (MonadBase IO m) => TableCoreInfoRM (CacheRefT m)
|
|
|
|
instance (MonadBase IO m) => CacheRM (CacheRefT m) where
|
|
|
|
askSchemaCache = CacheRefT (fmap lastBuiltSchemaCache . readMVar)
|
|
|
|
|
|
|
|
instance (MonadIO m, MonadBaseControl IO m, MonadTx m, MonadUnique m) => CacheRWM (CacheRefT m) where
|
|
|
|
buildSchemaCacheWithOptions options = CacheRefT $ flip modifyMVar \schemaCache ->
|
|
|
|
swap <$> runCacheRWT schemaCache (buildSchemaCacheWithOptions options)
|
|
|
|
invalidateCachedRemoteSchema name = CacheRefT $ flip modifyMVar \schemaCache ->
|
|
|
|
swap <$> runCacheRWT schemaCache (invalidateCachedRemoteSchema name)
|
2019-11-18 21:45:54 +03:00
|
|
|
|
2019-11-20 21:21:30 +03:00
|
|
|
instance Example (CacheRefT m ()) where
|
|
|
|
type Arg (CacheRefT m ()) = CacheRefT m :~> IO
|
|
|
|
evaluateExample m params action = evaluateExample (action ($$ m)) params ($ ())
|
|
|
|
|
|
|
|
type SpecWithCache m = SpecWith (CacheRefT m :~> IO)
|
|
|
|
|
|
|
|
singleTransaction :: CacheRefT m () -> CacheRefT m ()
|
|
|
|
singleTransaction = id
|
|
|
|
|
|
|
|
spec
|
|
|
|
:: ( MonadIO m
|
|
|
|
, MonadBaseControl IO m
|
|
|
|
, MonadTx m
|
|
|
|
, MonadUnique m
|
|
|
|
, UserInfoM m
|
|
|
|
, HasHttpManager m
|
|
|
|
, HasSQLGenCtx m
|
|
|
|
)
|
|
|
|
=> Q.ConnInfo -> SpecWithCache m
|
|
|
|
spec pgConnInfo = do
|
|
|
|
let dropAndInit time = CacheRefT $ flip modifyMVar \_ ->
|
|
|
|
dropCatalog *> (swap <$> migrateCatalog time)
|
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
|
|
|
|
(dropAndInit =<< liftIO getCurrentTime) `shouldReturn` MRInitialized
|
2019-11-18 21:45:54 +03:00
|
|
|
|
2019-11-20 21:21:30 +03:00
|
|
|
it "is idempotent" \(NT transact) -> do
|
|
|
|
let dumpSchema = transact $
|
2019-11-18 21:45:54 +03:00
|
|
|
execPGDump (PGDumpReqBody ["--schema-only"] (Just False)) pgConnInfo
|
|
|
|
time <- getCurrentTime
|
2019-11-20 21:21:30 +03:00
|
|
|
transact (dropAndInit time) `shouldReturn` MRInitialized
|
2019-11-18 21:45:54 +03:00
|
|
|
firstDump <- dumpSchema
|
2019-11-20 21:21:30 +03:00
|
|
|
transact (dropAndInit time) `shouldReturn` MRInitialized
|
2019-11-18 21:45:54 +03:00
|
|
|
secondDump <- dumpSchema
|
|
|
|
secondDump `shouldBe` firstDump
|
|
|
|
|
|
|
|
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
|
|
|
|
(transact . dropAndInit =<< getCurrentTime) `shouldReturn` MRInitialized
|
|
|
|
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
|
|
|
|
(transact . dropAndInit =<< getCurrentTime) `shouldReturn` MRInitialized
|
|
|
|
firstDump <- transact dumpMetadata
|
|
|
|
transact (runClearMetadata ClearMetadata) `shouldReturn` successMsg
|
|
|
|
secondDump <- transact dumpMetadata
|
2019-11-18 21:45:54 +03:00
|
|
|
secondDump `shouldBe` firstDump
|