{-# LANGUAGE TemplateHaskell #-}
module BtcLsp.Storage.Migration
( migrateAll,
)
where
import BtcLsp.Import
import qualified BtcLsp.Import.Psql as Psql
import qualified Database.Persist.Migration as PsqlMig
import qualified Database.Persist.Migration.Postgres as PsqlMig
migrateBefore :: PsqlMig.Migration
migrateBefore :: Migration
migrateBefore =
[ Version
1 Version -> Version -> OperationPath
PsqlMig.~> Version
2
OperationPath -> [Operation] -> MigrationPath
PsqlMig.:= [ Operation
dropFundInvHashConstraint,
Operation
dropFundInvoice,
Operation
dropFundInvHash,
Operation
dropFundProof
]
]
where
dropFundInvHashConstraint :: Operation
dropFundInvHashConstraint =
Text -> SqlPersistT IO [MigrateSql] -> Operation
PsqlMig.RawOperation Text
"Drop fund_inv_hash unique constraint" (SqlPersistT IO [MigrateSql] -> Operation)
-> SqlPersistT IO [MigrateSql] -> Operation
forall a b. (a -> b) -> a -> b
$
IO [MigrateSql] -> SqlPersistT IO [MigrateSql]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (IO [MigrateSql] -> SqlPersistT IO [MigrateSql])
-> ([MigrateSql] -> IO [MigrateSql])
-> [MigrateSql]
-> SqlPersistT IO [MigrateSql]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MigrateSql] -> IO [MigrateSql]
forall (m :: * -> *) a. Monad m => a -> m a
return ([MigrateSql] -> SqlPersistT IO [MigrateSql])
-> [MigrateSql] -> SqlPersistT IO [MigrateSql]
forall a b. (a -> b) -> a -> b
$
[Text -> [PersistValue] -> MigrateSql
PsqlMig.MigrateSql Text
"ALTER TABLE IF EXISTS swap_into_ln DROP CONSTRAINT IF EXISTS unique_swap_into_ln_fund_inv_hash" []]
dropFundInvoice :: Operation
dropFundInvoice =
Text -> SqlPersistT IO [MigrateSql] -> Operation
PsqlMig.RawOperation Text
"Drop fund_invoice" (SqlPersistT IO [MigrateSql] -> Operation)
-> SqlPersistT IO [MigrateSql] -> Operation
forall a b. (a -> b) -> a -> b
$
IO [MigrateSql] -> SqlPersistT IO [MigrateSql]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (IO [MigrateSql] -> SqlPersistT IO [MigrateSql])
-> ([MigrateSql] -> IO [MigrateSql])
-> [MigrateSql]
-> SqlPersistT IO [MigrateSql]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MigrateSql] -> IO [MigrateSql]
forall (m :: * -> *) a. Monad m => a -> m a
return ([MigrateSql] -> SqlPersistT IO [MigrateSql])
-> [MigrateSql] -> SqlPersistT IO [MigrateSql]
forall a b. (a -> b) -> a -> b
$
[Text -> [PersistValue] -> MigrateSql
PsqlMig.MigrateSql Text
"ALTER TABLE IF EXISTS swap_into_ln DROP COLUMN IF EXISTS fund_invoice" []]
dropFundInvHash :: Operation
dropFundInvHash =
Text -> SqlPersistT IO [MigrateSql] -> Operation
PsqlMig.RawOperation Text
"Drop fund_inv_hash" (SqlPersistT IO [MigrateSql] -> Operation)
-> SqlPersistT IO [MigrateSql] -> Operation
forall a b. (a -> b) -> a -> b
$
IO [MigrateSql] -> SqlPersistT IO [MigrateSql]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (IO [MigrateSql] -> SqlPersistT IO [MigrateSql])
-> ([MigrateSql] -> IO [MigrateSql])
-> [MigrateSql]
-> SqlPersistT IO [MigrateSql]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MigrateSql] -> IO [MigrateSql]
forall (m :: * -> *) a. Monad m => a -> m a
return ([MigrateSql] -> SqlPersistT IO [MigrateSql])
-> [MigrateSql] -> SqlPersistT IO [MigrateSql]
forall a b. (a -> b) -> a -> b
$
[Text -> [PersistValue] -> MigrateSql
PsqlMig.MigrateSql Text
"ALTER TABLE IF EXISTS swap_into_ln DROP COLUMN IF EXISTS fund_inv_hash" []]
dropFundProof :: Operation
dropFundProof =
Text -> SqlPersistT IO [MigrateSql] -> Operation
PsqlMig.RawOperation Text
"Drop fund_proof" (SqlPersistT IO [MigrateSql] -> Operation)
-> SqlPersistT IO [MigrateSql] -> Operation
forall a b. (a -> b) -> a -> b
$
IO [MigrateSql] -> SqlPersistT IO [MigrateSql]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (IO [MigrateSql] -> SqlPersistT IO [MigrateSql])
-> ([MigrateSql] -> IO [MigrateSql])
-> [MigrateSql]
-> SqlPersistT IO [MigrateSql]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MigrateSql] -> IO [MigrateSql]
forall (m :: * -> *) a. Monad m => a -> m a
return ([MigrateSql] -> SqlPersistT IO [MigrateSql])
-> [MigrateSql] -> SqlPersistT IO [MigrateSql]
forall a b. (a -> b) -> a -> b
$
[Text -> [PersistValue] -> MigrateSql
PsqlMig.MigrateSql Text
"ALTER TABLE IF EXISTS swap_into_ln DROP COLUMN IF EXISTS fund_proof" []]
migrateAfter :: PsqlMig.Migration
migrateAfter :: Migration
migrateAfter =
[ Version
0 Version -> Version -> OperationPath
PsqlMig.~> Version
1 OperationPath -> [Operation] -> MigrationPath
PsqlMig.:= [Operation
lnChanSearchIndexes]
]
where
lnChanSearchIndexesSql :: Text
lnChanSearchIndexesSql :: Text
lnChanSearchIndexesSql =
Text
"CREATE INDEX IF NOT EXISTS "
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"ln_chan_status_idx "
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"ON ln_chan (status);"
lnChanSearchIndexes :: Operation
lnChanSearchIndexes =
Text -> SqlPersistT IO [MigrateSql] -> Operation
PsqlMig.RawOperation Text
"Create LnChan search indexes" (SqlPersistT IO [MigrateSql] -> Operation)
-> SqlPersistT IO [MigrateSql] -> Operation
forall a b. (a -> b) -> a -> b
$
IO [MigrateSql] -> SqlPersistT IO [MigrateSql]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (IO [MigrateSql] -> SqlPersistT IO [MigrateSql])
-> ([MigrateSql] -> IO [MigrateSql])
-> [MigrateSql]
-> SqlPersistT IO [MigrateSql]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MigrateSql] -> IO [MigrateSql]
forall (m :: * -> *) a. Monad m => a -> m a
return ([MigrateSql] -> SqlPersistT IO [MigrateSql])
-> [MigrateSql] -> SqlPersistT IO [MigrateSql]
forall a b. (a -> b) -> a -> b
$
[Text -> [PersistValue] -> MigrateSql
PsqlMig.MigrateSql Text
lnChanSearchIndexesSql []]
migrateAll :: (Storage m, KatipContext m) => m ()
migrateAll :: forall (m :: * -> *). (Storage m, KatipContext m) => m ()
migrateAll = do
$(logTM) Severity
InfoS LogStr
"Running Persistent BEFORE migrations..."
Migration -> m ()
forall {m :: * -> *}. Storage m => Migration -> m ()
runM Migration
migrateBefore
$(logTM) Severity
InfoS LogStr
"Running Persistent AUTO migrations..."
ReaderT SqlBackend m () -> m ()
forall (m :: * -> *) a. Storage m => ReaderT SqlBackend m a -> m a
runSql (Migration -> ReaderT SqlBackend m ()
forall (m :: * -> *).
MonadIO m =>
Migration -> ReaderT SqlBackend m ()
Psql.runMigration Migration
migrateAuto)
$(logTM) Severity
InfoS LogStr
"Running Persistent AFTER migrations..."
Migration -> m ()
forall {m :: * -> *}. Storage m => Migration -> m ()
runM Migration
migrateAfter
$(logTM) Severity
InfoS LogStr
"Persistent database migrated!"
where
runM :: Migration -> m ()
runM [] = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
runM Migration
x = do
Pool SqlBackend
pool <- m (Pool SqlBackend)
forall (m :: * -> *). Storage m => m (Pool SqlBackend)
getSqlPool
IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$
ReaderT SqlBackend IO () -> Pool SqlBackend -> IO ()
forall backend (m :: * -> *) a.
(MonadUnliftIO m, BackendCompatible SqlBackend backend) =>
ReaderT backend m a -> Pool backend -> m a
Psql.runSqlPool
(MigrateSettings -> Migration -> ReaderT SqlBackend IO ()
PsqlMig.runMigration MigrateSettings
PsqlMig.defaultSettings Migration
x)
Pool SqlBackend
pool