module Wire.UserSearch.Migration where

import Data.Aeson
import Database.Bloodhound.Types qualified as ES
import Imports
import Numeric.Natural
import System.Logger.Class (ToBytes (..))

newtype MigrationVersion = MigrationVersion {MigrationVersion -> Natural
migrationVersion :: Natural}
  deriving (Int -> MigrationVersion -> ShowS
[MigrationVersion] -> ShowS
MigrationVersion -> String
(Int -> MigrationVersion -> ShowS)
-> (MigrationVersion -> String)
-> ([MigrationVersion] -> ShowS)
-> Show MigrationVersion
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MigrationVersion -> ShowS
showsPrec :: Int -> MigrationVersion -> ShowS
$cshow :: MigrationVersion -> String
show :: MigrationVersion -> String
$cshowList :: [MigrationVersion] -> ShowS
showList :: [MigrationVersion] -> ShowS
Show, MigrationVersion -> MigrationVersion -> Bool
(MigrationVersion -> MigrationVersion -> Bool)
-> (MigrationVersion -> MigrationVersion -> Bool)
-> Eq MigrationVersion
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MigrationVersion -> MigrationVersion -> Bool
== :: MigrationVersion -> MigrationVersion -> Bool
$c/= :: MigrationVersion -> MigrationVersion -> Bool
/= :: MigrationVersion -> MigrationVersion -> Bool
Eq, Eq MigrationVersion
Eq MigrationVersion =>
(MigrationVersion -> MigrationVersion -> Ordering)
-> (MigrationVersion -> MigrationVersion -> Bool)
-> (MigrationVersion -> MigrationVersion -> Bool)
-> (MigrationVersion -> MigrationVersion -> Bool)
-> (MigrationVersion -> MigrationVersion -> Bool)
-> (MigrationVersion -> MigrationVersion -> MigrationVersion)
-> (MigrationVersion -> MigrationVersion -> MigrationVersion)
-> Ord MigrationVersion
MigrationVersion -> MigrationVersion -> Bool
MigrationVersion -> MigrationVersion -> Ordering
MigrationVersion -> MigrationVersion -> MigrationVersion
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: MigrationVersion -> MigrationVersion -> Ordering
compare :: MigrationVersion -> MigrationVersion -> Ordering
$c< :: MigrationVersion -> MigrationVersion -> Bool
< :: MigrationVersion -> MigrationVersion -> Bool
$c<= :: MigrationVersion -> MigrationVersion -> Bool
<= :: MigrationVersion -> MigrationVersion -> Bool
$c> :: MigrationVersion -> MigrationVersion -> Bool
> :: MigrationVersion -> MigrationVersion -> Bool
$c>= :: MigrationVersion -> MigrationVersion -> Bool
>= :: MigrationVersion -> MigrationVersion -> Bool
$cmax :: MigrationVersion -> MigrationVersion -> MigrationVersion
max :: MigrationVersion -> MigrationVersion -> MigrationVersion
$cmin :: MigrationVersion -> MigrationVersion -> MigrationVersion
min :: MigrationVersion -> MigrationVersion -> MigrationVersion
Ord)

instance ToJSON MigrationVersion where
  toJSON :: MigrationVersion -> Value
toJSON (MigrationVersion Natural
v) = [Pair] -> Value
object [Key
"migration_version" Key -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
.= Natural
v]

instance FromJSON MigrationVersion where
  parseJSON :: Value -> Parser MigrationVersion
parseJSON = String
-> (Object -> Parser MigrationVersion)
-> Value
-> Parser MigrationVersion
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"MigrationVersion" ((Object -> Parser MigrationVersion)
 -> Value -> Parser MigrationVersion)
-> (Object -> Parser MigrationVersion)
-> Value
-> Parser MigrationVersion
forall a b. (a -> b) -> a -> b
$ \Object
o -> Natural -> MigrationVersion
MigrationVersion (Natural -> MigrationVersion)
-> Parser Natural -> Parser MigrationVersion
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser Natural
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"migration_version"

instance ToBytes MigrationVersion where
  bytes :: MigrationVersion -> Builder
bytes = Integer -> Builder
forall a. ToBytes a => a -> Builder
bytes (Integer -> Builder)
-> (MigrationVersion -> Integer) -> MigrationVersion -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Natural -> Integer
forall a. Integral a => a -> Integer
toInteger (Natural -> Integer)
-> (MigrationVersion -> Natural) -> MigrationVersion -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MigrationVersion -> Natural
migrationVersion

data MigrationException
  = CreateMigrationIndexFailed String
  | FetchMigrationVersionsFailed String
  | PersistVersionFailed MigrationVersion String
  | PutMappingFailed String
  | TargetIndexAbsent
  | VersionSourceMissing (ES.SearchResult MigrationVersion)
  deriving (Int -> MigrationException -> ShowS
[MigrationException] -> ShowS
MigrationException -> String
(Int -> MigrationException -> ShowS)
-> (MigrationException -> String)
-> ([MigrationException] -> ShowS)
-> Show MigrationException
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MigrationException -> ShowS
showsPrec :: Int -> MigrationException -> ShowS
$cshow :: MigrationException -> String
show :: MigrationException -> String
$cshowList :: [MigrationException] -> ShowS
showList :: [MigrationException] -> ShowS
Show)

instance Exception MigrationException