packages feed

dag-migrations-0.1.0.0 / src / Database / DagMigrations / Adapter.hs

-- | The capability that a database adapter provides. This module is for-- the authors of adapters: a project passes an t'Adapter' to the runner,-- and never calls its fields. The module re-exports t'Fingerprint' and-- t'TrackingVersion', so that an adapter author needs only this import. A-- project imports "Database.DagMigrations" instead.module Database.DagMigrations.Adapter  ( Adapter (..),    BootstrapOutcome (..),    AppliedRow (..),    Fingerprint (..),    TrackingVersion (..),  )whereimport Data.Map.Strict (Map)import Data.Text (Text)import Database.DagMigrations.Migration (Fingerprint (..))import Database.DagMigrations.Progress (TrackingVersion (..))-- | What 'bootstrap' did to the tracking table.data BootstrapOutcome  = -- | The table was current, and the adapter changed nothing.    AlreadyCurrent  | -- | The adapter created the table, at this version.    Created TrackingVersion  | -- | The adapter upgraded the table from the first version to the    -- second.    Upgraded TrackingVersion TrackingVersion  deriving (Eq, Show)-- | The row with which the runner records an applied migration.data AppliedRow = AppliedRow  { appliedIdentifier :: Text,    appliedDescription :: Text,    appliedFingerprint :: Maybe Fingerprint  }  deriving (Eq, Show)-- | The operations of one database connection. @tx@ is the adapter's-- transaction value, which only 'withTransaction' produces. @ae@ is the-- adapter's error type. Each operation returns the adapter's own errors-- as 'Left', and propagates any other exception. The one exception to this-- rule is 'withRunLock', which throws a failure to release the lock, as its-- documentation states.---- When an asynchronous exception, such as the one that-- 'System.Timeout.timeout' throws, interrupts an operation, the operation-- rethrows it, and leaves the session in an unknown state: a statement may-- still be running, a transaction may be open, and the session may later-- hold the run lock. The caller then closes the session, which ends the-- transaction and releases the lock.data Adapter tx ae = Adapter  { -- | Runs the action while holding the lock that keeps two runs apart,    -- and releases the lock when the action returns or throws. When another    -- session holds the lock, waits until that session releases it. An    -- asynchronous exception, such as the one that    -- 'System.Timeout.timeout' throws, interrupts the wait, and leaves the    -- session in an unknown state, in which it may later hold the lock.    -- After an interrupted wait, the caller closes the session. The lock    -- belongs to the session, so closing the session releases it. Returns    -- 'Left', and does not run the action, when the adapter cannot take the    -- lock: for example, when the connection fails, or when a limit on the    -- wait that the connection sets, such as PostgreSQL's @lock_timeout@,    -- expires. When the action has run and the adapter then fails to    -- release the lock, the adapter throws the failure as an exception, and    -- does not return 'Left'.    withRunLock :: forall a. IO a -> IO (Either ae a),    -- | Reads the applied identifiers and their recorded fingerprints, from    -- every version of the tracking table that the adapter recognizes.    -- Writes nothing. Returns an empty map when the tracking table does not    -- exist. From a @dbmigrations@ table (version 0), leaves out the row    -- @root@, which @dbmigrations@ writes for its own bootstrap migration,    -- reads an identifier that appears in several rows as one entry, and    -- reads no fingerprints.    readApplied :: IO (Either ae (Map Text (Maybe Fingerprint))),    -- | Creates or upgrades the tracking table, in one transaction. Does    -- nothing, and returns 'AlreadyCurrent', when the table is current, so    -- that a repeated call does no harm. An adapter that recognizes the    -- @dbmigrations@ table upgrades it without its row @root@, and returns    -- 'Upgraded' with version 0 as the first version. When that table holds    -- an identifier in several rows, the upgraded table holds one row for    -- the identifier. A row of that table holds nothing but the    -- identifier, so the upgrade loses no information.    bootstrap :: IO (Either ae BootstrapOutcome),    -- | Runs the action in a transaction. Commits when the action returns    -- 'Right', and rolls back when it returns 'Left'. When the action throws    -- an exception of the adapter's database library, rolls back and returns    -- the adapter's error as 'Left' ('Left' @ae@). When it throws any other    -- exception, rolls back and rethrows; an asynchronous exception that    -- interrupts a statement leaves the session to the caller to close, as    -- the documentation of t'Adapter' states.    withTransaction :: forall e a. (tx -> IO (Either e a)) -> IO (Either (Either ae e) a),    -- | Records an applied migration inside the transaction, with the    -- database's current time. Fails with the adapter's error when a row    -- for the identifier already exists.    recordApplied :: tx -> AppliedRow -> IO (Either ae ()),    -- | Removes the row of a reverted migration inside the transaction.    -- Fails with the adapter's error when no row for the identifier exists.    -- The runner removes only rows that it read while it held the run lock,    -- so when the row is missing, another process has changed the table.    removeApplied :: tx -> Text -> IO (Either ae ()),    -- | Sets the recorded fingerprints of the given applied migrations    -- inside the transaction. Ignores identifiers that have no row.    recordFingerprints :: tx -> Map Text (Maybe Fingerprint) -> IO (Either ae ())  }