mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 01:12:56 +03:00
server: Split CatalogVersion into Source… and Metadata…
This reflects the two different usages, which should not be conflated. We also propagate the type a little more, to avoid `Text`. PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4931 GitOrigin-RevId: 16278f14aa4c2cb5667ea54bbb6b25e6d362835c
This commit is contained in:
parent
82831a8611
commit
2be2200b1a
@ -39,7 +39,6 @@ import Hasura.RQL.Types.EventTrigger (RecreateEventTriggers (..))
|
||||
import Hasura.RQL.Types.Source
|
||||
import Hasura.RQL.Types.SourceCustomization
|
||||
import Hasura.SQL.Backend
|
||||
import Hasura.Server.Migrate.Version
|
||||
import Language.Haskell.TH.Lib qualified as TH
|
||||
import Language.Haskell.TH.Syntax qualified as TH
|
||||
import Text.Shakespeare.Text qualified as ST
|
||||
@ -142,15 +141,15 @@ migrateSourceCatalog :: MonadMSSQLTx m => m RecreateEventTriggers
|
||||
migrateSourceCatalog =
|
||||
getSourceCatalogVersion >>= migrateSourceCatalogFrom
|
||||
|
||||
migrateSourceCatalogFrom :: MonadMSSQLTx m => CatalogVersion -> m RecreateEventTriggers
|
||||
migrateSourceCatalogFrom :: MonadMSSQLTx m => SourceCatalogVersion -> m RecreateEventTriggers
|
||||
migrateSourceCatalogFrom prevVersion
|
||||
| prevVersion == latestSourceCatalogVersion = pure RETDoNothing
|
||||
| [] <- neededMigrations =
|
||||
throw400 NotSupported $
|
||||
"Expected source catalog version <= "
|
||||
<> latestSourceCatalogVersionText
|
||||
<> tshow latestSourceCatalogVersion
|
||||
<> ", but the current version is "
|
||||
<> (tshow prevVersion)
|
||||
<> tshow prevVersion
|
||||
| otherwise = do
|
||||
liftMSSQLTx $ traverse_ snd neededMigrations
|
||||
setSourceCatalogVersion latestSourceCatalogVersion
|
||||
@ -159,7 +158,7 @@ migrateSourceCatalogFrom prevVersion
|
||||
neededMigrations =
|
||||
dropWhile ((/= prevVersion) . fst) sourceMigrations
|
||||
|
||||
sourceMigrations :: [(CatalogVersion, TxE QErr [Text])]
|
||||
sourceMigrations :: [(SourceCatalogVersion, TxE QErr [Text])]
|
||||
sourceMigrations =
|
||||
$( let migrationFromFile from =
|
||||
let to = succ from
|
||||
|
@ -1,8 +1,9 @@
|
||||
{-# LANGUAGE QuasiQuotes #-}
|
||||
|
||||
module Hasura.Backends.MSSQL.DDL.Source.Version
|
||||
( latestSourceCatalogVersion,
|
||||
latestSourceCatalogVersionText,
|
||||
( SourceCatalogVersion,
|
||||
initialSourceCatalogVersion,
|
||||
latestSourceCatalogVersion,
|
||||
previousSourceCatalogVersions,
|
||||
setSourceCatalogVersion,
|
||||
getSourceCatalogVersion,
|
||||
@ -14,24 +15,27 @@ import Database.ODBC.SQLServer
|
||||
import Database.ODBC.TH qualified as ODBC
|
||||
import Hasura.Backends.MSSQL.Connection (MonadMSSQLTx (..))
|
||||
import Hasura.Backends.MSSQL.SQL.Error qualified as HGE
|
||||
import Hasura.Base.Error
|
||||
import Hasura.Prelude
|
||||
import Hasura.Server.Migrate.Version
|
||||
import Hasura.SQL.Backend (BackendType (MSSQL))
|
||||
import Hasura.Server.Migrate.Version qualified as Version
|
||||
|
||||
latestSourceCatalogVersion :: CatalogVersion
|
||||
latestSourceCatalogVersion = CatalogVersion 2
|
||||
type SourceCatalogVersion = Version.SourceCatalogVersion 'MSSQL
|
||||
|
||||
latestSourceCatalogVersionText :: Text
|
||||
latestSourceCatalogVersionText = tshow latestSourceCatalogVersion
|
||||
initialSourceCatalogVersion :: SourceCatalogVersion
|
||||
initialSourceCatalogVersion = Version.SourceCatalogVersion 1
|
||||
|
||||
previousSourceCatalogVersions :: [CatalogVersion]
|
||||
previousSourceCatalogVersions = [CatalogVersion 1 .. pred latestSourceCatalogVersion]
|
||||
latestSourceCatalogVersion :: SourceCatalogVersion
|
||||
latestSourceCatalogVersion = Version.SourceCatalogVersion 2
|
||||
|
||||
setSourceCatalogVersion :: MonadMSSQLTx m => CatalogVersion -> m ()
|
||||
setSourceCatalogVersion (CatalogVersion version) = liftMSSQLTx $ unitQueryE HGE.defaultMSSQLTxErrorHandler setSourceCatalogVersionQuery
|
||||
previousSourceCatalogVersions :: [SourceCatalogVersion]
|
||||
previousSourceCatalogVersions = [initialSourceCatalogVersion .. pred latestSourceCatalogVersion]
|
||||
|
||||
setSourceCatalogVersion :: MonadMSSQLTx m => SourceCatalogVersion -> m ()
|
||||
setSourceCatalogVersion (Version.SourceCatalogVersion version) =
|
||||
liftMSSQLTx $ unitQueryE HGE.defaultMSSQLTxErrorHandler setSourceCatalogVersionQuery
|
||||
where
|
||||
setSourceCatalogVersionQuery =
|
||||
[ODBC.sql|
|
||||
[ODBC.sql|
|
||||
BEGIN TRANSACTION
|
||||
IF EXISTS (select 1 from hdb_catalog.hdb_source_catalog_version WITH (UPDLOCK,SERIALIZABLE))
|
||||
BEGIN
|
||||
@ -45,9 +49,8 @@ setSourceCatalogVersion (CatalogVersion version) = liftMSSQLTx $ unitQueryE HGE.
|
||||
END
|
||||
COMMIT TRANSACTION
|
||||
|]
|
||||
setSourceCatalogVersion CatalogVersion08 =
|
||||
throw500 "Cannot set the source catalog version to an unstable version."
|
||||
|
||||
getSourceCatalogVersion :: MonadMSSQLTx m => m CatalogVersion
|
||||
getSourceCatalogVersion :: MonadMSSQLTx m => m SourceCatalogVersion
|
||||
getSourceCatalogVersion =
|
||||
CatalogVersion <$> liftMSSQLTx (singleRowQueryE HGE.defaultMSSQLTxErrorHandler [ODBC.sql| SELECT version FROM hdb_catalog.hdb_source_catalog_version |])
|
||||
Version.SourceCatalogVersion
|
||||
<$> liftMSSQLTx (singleRowQueryE HGE.defaultMSSQLTxErrorHandler [ODBC.sql| SELECT version FROM hdb_catalog.hdb_source_catalog_version |])
|
||||
|
@ -315,10 +315,10 @@ getMaintenanceModeVersionTx = liftTx $ do
|
||||
-- the previous version and the current version will change depending
|
||||
-- upon between which versions we need to support maintenance mode
|
||||
if
|
||||
| catalogVersion == CatalogVersion 40 -> pure PreviousMMVersion
|
||||
| catalogVersion == MetadataCatalogVersion 40 -> pure PreviousMMVersion
|
||||
-- The catalog is migrated to the 43rd version for a source
|
||||
-- which was initialised by a v1 graphql-engine instance (See @initSource@).
|
||||
| catalogVersion == CatalogVersion 43 -> pure CurrentMMVersion
|
||||
| catalogVersion == MetadataCatalogVersion 43 -> pure CurrentMMVersion
|
||||
| catalogVersion == latestCatalogVersion -> pure CurrentMMVersion
|
||||
| otherwise ->
|
||||
throw500 $
|
||||
|
@ -53,7 +53,7 @@ import Hasura.RQL.Types.SourceCustomization
|
||||
import Hasura.RQL.Types.Table
|
||||
import Hasura.SQL.Backend
|
||||
import Hasura.Server.Migrate.Internal
|
||||
import Hasura.Server.Migrate.Version
|
||||
import Hasura.Server.Migrate.Version (MetadataCatalogVersion (..))
|
||||
import Language.Haskell.TH.Lib qualified as TH
|
||||
import Language.Haskell.TH.Syntax qualified as TH
|
||||
|
||||
@ -199,7 +199,7 @@ prepareCatalog sourceConfig = runTx (_pscExecCtx sourceConfig) Q.ReadWrite do
|
||||
migrateTo43MetadataCatalog currMetadataCatalogVersion
|
||||
liftTx createVersionTable
|
||||
-- Migrate the catalog from initial version i.e '0'
|
||||
migrateSourceCatalogFrom "0"
|
||||
migrateSourceCatalogFrom initialSourceCatalogVersion
|
||||
| otherwise -> migrateSourceCatalog
|
||||
where
|
||||
initPgSourceCatalog = do
|
||||
@ -259,15 +259,15 @@ migrateSourceCatalog =
|
||||
-- changes introduced in the newly added source catalog migrations. When the source is already
|
||||
-- in the latest catalog version, we do nothing because nothing has changed w.r.t the source catalog
|
||||
-- so recreating the event triggers will only be extraneous.
|
||||
migrateSourceCatalogFrom :: (MonadTx m) => Text -> m RecreateEventTriggers
|
||||
migrateSourceCatalogFrom :: (MonadTx m) => SourceCatalogVersion pgKind -> m RecreateEventTriggers
|
||||
migrateSourceCatalogFrom prevVersion
|
||||
| prevVersion == latestSourceCatalogVersionText = pure RETDoNothing
|
||||
| prevVersion == latestSourceCatalogVersion = pure RETDoNothing
|
||||
| [] <- neededMigrations =
|
||||
throw400 NotSupported $
|
||||
"Expected source catalog version <= "
|
||||
<> latestSourceCatalogVersionText
|
||||
<> tshow latestSourceCatalogVersion
|
||||
<> ", but the current version is "
|
||||
<> prevVersion
|
||||
<> tshow prevVersion
|
||||
| otherwise = do
|
||||
liftTx $ traverse_ snd neededMigrations
|
||||
setSourceCatalogVersion
|
||||
@ -276,7 +276,7 @@ migrateSourceCatalogFrom prevVersion
|
||||
neededMigrations =
|
||||
dropWhile ((/= prevVersion) . fst) sourceMigrations
|
||||
|
||||
sourceMigrations :: [(Text, Q.TxE QErr ())]
|
||||
sourceMigrations :: [(SourceCatalogVersion pgKind, Q.TxE QErr ())]
|
||||
sourceMigrations =
|
||||
$( let migrationFromFile from =
|
||||
let to = succ from
|
||||
@ -284,12 +284,12 @@ sourceMigrations =
|
||||
in [|Q.multiQE defaultTxErrorHandler $(makeRelativeToProject path >>= Q.sqlFromFile)|]
|
||||
|
||||
migrationsFromFile = map $ \from ->
|
||||
[|($(TH.lift $ tshow from), $(migrationFromFile from))|]
|
||||
[|($(TH.lift from), $(migrationFromFile from))|]
|
||||
in TH.listE $ migrationsFromFile previousSourceCatalogVersions
|
||||
)
|
||||
|
||||
-- Upgrade the hdb_catalog schema to v43 (Metadata catalog)
|
||||
upMigrationsUntil43 :: [(CatalogVersion, Q.TxE QErr ())]
|
||||
upMigrationsUntil43 :: [(MetadataCatalogVersion, Q.TxE QErr ())]
|
||||
upMigrationsUntil43 =
|
||||
$( let migrationFromFile from to =
|
||||
let path = "src-rsr/migrations/" <> from <> "_to_" <> to <> ".sql"
|
||||
@ -298,7 +298,7 @@ upMigrationsUntil43 =
|
||||
migrationsFromFile = map $ \(to :: Int) ->
|
||||
let from = pred to
|
||||
in [|
|
||||
( $(TH.lift (CatalogVersion from)),
|
||||
( $(TH.lift (MetadataCatalogVersion from)),
|
||||
$(migrationFromFile (show from) (show to))
|
||||
)
|
||||
|]
|
||||
@ -310,9 +310,9 @@ upMigrationsUntil43 =
|
||||
-- moved to source catalog migrations and the 41st up migration is removed
|
||||
-- entirely.
|
||||
$
|
||||
[|(CatalogVersion08, $(migrationFromFile "08" "1"))|] :
|
||||
[|(MetadataCatalogVersion08, $(migrationFromFile "08" "1"))|] :
|
||||
migrationsFromFile [2 .. 3]
|
||||
++ [|(CatalogVersion 3, from3To4)|] :
|
||||
++ [|(MetadataCatalogVersion 3, from3To4)|] :
|
||||
(migrationsFromFile [5 .. 40]) ++ migrationsFromFile [42 .. 43]
|
||||
)
|
||||
|
||||
|
@ -4,27 +4,33 @@
|
||||
--
|
||||
-- Deals with catalog version - used by 'Hasura.Backends.Postgres.DDL.Source'.
|
||||
module Hasura.Backends.Postgres.DDL.Source.Version
|
||||
( latestSourceCatalogVersion,
|
||||
latestSourceCatalogVersionText,
|
||||
( SourceCatalogVersion,
|
||||
initialSourceCatalogVersion,
|
||||
latestSourceCatalogVersion,
|
||||
previousSourceCatalogVersions,
|
||||
getSourceCatalogVersion,
|
||||
setSourceCatalogVersion,
|
||||
)
|
||||
where
|
||||
|
||||
import Data.Text qualified as T
|
||||
import Database.PG.Query qualified as Q
|
||||
import Hasura.Backends.Postgres.Connection
|
||||
import Hasura.Base.Error
|
||||
import Hasura.Prelude
|
||||
import Hasura.Server.Migrate.Version
|
||||
import Hasura.SQL.Backend (BackendType (Postgres), PostgresKind)
|
||||
import Hasura.Server.Migrate.Version qualified as Version
|
||||
|
||||
latestSourceCatalogVersion :: CatalogVersion
|
||||
latestSourceCatalogVersion = CatalogVersion 2
|
||||
type SourceCatalogVersion (pgKind :: PostgresKind) = Version.SourceCatalogVersion ('Postgres pgKind)
|
||||
|
||||
latestSourceCatalogVersionText :: Text
|
||||
latestSourceCatalogVersionText = tshow latestSourceCatalogVersion
|
||||
initialSourceCatalogVersion :: SourceCatalogVersion pgKind
|
||||
initialSourceCatalogVersion = Version.SourceCatalogVersion 0
|
||||
|
||||
previousSourceCatalogVersions :: [CatalogVersion]
|
||||
previousSourceCatalogVersions = [CatalogVersion 0 .. pred latestSourceCatalogVersion]
|
||||
latestSourceCatalogVersion :: SourceCatalogVersion pgKind
|
||||
latestSourceCatalogVersion = Version.SourceCatalogVersion 2
|
||||
|
||||
previousSourceCatalogVersions :: [SourceCatalogVersion pgKind]
|
||||
previousSourceCatalogVersions = [initialSourceCatalogVersion .. pred latestSourceCatalogVersion]
|
||||
|
||||
setSourceCatalogVersion :: MonadTx m => m ()
|
||||
setSourceCatalogVersion =
|
||||
@ -32,20 +38,22 @@ setSourceCatalogVersion =
|
||||
Q.unitQE
|
||||
defaultTxErrorHandler
|
||||
[Q.sql|
|
||||
INSERT INTO hdb_catalog.hdb_source_catalog_version(version, upgraded_on)
|
||||
VALUES ($1, NOW())
|
||||
ON CONFLICT ((version IS NOT NULL))
|
||||
DO UPDATE SET version = $1, upgraded_on = NOW()
|
||||
|]
|
||||
(Identity latestSourceCatalogVersionText)
|
||||
INSERT INTO hdb_catalog.hdb_source_catalog_version(version, upgraded_on)
|
||||
VALUES ($1, NOW())
|
||||
ON CONFLICT ((version IS NOT NULL))
|
||||
DO UPDATE SET version = $1, upgraded_on = NOW()
|
||||
|]
|
||||
(Identity (tshow latestSourceCatalogVersion))
|
||||
False
|
||||
|
||||
getSourceCatalogVersion :: MonadTx m => m Text
|
||||
getSourceCatalogVersion =
|
||||
liftTx $
|
||||
runIdentity . Q.getRow
|
||||
<$> Q.withQE
|
||||
defaultTxErrorHandler
|
||||
[Q.sql| SELECT version FROM hdb_catalog.hdb_source_catalog_version |]
|
||||
()
|
||||
False
|
||||
getSourceCatalogVersion :: MonadTx m => m (SourceCatalogVersion postgres)
|
||||
getSourceCatalogVersion = do
|
||||
versionText <-
|
||||
liftTx $
|
||||
runIdentity . Q.getRow
|
||||
<$> Q.withQE
|
||||
defaultTxErrorHandler
|
||||
[Q.sql| SELECT version FROM hdb_catalog.hdb_source_catalog_version |]
|
||||
()
|
||||
False
|
||||
readEither (T.unpack versionText) `onLeft` (throw500 . (("Invalid source catalog version in the metadata: " <>) . T.pack))
|
||||
|
@ -157,7 +157,7 @@ migrateCatalog maybeDefaultSourceConfig maintenanceMode migrationTime = do
|
||||
pure MRInitialized
|
||||
|
||||
-- migrates an existing catalog to the latest version from an existing verion
|
||||
migrateFrom :: CatalogVersion -> m MigrationResult
|
||||
migrateFrom :: MetadataCatalogVersion -> m MigrationResult
|
||||
migrateFrom previousVersion
|
||||
| previousVersion == latestCatalogVersion = pure MRNothingToDo
|
||||
| otherwise = do
|
||||
@ -191,13 +191,13 @@ downgradeCatalog ::
|
||||
m MigrationResult
|
||||
downgradeCatalog defaultSourceConfig opts time = do
|
||||
currentCatalogVersion <- liftTx getCatalogVersion
|
||||
targetVersionFloat :: CatalogVersion <-
|
||||
targetVersionFloat :: MetadataCatalogVersion <-
|
||||
onLeft (readEither (T.unpack $ dgoTargetVersion opts)) $ \err ->
|
||||
throw500 $ "Unexpected: couldn't convert " <> dgoTargetVersion opts <> " to a float, error: " <> tshow err
|
||||
downgradeFrom currentCatalogVersion targetVersionFloat
|
||||
where
|
||||
-- downgrades an existing catalog to the specified version
|
||||
downgradeFrom :: CatalogVersion -> CatalogVersion -> m MigrationResult
|
||||
downgradeFrom :: MetadataCatalogVersion -> MetadataCatalogVersion -> m MigrationResult
|
||||
downgradeFrom previousVersion targetVersion
|
||||
| previousVersion == targetVersion = pure MRNothingToDo
|
||||
| otherwise =
|
||||
@ -223,9 +223,9 @@ downgradeCatalog defaultSourceConfig opts time = do
|
||||
(reverse (migrations defaultSourceConfig (dgoDryRun opts) MaintenanceModeDisabled))
|
||||
|
||||
downgrade ::
|
||||
CatalogVersion ->
|
||||
CatalogVersion ->
|
||||
[(CatalogVersion, MigrationPair m)] ->
|
||||
MetadataCatalogVersion ->
|
||||
MetadataCatalogVersion ->
|
||||
[(MetadataCatalogVersion, MigrationPair m)] ->
|
||||
Either Text [m ()]
|
||||
downgrade lower upper = skipFutureDowngrades
|
||||
where
|
||||
@ -235,7 +235,7 @@ downgradeCatalog defaultSourceConfig opts time = do
|
||||
-- Then we take migrations as needed until we reach the target
|
||||
-- version, dropping any remaining migrations from the end of the
|
||||
-- (reversed) list.
|
||||
skipFutureDowngrades, dropOlderDowngrades :: [(CatalogVersion, MigrationPair m)] -> Either Text [m ()]
|
||||
skipFutureDowngrades, dropOlderDowngrades :: [(MetadataCatalogVersion, MigrationPair m)] -> Either Text [m ()]
|
||||
skipFutureDowngrades xs | previousVersion == lower = dropOlderDowngrades xs
|
||||
skipFutureDowngrades [] = Left "the starting version is unrecognized."
|
||||
skipFutureDowngrades ((x, _) : xs)
|
||||
@ -255,7 +255,7 @@ migrations ::
|
||||
Maybe (SourceConnConfiguration ('Postgres 'Vanilla)) ->
|
||||
Bool ->
|
||||
MaintenanceMode () ->
|
||||
[(CatalogVersion, MigrationPair m)]
|
||||
[(MetadataCatalogVersion, MigrationPair m)]
|
||||
migrations maybeDefaultSourceConfig dryRun maintenanceMode =
|
||||
-- We need to build the list of migrations at compile-time so that we can compile the SQL
|
||||
-- directly into the executable using `Q.sqlFromFile`. The GHC stage restriction makes
|
||||
@ -271,7 +271,7 @@ migrations maybeDefaultSourceConfig dryRun maintenanceMode =
|
||||
then [|Just (runTxOrPrint $(Q.sqlFromFile path))|]
|
||||
else [|Nothing|]
|
||||
|
||||
migrationsFromFile = map $ \(to :: CatalogVersion) ->
|
||||
migrationsFromFile = map $ \(to :: MetadataCatalogVersion) ->
|
||||
let from = pred to
|
||||
in [|
|
||||
( $(TH.lift from),
|
||||
@ -287,12 +287,12 @@ migrations maybeDefaultSourceConfig dryRun maintenanceMode =
|
||||
-- source catalog changes and we'd like to keep source catalog migrations in a different
|
||||
-- path than metadata catalog migrations.
|
||||
$
|
||||
[|(CatalogVersion08, MigrationPair $(migrationFromFile "08" "1") Nothing)|] :
|
||||
migrationsFromFile [CatalogVersion 2 .. CatalogVersion 3]
|
||||
++ [|(CatalogVersion 3, MigrationPair from3To4 Nothing)|] :
|
||||
(migrationsFromFile [CatalogVersion 5 .. CatalogVersion 40] ++ migrationsFromFile [CatalogVersion 42])
|
||||
++ [|(CatalogVersion 42, MigrationPair from42To43 (Just from43To42))|] :
|
||||
migrationsFromFile [CatalogVersion 44 .. latestCatalogVersion]
|
||||
[|(MetadataCatalogVersion08, MigrationPair $(migrationFromFile "08" "1") Nothing)|] :
|
||||
migrationsFromFile [MetadataCatalogVersion 2 .. MetadataCatalogVersion 3]
|
||||
++ [|(MetadataCatalogVersion 3, MigrationPair from3To4 Nothing)|] :
|
||||
(migrationsFromFile [MetadataCatalogVersion 5 .. MetadataCatalogVersion 40] ++ migrationsFromFile [MetadataCatalogVersion 42])
|
||||
++ [|(MetadataCatalogVersion 42, MigrationPair from42To43 (Just from43To42))|] :
|
||||
migrationsFromFile [MetadataCatalogVersion 44 .. latestCatalogVersion]
|
||||
)
|
||||
where
|
||||
runTxOrPrint :: Q.Query -> m ()
|
||||
|
@ -22,7 +22,7 @@ import Hasura.Server.Migrate.Version
|
||||
|
||||
-- | The old 0.8 catalog version is non-integral, so the version has always been
|
||||
-- stored as a string.
|
||||
getCatalogVersion :: Q.TxE QErr CatalogVersion
|
||||
getCatalogVersion :: Q.TxE QErr MetadataCatalogVersion
|
||||
getCatalogVersion = do
|
||||
versionText <-
|
||||
runIdentity . Q.getRow
|
||||
|
@ -18,11 +18,11 @@ import Language.Haskell.TH.Syntax qualified as TH
|
||||
-- | The current catalog schema version. We store this in a file
|
||||
-- because we want to append the current verson to the catalog_versions file
|
||||
-- when tagging a new release, in @tag-release.sh@.
|
||||
latestCatalogVersion :: CatalogVersion
|
||||
latestCatalogVersion :: MetadataCatalogVersion
|
||||
latestCatalogVersion =
|
||||
$( do
|
||||
let s = $(makeRelativeToProject "src-rsr/catalog_version.txt" >>= embedStringFile)
|
||||
TH.lift (read s :: CatalogVersion)
|
||||
TH.lift (read s :: MetadataCatalogVersion)
|
||||
)
|
||||
|
||||
latestCatalogVersionString :: Text
|
||||
|
@ -1,37 +1,47 @@
|
||||
module Hasura.Server.Migrate.Version (CatalogVersion (..)) where
|
||||
module Hasura.Server.Migrate.Version
|
||||
( MetadataCatalogVersion (..),
|
||||
SourceCatalogVersion (..),
|
||||
)
|
||||
where
|
||||
|
||||
import Data.List (isPrefixOf)
|
||||
import Hasura.Prelude
|
||||
import Hasura.SQL.Backend (BackendType)
|
||||
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 CatalogVersion
|
||||
data MetadataCatalogVersion
|
||||
= -- | A typical catalog version.
|
||||
CatalogVersion Int
|
||||
MetadataCatalogVersion Int
|
||||
| -- | Maintained for compatibility with catalog version 0.8.
|
||||
CatalogVersion08
|
||||
MetadataCatalogVersion08
|
||||
deriving stock (Eq, Lift)
|
||||
|
||||
instance Ord CatalogVersion where
|
||||
instance Ord MetadataCatalogVersion where
|
||||
compare = compare `on` toFloat
|
||||
where
|
||||
toFloat :: CatalogVersion -> Float
|
||||
toFloat (CatalogVersion v) = fromIntegral v
|
||||
toFloat CatalogVersion08 = 0.8
|
||||
toFloat :: MetadataCatalogVersion -> Float
|
||||
toFloat (MetadataCatalogVersion v) = fromIntegral v
|
||||
toFloat MetadataCatalogVersion08 = 0.8
|
||||
|
||||
instance Enum CatalogVersion where
|
||||
toEnum = CatalogVersion
|
||||
fromEnum (CatalogVersion v) = v
|
||||
fromEnum CatalogVersion08 = error "Cannot enumerate unstable catalog versions."
|
||||
instance Enum MetadataCatalogVersion where
|
||||
toEnum = MetadataCatalogVersion
|
||||
fromEnum (MetadataCatalogVersion v) = v
|
||||
fromEnum MetadataCatalogVersion08 = error "Cannot enumerate unstable catalog versions."
|
||||
|
||||
instance Show CatalogVersion where
|
||||
show (CatalogVersion v) = show v
|
||||
show CatalogVersion08 = "0.8"
|
||||
instance Show MetadataCatalogVersion where
|
||||
show (MetadataCatalogVersion v) = show v
|
||||
show MetadataCatalogVersion08 = "0.8"
|
||||
|
||||
instance Read CatalogVersion where
|
||||
instance Read MetadataCatalogVersion where
|
||||
readsPrec prec s
|
||||
| "0.8" `isPrefixOf` s =
|
||||
[(CatalogVersion08, drop 3 s)]
|
||||
[(MetadataCatalogVersion08, drop 3 s)]
|
||||
| otherwise =
|
||||
map (first CatalogVersion) $ filter ((>= 0) . fst) $ readsPrec @Int prec s
|
||||
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 Int
|
||||
deriving newtype (Eq, Enum, Show, Read)
|
||||
deriving stock (Lift)
|
||||
|
@ -2,33 +2,39 @@ module Hasura.Server.Migrate.VersionSpec (spec) where
|
||||
|
||||
import Data.Either (isLeft)
|
||||
import Hasura.Prelude
|
||||
import Hasura.Server.Migrate.Version
|
||||
import Hasura.SQL.Backend (BackendType (MSSQL))
|
||||
import Hasura.Server.Migrate.Version (MetadataCatalogVersion (..))
|
||||
import Hasura.Server.Migrate.Version qualified as Version
|
||||
import Hedgehog.Gen qualified as Gen
|
||||
import Hedgehog.Range qualified as Range
|
||||
import Test.Hspec
|
||||
import Test.Hspec.Hedgehog
|
||||
|
||||
-- 'SourceCatalogVersion' is parameterized by a phantom backend type.
|
||||
-- The backend type is irrelevant; this is an arbitrary choice.
|
||||
type SourceCatalogVersion = Version.SourceCatalogVersion 'MSSQL
|
||||
|
||||
spec :: Spec
|
||||
spec =
|
||||
describe "a catalog version" do
|
||||
spec = do
|
||||
describe "a metadata catalog version" do
|
||||
it "can be read from an integer" $ hedgehog do
|
||||
expected :: Int <- forAll $ Gen.integral (Range.constant 0 maxBound)
|
||||
let input = show expected
|
||||
let version :: Either String CatalogVersion = readEither input
|
||||
version === Right (CatalogVersion expected)
|
||||
let version :: Either String MetadataCatalogVersion = readEither input
|
||||
version === Right (MetadataCatalogVersion expected)
|
||||
|
||||
it "cannot be read from a negative integer" $ hedgehog do
|
||||
expected :: Int <- forAll $ Gen.integral (Range.constant minBound (-1))
|
||||
let input = show expected
|
||||
let version :: Either String CatalogVersion = readEither input
|
||||
let version :: Either String MetadataCatalogVersion = readEither input
|
||||
assert $ isLeft version
|
||||
|
||||
it "can read the number 0.8, for legacy reasons" do
|
||||
let version :: Either String CatalogVersion = readEither "0.8"
|
||||
in version `shouldBe` Right CatalogVersion08
|
||||
let version :: Either String MetadataCatalogVersion = readEither "0.8"
|
||||
in version `shouldBe` Right MetadataCatalogVersion08
|
||||
|
||||
it "cannot read the number 0.8 in another form" do
|
||||
let version :: Either String CatalogVersion = readEither "8e-1"
|
||||
let version :: Either String MetadataCatalogVersion = readEither "8e-1"
|
||||
in version `shouldSatisfy` isLeft
|
||||
|
||||
it "cannot read any other non-integral number" $ hedgehog do
|
||||
@ -36,5 +42,27 @@ spec =
|
||||
forAll $
|
||||
Gen.filter (/= 0.8) $ Gen.float (Range.constantFrom 0 (-1e6) 1e6)
|
||||
let input = show expected
|
||||
let version :: Either String CatalogVersion = readEither input
|
||||
let version :: Either String MetadataCatalogVersion = readEither input
|
||||
assert $ isLeft version
|
||||
|
||||
it "shows as a number" $ hedgehog do
|
||||
expected :: Int <- forAll $ Gen.integral (Range.constant 0 maxBound)
|
||||
let input = show expected
|
||||
let version :: MetadataCatalogVersion = read input
|
||||
show version === input
|
||||
|
||||
it "shows as '0.8'" do
|
||||
show MetadataCatalogVersion08 `shouldBe` "0.8"
|
||||
|
||||
describe "a source catalog version" do
|
||||
it "can be read from an integer" $ hedgehog do
|
||||
expected :: Int <- forAll $ Gen.integral (Range.constant 0 maxBound)
|
||||
let input = show expected
|
||||
let version :: Either String SourceCatalogVersion = readEither input
|
||||
version === Right (Version.SourceCatalogVersion expected)
|
||||
|
||||
it "shows as a number" $ hedgehog do
|
||||
expected :: Int <- forAll $ Gen.integral (Range.constant 0 maxBound)
|
||||
let input = show expected
|
||||
let version :: SourceCatalogVersion = read input
|
||||
show version === input
|
||||
|
Loading…
Reference in New Issue
Block a user