Don't you ever trust computers.
- first rule of programist bible.
This library combines postgresql-migration
and persistent
,
for the common use case of:
- Run my manually defined migrations.
- Check if the schema defined in persistent aligns with the database.
- If not, rollback and error with the migration plan persistent wants to do.
It could for example look something like this with katip logging:
import PostgreSQL.Migration.Persistent
import Database.Schema.User()
import Database.Schema.Company()
migrateAll :: Migration
migrateAll = migrateModels $(discoverEntities)
main :: IO ()
main = do
katipConfig <- mkKatipConfig "server" environment
let migDir = "migrations/up"
let migrationOptions = defaultOptions migDir $ \case
Left errmsg -> runContext katipConfig $ $logTM AlertS $ logStr errmsg
Right infoMsg -> runContext katipConfig $ $logTM InfoS $ logStr infoMsg
result <- runMigrations migrationOptions migrateAll rawPool
runContext katipConfig $ case result of
MigrationConsistent -> $logTM InfoS "migration consistent"
MigrationRollbackDueTo rollback -> do
$logTM ErrorS $ logStr $ errorMessage rollback
error "invalid migrations"
MigrationLibraryError err' -> do
$logTM ErrorS $ logStr err'
error "migration library error"
MigrationNotBackedByPg ->
error "app expects pg backing for migrations to work"
runMyApp
By default the migrations are applied in a large transaction, but you can modify this behavior by overriding options record, for example:
import Database.PostgreSQL.Simple.Migration qualified as Migration
main = do
...
let migrationOptions = defaultOptions migDir $ \case
Left errmsg -> runContext katipConfig $ $logTM AlertS $ logStr errmsg
Right infoMsg -> runContext katipConfig $ $logTM InfoS $ logStr infoMsg
let overridenOptions = migrationOptions { pmoMigrationOptions = (pmoMigrationOptions migrationOptions ) {Migration.optTransactionControl = Migration.TransactionPerStep }}
pretty much all options are exposed from the underlying postgresql-migration library.
Enter the nix shell.
nix develop
You can checkout the makefile to see what's available:
cat makefile
make run
make ghcid