Skip to content

A library's migrations

A library that owns a table ships the migrations that make it, in its own migrations/, installed with its package. Name it with --source and its migrations run first, as a history of their own:

Terminal window
rowtype-migrate up --source spool # the library's, then yours
rowtype-migrate up --source spool=vendor/sql # a library's folder, named

Each source is ordered and checked only among its own, so upgrading a library whose migration is older than your last one applies it rather than refusing it as late. The record says whose each migration is: yours are app. status, dump and squash take --source too, and squash leaves a library’s tables out of your baseline, since its own migrations make them. A squash is refused for a library that made what a baseline could not tell from yours: on Postgres anything but tables – a type, a function, a view – and on SQLite a view, or a trigger on what it did not make.

Repeat --source for each library, in the order they run. Libraries run first, in the order given, and your migrations last, since they may stand on the libraries’ tables.

A package installs its migrations in share/<package>/migrations, where --source <package> finds them under the switch’s prefix. In the library’s dune:

(install
(section share)
(package spool)
(files
(glob_files
(migrations/*.sql with_prefix migrations))))

Rowtype_migrate.of_package ~prefix package reads a package’s migrations, and Rowtype_migrate.source names them, for apply’s ~libraries:

migrate_with_libraries.ml
let migrate env ~prefix target =
let ( let* ) = Result.bind in
let* spool = Rowtype_migrate.of_package ~prefix "spool" in
let* spool = Rowtype_migrate.source "spool" spool in
let* migrations = Rowtype_migrate.of_directory "migrations" in
Rowtype_migrate_postgres.run env target @@ fun db ->
Rowtype_migrate_postgres.apply ~libraries:[ spool ] db migrations

A source’s name is lower-case letters, digits, hyphens and underscores, starting with a letter, as a package’s is; app is the application’s, and refused.