-- | Migrations, the interpreter that runs them, identifier codecs, and-- fingerprints. A project imports "Database.DagMigrations", which re-exports-- everything that this module exports.module Database.DagMigrations.Migration ( Migration (..), hoistMigration, Interpreter (..), IdentifierCodec (..), textIdentifiers, Fingerprint (..), )whereimport Data.Set (Set)import Data.Text (Text)-- | A value that the author of a migration sets, and changes when the-- migration changes after a run has applied it to some database. A run-- records it when it applies the migration, and later runs compare it.newtype Fingerprint = Fingerprint Text deriving (Eq, Ord, Show)-- | A migration with identifier type @i@, whose actions run in the monad-- @m@ that the project chooses.data Migration i m = Migration { -- | The identifier, unique in a registry. migrationId :: i, -- | The identifiers of the migrations that this one depends on directly. migrationDependencies :: Set i, -- | A description, which the run records in the tracking table when it -- applies the migration. migrationDescription :: Text, -- | The fingerprint, if the author gives one. migrationFingerprint :: Maybe Fingerprint, -- | The action that applies the migration. migrationUp :: m (), -- | The action that reverts the migration, or 'Nothing' when the -- migration cannot be reverted. migrationDown :: Maybe (m ()) }-- | Moves a migration's actions into another monad, through a natural-- transformation. Every other field stays the same.hoistMigration :: (forall a. m a -> n a) -> Migration i m -> Migration i nhoistMigration nat migration = migration { migrationUp = nat (migrationUp migration), migrationDown = fmap nat (migrationDown migration) }-- | Runs an action of the project's monad @m@ inside an open transaction,-- and returns 'Left' when the action fails with an error of the project's-- type @e@.---- * The adapter opens the transaction, and passes its value @tx@. The-- interpreter must neither commit nor roll back the transaction; the-- adapter does both.-- * The interpreter's side effects are those of the action, and the action-- performs them inside the transaction.-- * When the action throws an exception of the adapter's database library,-- the adapter rolls back and the run reports the adapter's error. When it-- throws any other exception, the adapter rolls back and releases its-- lock, and the exception propagates out of the run.newtype Interpreter tx m e = Interpreter { runInterpreter :: forall a. tx -> m a -> IO (Either e a) }-- | Converts identifiers to and from the text that the adapter stores in-- the tracking table. The codec must satisfy-- @decodeIdentifier (encodeIdentifier i) == Just i@ for every @i@. The-- planner orders identifiers by @Ord i@, not by their encoded text. A run-- accepts a row of the tracking table only when its text is the encoding of-- the identifier that it decodes to.data IdentifierCodec i = IdentifierCodec { encodeIdentifier :: i -> Text, decodeIdentifier :: Text -> Maybe i }-- | The codec for identifiers that are already text.textIdentifiers :: IdentifierCodec TexttextIdentifiers = IdentifierCodec {encodeIdentifier = id, decodeIdentifier = Just}