Module Migra_engine.RunnerSource

Sourcetype migration_record = {
  1. version : int64;
  2. created_at : string;
}
Sourcetype execution_result =
  1. | Success of Migration.t
  2. | Failure of Migration.t * Types.error
Sourcetype rollback_strategy =
  1. | Step of int
  2. | To of int64
  3. | All

The schema_migrations table

Every operation takes an optional ?table (default default_table) naming the table that tracks applied migrations.

Sourceval default_table : string
Sourceval validate_table_name : string -> (unit, Types.error) result

Validate that a table name is a safe SQL identifier (it is interpolated into queries, not bound as a parameter).

Sourceval ensure_migrations_table : ?table:string -> Dialect.t -> Types.db_conn -> (unit, [> Caqti_error.t ]) Lwt_result.t

Create the migrations table if absent and add the checksum column to pre-existing tables that lack it (dialect-aware). Idempotent.

Sourceval is_applied : ?table:string -> Types.db_conn -> int64 -> (bool, [> Caqti_error.t ]) Lwt_result.t
Sourceval get_applied_versions : ?table:string -> Types.db_conn -> (int64 list, [> Caqti_error.t ]) Lwt_result.t
Sourceval get_applied_records : ?table:string -> Dialect.t -> Types.db_conn -> (migration_record list, [> Caqti_error.t ]) Lwt_result.t

Get all applied migrations with timestamps, sorted chronologically (dialect-aware)

Sourceval get_applied_checksums : ?table:string -> Types.db_conn -> ((int64 * string option) list, [> Caqti_error.t ]) Lwt_result.t

Get all applied (version, checksum) pairs, sorted chronologically. The checksum is None for rows recorded before checksums were tracked.

Sourceval get_latest_version : ?table:string -> Types.db_conn -> (int64 option, [> Caqti_error.t ]) Lwt_result.t

Internal Operations

The following functions are exposed for testing purposes. They should not be used directly in application code.

Sourceval add_migration : ?table:string -> Types.db_conn -> int64 -> string option -> (unit, [> Caqti_error.t ]) Lwt_result.t

Add a migration (mark as applied) with its checksum. Internal - use run_migration.

Sourceval remove_migration : ?table:string -> Types.db_conn -> int64 -> (unit, [> Caqti_error.t ]) Lwt_result.t

Remove a migration record (for rollback). Internal - use rollback_migration instead.

Sourceval is_success : execution_result -> bool
Sourceval migration_of_result : execution_result -> Migration.t
Sourceval error_of_result : execution_result -> Types.error option
Sourceval run_until_failure : step:('a -> 'b Lwt.t) -> is_ok:('b -> bool) -> 'a list -> 'b list Lwt.t

Run step over each item in order, stopping after the first result for which is_ok is false (that failing result is still included). Shared sequential engine; callers supply a step that may add timing or output.

Sourceval pending_migrations : ?table:string -> ?migrations_dir:string -> Types.db_conn -> (Migration.t list, Types.error) Lwt_result.t

All migrations on disk minus those already applied. Fails with OutOfOrder if a pending migration predates the latest applied one.

Sourceval applied_migrations : ?table:string -> ?migrations_dir:string -> Types.db_conn -> (Migration.t list, Types.error) Lwt_result.t

Migrations that are both recorded as applied and present on disk, in chronological order.

Sourceval rollback_targets : ?table:string -> ?migrations_dir:string -> Types.db_conn -> rollback_strategy -> (Migration.t list, Types.error) Lwt_result.t
Sourceval validate : ?table:string -> ?migrations_dir:string -> Types.db_conn -> (unit, Types.error) Lwt_result.t

Validate applied migrations against the files on disk: detects an applied migration whose file is missing (AppliedFileMissing) or whose contents changed since it was applied (ChecksumMismatch). Rows recorded before checksums were tracked are skipped.

Sourceval run_migration : ?verbose:bool -> ?table:string -> Types.db_conn -> Migration.t -> execution_result Lwt.t

Execute a migration's up SQL within a transaction, recording its checksum. Returns Success on success, Failure on error (transaction rolled back).

Sourceval run_migrations : ?verbose:bool -> ?table:string -> Types.db_conn -> Migration.t list -> execution_result list Lwt.t
Sourceval run_pending : ?verbose:bool -> ?table:string -> Types.db_conn -> string -> (execution_result list, Types.error) Lwt_result.t
Sourceval rollback_migration : ?verbose:bool -> ?table:string -> Types.db_conn -> Migration.t -> execution_result Lwt.t

Execute a migration's down SQL (rollback) within a transaction. On failure, transaction is rolled back automatically.

Sourceval rollback_step : ?verbose:bool -> ?table:string -> ?migrations_dir:string -> Types.db_conn -> int -> (execution_result list, Types.error) Lwt_result.t
Sourceval rollback_to : ?verbose:bool -> ?table:string -> ?migrations_dir:string -> Types.db_conn -> int64 -> (execution_result list, Types.error) Lwt_result.t
Sourceval rollback_all : ?verbose:bool -> ?table:string -> ?migrations_dir:string -> Types.db_conn -> (execution_result list, Types.error) Lwt_result.t