graphql-engine/server/src-lib/Hasura/Server/Migrate/Version.hs
Samir Talwar 342391f39d Upgrade Ormolu to v0.5.
This upgrades the version of Ormolu required by the HGE repository to v0.5.0.1, and reformats all code accordingly.

Ormolu v0.5 reformats code that uses infix operators. This is mostly useful, adding newlines and indentation to make it clear which operators are applied first, but in some cases, it's unpleasant. To make this easier on the eyes, I had to do the following:

* Add a few fixity declarations (search for `infix`)
* Add parentheses to make precedence clear, allowing Ormolu to keep everything on one line
* Rename `relevantEq` to `(==~)` in #6651 and set it to `infix 4`
* Add a few _.ormolu_ files (thanks to @hallettj for helping me get started), mostly for Autodocodec operators that don't have explicit fixity declarations

In general, I think these changes are quite reasonable. They mostly affect indentation.

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6675
GitOrigin-RevId: cd47d87f1d089fb0bc9dcbbe7798dbceedcd7d83
2022-11-02 20:55:13 +00:00

107 lines
4.1 KiB
Haskell

module Hasura.Server.Migrate.Version
( MetadataCatalogVersion (..),
SourceCatalogVersion (..),
SourceCatalogMigrationState (..),
)
where
import Data.Aeson qualified as A
import Data.List (isPrefixOf)
import Data.Text.Extended
import Hasura.Logging (Hasura, LogLevel (..), ToEngineLog (..))
import Hasura.Prelude
import Hasura.RQL.Types.Common (SourceName)
import Hasura.SQL.Backend (BackendType)
import Hasura.Server.Logging (StartupLog (..))
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.
data MetadataCatalogVersion
= -- | A typical catalog version.
MetadataCatalogVersion Int
| -- | Maintained for compatibility with catalog version 0.8.
MetadataCatalogVersion08
deriving stock (Eq, Lift)
instance Ord MetadataCatalogVersion where
compare = compare `on` toFloat
where
toFloat :: MetadataCatalogVersion -> Float
toFloat (MetadataCatalogVersion v) = fromIntegral v
toFloat MetadataCatalogVersion08 = 0.8
instance Enum MetadataCatalogVersion where
toEnum = MetadataCatalogVersion
fromEnum (MetadataCatalogVersion v) = v
fromEnum MetadataCatalogVersion08 = error "Cannot enumerate unstable catalog versions."
instance Show MetadataCatalogVersion where
show (MetadataCatalogVersion v) = show v
show MetadataCatalogVersion08 = "0.8"
instance Read MetadataCatalogVersion where
readsPrec prec s
| "0.8" `isPrefixOf` s =
[(MetadataCatalogVersion08, drop 3 s)]
| otherwise =
map (first MetadataCatalogVersion) $ filter ((>= 0) . fst) $ readsPrec @Int prec s
-- | This is the source catalog version, used when deciding whether to (re-)create event triggers.
newtype SourceCatalogVersion (backend :: BackendType) = SourceCatalogVersion {unSourceCatalogVersion :: Int}
deriving newtype (Eq, Enum, Show, Read)
deriving stock (Lift)
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 ->
"source "
<> sourceName
<<> " is already at the latest catalog version ("
<> tshow catalogVersion
<> ")."
SCMSInitialized catalogVersion ->
"source "
<> sourceName
<<> " has the source catalog version successfully initialized (at version "
<> 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
]
}