{-# LANGUAGE GHC2021 #-}{-# LANGUAGE OverloadedStrings #-}-- | The monad of the example project's migrations, and its operations.-- "Database.DagMigrations.PostgreSQL.Example" holds the migrations; the-- guides @writing-haskell-migrations.md@ and-- @converting-from-dbmigrations.md@ quote both modules verbatim.---- This module does not export the constructor of 'ProjectMigration', and-- 'ProjectMigration' has no 'Control.Monad.IO.Class.MonadIO' instance. So-- a migration in another module reaches the database only through-- 'runSql', 'queryRows' and 'executeRow'. Some code can still run other-- 'IO': code that imports "Database.PostgreSQL.Simple.Internal" or-- "System.IO.Unsafe", and a 'Database.PostgreSQL.Simple.FromRow.FromRow'-- instance that runs 'IO'. SQL also runs with the privileges of the-- database role. A reviewer of a migration checks its imports, the-- instances of the row types that it reads and writes, the application-- functions that it calls, and its SQL.module Database.DagMigrations.PostgreSQL.Example.Migration ( ProjectMigration, runSql, queryRows, executeRow, failMigration, projectInterpreter, )whereimport Control.Monad (void)import Control.Monad.Trans.Class (lift)import Control.Monad.Trans.Except (ExceptT, runExceptT, throwE)import Control.Monad.Trans.Reader (ReaderT (..), runReaderT)import Data.Text (Text)import Database.DagMigrations (Interpreter (..))import Database.DagMigrations.PostgreSQL.Simple (Transaction, executeSqlIn, transactionConnection)import Database.PostgreSQL.Simple (FromRow, Query, ToRow, execute, query)-- | The monad in which the project's migrations run.newtype ProjectMigration a = ProjectMigration (ReaderT Transaction (ExceptT Text IO) a) deriving (Functor, Applicative, Monad)-- | Runs SQL text in the migration's transaction. The text may hold several-- statements, and its last statement may be a query, whose rows 'runSql'-- discards. The text must not hold @COMMIT@, @ROLLBACK@,-- @COPY ... FROM STDIN@ or @COPY ... TO STDOUT@. When PostgreSQL rejects a-- statement, the migration fails with the adapter's error, and the adapter-- rolls back its transaction.runSql :: Text -> ProjectMigration ()runSql sql = ProjectMigration (ReaderT (lift . (`executeSqlIn` sql)))-- | Runs a query in the migration's transaction, with the parameters in-- place of its @?@ marks, and returns its rows. The query holds one @?@ for-- each parameter, writes a literal question mark as @??@, and does not hold-- @COMMIT@ or @ROLLBACK@; its columns match the row type. Otherwise, or-- when PostgreSQL rejects the query, the migration fails with the adapter's-- error, and the adapter rolls back its transaction.queryRows :: (ToRow q, FromRow r) => Query -> q -> ProjectMigration [r]queryRows sql parameters = ProjectMigration (ReaderT (\tx -> lift (query (transactionConnection tx) sql parameters)))-- | Runs a statement in the migration's transaction, with the parameters in-- place of its @?@ marks. The statement holds one @?@ for each parameter,-- writes a literal question mark as @??@, and is not @COMMIT@ or-- @ROLLBACK@. Otherwise, or when PostgreSQL rejects the statement, the-- migration fails with the adapter's error, and the adapter rolls back its-- transaction.executeRow :: (ToRow q) => Query -> q -> ProjectMigration ()executeRow sql parameters = ProjectMigration (ReaderT (\tx -> lift (void (execute (transactionConnection tx) sql parameters))))-- | Ends the migration with this message. The adapter rolls back the-- migration's transaction, and the run reports the message in-- 'Database.DagMigrations.MigrationFailed'.failMigration :: Text -> ProjectMigration afailMigration = ProjectMigration . lift . throwE-- | Runs a 'ProjectMigration' in a transaction of the PostgreSQL adapter.projectInterpreter :: Interpreter Transaction ProjectMigration TextprojectInterpreter = Interpreter (\tx (ProjectMigration action) -> runExceptT (runReaderT action tx))