postgresql-migration-persistent: A PostgreSQL persistent schema migration utility

[ database, library, mit ] [ Propose Tags ] [ Report a vulnerability ]

Wraps postgresql migration and persistent to make sure the persistent schema aligns with what's in the database. If not, it returns a list of suggested manual migrations to be put in postgresql-migration.


[Skip to Readme]

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 1.0.0, 1.1.0
Change log Changelog.md
Dependencies base (>=4.9.1.0 && <5), mtl (<3), persistent (<3), persistent-postgresql (<3), postgresql-migration (<0.3), postgresql-simple (<1), resource-pool (<0.5), text (<3) [details]
License MIT
Copyright 2025 Jappie Klooster
Author Jappie Klooster, Jean-Paul Calderone
Maintainer hi@jappie.me
Category Database
Home page https://github.com/jappeace/postgresql-migration-persistent#readme
Bug tracker https://github.com/jappeace/postgresql-migration-persistent/issues
Source repo head: git clone https://github.com/jappeace/postgresql-migration-persistent
Uploaded by Jappie at 2025-05-20T22:59:02Z
Distributions
Downloads 4 total (4 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user [build log]
All reported builds failed as of 2025-05-20 [all 2 reports]

Readme for postgresql-migration-persistent-1.1.0

[back to package description]

Don't you ever trust computers.

- first rule of programist bible.

This library combines postgresql-migration and persistent, for the common use case of:

  1. Run my manually defined migrations.
  2. Check if the schema defined in persistent aligns with the database.
  3. If not, rollback and error with the migration plan persistent wants to do.

Usage

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.

Tools

Enter the nix shell.

nix develop

You can checkout the makefile to see what's available:

cat makefile

Running

make run

Fast filewatch which runs tests

make ghcid