2022-07-04 12:30:53 +03:00
|
|
|
module Hasura.Server.Migrate.Version
|
|
|
|
( MetadataCatalogVersion (..),
|
|
|
|
SourceCatalogVersion (..),
|
2022-11-02 01:41:22 +03:00
|
|
|
SourceCatalogMigrationState (..),
|
2022-07-04 12:30:53 +03:00
|
|
|
)
|
|
|
|
where
|
2022-03-16 03:39:21 +03:00
|
|
|
|
2022-11-02 01:41:22 +03:00
|
|
|
import Data.Aeson qualified as A
|
2022-06-29 11:18:32 +03:00
|
|
|
import Data.List (isPrefixOf)
|
2022-11-02 01:41:22 +03:00
|
|
|
import Data.Text.Extended
|
|
|
|
import Hasura.Logging (Hasura, LogLevel (..), ToEngineLog (..))
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Prelude
|
2022-11-02 01:41:22 +03:00
|
|
|
import Hasura.RQL.Types.Common (SourceName)
|
2022-07-04 12:30:53 +03:00
|
|
|
import Hasura.SQL.Backend (BackendType)
|
2022-11-02 01:41:22 +03:00
|
|
|
import Hasura.Server.Logging (StartupLog (..))
|
2022-06-29 11:18:32 +03:00
|
|
|
import Language.Haskell.TH.Lift (Lift)
|
|
|
|
|
|
|
|
-- | Represents the catalog version. This is stored in the database and then
|
|
|
|
-- compared with the latest version on startup.
|
2022-07-04 12:30:53 +03:00
|
|
|
data MetadataCatalogVersion
|
2022-06-29 11:18:32 +03:00
|
|
|
= -- | A typical catalog version.
|
2022-07-04 12:30:53 +03:00
|
|
|
MetadataCatalogVersion Int
|
2022-06-29 11:18:32 +03:00
|
|
|
| -- | Maintained for compatibility with catalog version 0.8.
|
2022-07-04 12:30:53 +03:00
|
|
|
MetadataCatalogVersion08
|
2022-06-29 11:18:32 +03:00
|
|
|
deriving stock (Eq, Lift)
|
|
|
|
|
2022-07-04 12:30:53 +03:00
|
|
|
instance Ord MetadataCatalogVersion where
|
2022-06-29 11:18:32 +03:00
|
|
|
compare = compare `on` toFloat
|
|
|
|
where
|
2022-07-04 12:30:53 +03:00
|
|
|
toFloat :: MetadataCatalogVersion -> Float
|
|
|
|
toFloat (MetadataCatalogVersion v) = fromIntegral v
|
|
|
|
toFloat MetadataCatalogVersion08 = 0.8
|
2022-06-29 11:18:32 +03:00
|
|
|
|
2022-07-04 12:30:53 +03:00
|
|
|
instance Enum MetadataCatalogVersion where
|
|
|
|
toEnum = MetadataCatalogVersion
|
|
|
|
fromEnum (MetadataCatalogVersion v) = v
|
|
|
|
fromEnum MetadataCatalogVersion08 = error "Cannot enumerate unstable catalog versions."
|
2020-02-24 17:33:56 +03:00
|
|
|
|
2022-07-04 12:30:53 +03:00
|
|
|
instance Show MetadataCatalogVersion where
|
|
|
|
show (MetadataCatalogVersion v) = show v
|
|
|
|
show MetadataCatalogVersion08 = "0.8"
|
2019-10-11 08:13:57 +03:00
|
|
|
|
2022-07-04 12:30:53 +03:00
|
|
|
instance Read MetadataCatalogVersion where
|
2022-06-29 11:18:32 +03:00
|
|
|
readsPrec prec s
|
|
|
|
| "0.8" `isPrefixOf` s =
|
2022-11-02 23:53:23 +03:00
|
|
|
[(MetadataCatalogVersion08, drop 3 s)]
|
2022-06-29 11:18:32 +03:00
|
|
|
| otherwise =
|
2022-11-02 23:53:23 +03:00
|
|
|
map (first MetadataCatalogVersion) $ filter ((>= 0) . fst) $ readsPrec @Int prec s
|
2022-07-04 12:30:53 +03:00
|
|
|
|
|
|
|
-- | This is the source catalog version, used when deciding whether to (re-)create event triggers.
|
2022-11-02 01:41:22 +03:00
|
|
|
newtype SourceCatalogVersion (backend :: BackendType) = SourceCatalogVersion {unSourceCatalogVersion :: Int}
|
2022-07-04 12:30:53 +03:00
|
|
|
deriving newtype (Eq, Enum, Show, Read)
|
|
|
|
deriving stock (Lift)
|
2022-11-02 01:41:22 +03:00
|
|
|
|
|
|
|
data SourceCatalogMigrationState
|
|
|
|
= -- | Source has not been initialized yet.
|
|
|
|
SCMSUninitializedSource
|
|
|
|
| -- | Source catalog is already at the latest catalog version.
|
|
|
|
SCMSNothingToDo Int
|
|
|
|
| -- | Initialization of the source catalog along with the catalog version.
|
|
|
|
SCMSInitialized Int
|
|
|
|
| -- | Source catalog migration <old catalog version> to <new catalog version>.
|
|
|
|
SCMSMigratedTo Int Int
|
|
|
|
| -- | Source catalog migration on hold with reason (Maintenance mode, read only mode etc).
|
|
|
|
SCMSMigrationOnHold Text
|
|
|
|
| SCMSNotSupported
|
|
|
|
|
|
|
|
instance ToEngineLog (SourceName, SourceCatalogMigrationState) Hasura where
|
|
|
|
toEngineLog (sourceName, migrationStatus) =
|
|
|
|
let migrationStatusMessage =
|
|
|
|
case migrationStatus of
|
|
|
|
SCMSUninitializedSource -> "source " <> sourceName <<> " has not been initialized yet."
|
|
|
|
SCMSNothingToDo catalogVersion ->
|
2022-11-02 23:53:23 +03:00
|
|
|
"source "
|
|
|
|
<> sourceName
|
|
|
|
<<> " is already at the latest catalog version ("
|
2022-11-02 01:41:22 +03:00
|
|
|
<> tshow catalogVersion
|
|
|
|
<> ")."
|
|
|
|
SCMSInitialized catalogVersion ->
|
2022-11-02 23:53:23 +03:00
|
|
|
"source "
|
|
|
|
<> sourceName
|
|
|
|
<<> " has the source catalog version successfully initialized (at version "
|
2022-11-02 01:41:22 +03:00
|
|
|
<> tshow catalogVersion
|
|
|
|
<> ")."
|
|
|
|
SCMSMigratedTo oldCatalogVersion newCatalogVersion ->
|
|
|
|
"source "
|
|
|
|
<> sourceName <<> " has been migrated successfully from catalog version "
|
|
|
|
<> tshow oldCatalogVersion
|
|
|
|
<> " to "
|
|
|
|
<> tshow newCatalogVersion
|
|
|
|
<> "."
|
|
|
|
SCMSMigrationOnHold reason ->
|
|
|
|
"Source catalog migration for source: " <> sourceName <<> " is on hold due to " <> reason <> "."
|
|
|
|
SCMSNotSupported ->
|
|
|
|
"Source catalog migration is not supported for source " <>> sourceName
|
|
|
|
in toEngineLog $
|
|
|
|
StartupLog
|
|
|
|
{ slLogLevel = LevelInfo,
|
|
|
|
slKind = "source_catalog_migrate",
|
|
|
|
slInfo =
|
|
|
|
A.toJSON $
|
|
|
|
A.object
|
|
|
|
[ "source" A..= sourceName,
|
|
|
|
"message" A..= migrationStatusMessage
|
|
|
|
]
|
|
|
|
}
|