Repodb.Migration
repodb · API reference
type default_expr =
| Raw of string
| Current_timestamp
| Null
| Bool of bool
| Int of int
| Int64 of int64
| Float of float
| Text of stringDeclarative database migrations and migration planning.
A migration has a monotonically increasing version, a human-readable name, and lists of up and down operations. Repodb can render those operations for PostgreSQL, SQLite, MySQL/MariaDB, or the generic dialect used by tests.
Example:
let create_users =
Migration.migration ~version:20260101120000L ~name:"create_users"
~up:
[ Migration.create_table "users"
[ Migration.typed_column "id" Types.int64
~primary_key:true ~nullable:false;
Migration.typed_column "email" Types.string
~nullable:false ~unique:true;
Migration.typed_column "inserted_at" Types.ptime
~nullable:false
~default_expr:Migration.Current_timestamp
] ]
~down:[ Migration.drop_table "users" ]Use typed_column when the column maps to a Types.t; use column with an explicit SQL type for backend-specific types. Prefer structured default expressions over Raw when possible.
Planning functions compare available migrations with already-applied versions and return ordered actions. This separates migration planning from execution so CLIs can print dry-runs, apply targets, or rollback a fixed number of steps before touching the database.
type column_type =
| RawType of string
| Typed : 'a Types.t -> column_typetype fk_action =
| Cascade
| Restrict
| Set_null
| Set_default
| No_actiontype column_def = {
col_name : string;
col_type : column_type;
col_nullable : bool;
col_primary_key : bool;
col_unique : bool;
col_default : default_expr option;
col_references : (string * string) option;
col_on_delete : fk_action option;
col_on_update : fk_action option;
}type index_def = {
idx_name : string option;
idx_table : string;
idx_columns : string list;
idx_unique : bool;
}type operation =
| Create_table of {
name : string;
columns : column_def list;
}
| Drop_table of string
| Alter_table of {
name : string;
changes : alter_change list;
}
| Create_index of index_def
| Drop_index of string
| Execute of stringand alter_change =
| Add_column of column_def
| Drop_column of string
| Rename_column of {
from : string;
to_ : string;
}
| Alter_column of {
name : string;
new_type : string option;
new_nullable : bool option;
}type t = {
version : int64;
name : string;
up : operation list;
down : operation list;
}val default_expr_of_args :
default_expr:default_expr option ->
default:string option ->
default_expr optionval make_column :
?nullable:bool ->
?primary_key:bool ->
?unique:bool ->
?default:string ->
?default_expr:default_expr ->
?references:(string * string) ->
?on_delete:fk_action ->
?on_update:fk_action ->
col_type:column_type ->
string ->
column_defval column :
?nullable:bool ->
?primary_key:bool ->
?unique:bool ->
?default:string ->
?default_expr:default_expr ->
?references:(string * string) ->
?on_delete:fk_action ->
?on_update:fk_action ->
string ->
string ->
column_defval typed_column :
?nullable:bool ->
?primary_key:bool ->
?unique:bool ->
?default:string ->
?default_expr:default_expr ->
?references:(string * string) ->
?on_delete:fk_action ->
?on_update:fk_action ->
string ->
'a Types.t ->
column_defval create_table : string -> column_def list -> operationval drop_table : string -> operationval add_column : column_def -> alter_changeval drop_column : string -> alter_changeval rename_column : from:string -> to_:string -> alter_changeval alter_table : string -> alter_change list -> operationval create_index :
?unique:bool ->
?name:string ->
string ->
string list ->
operationval drop_index : string -> operationval execute : string -> operationval timestamps : unit -> column_def listval migration :
version:int64 ->
name:string ->
up:operation list ->
down:operation list ->
tval render_default_expr : dialect:Driver.dialect -> default_expr -> stringval column_type_to_sql : dialect:Driver.dialect -> column_type -> stringval fk_action_to_sql : fk_action -> stringval column_to_sql_dialect : dialect:Driver.dialect -> column_def -> stringval column_to_sql : column_def -> stringval operation_to_sql_dialect : dialect:Driver.dialect -> operation -> stringval operation_to_sql : operation -> stringval schema_migrations_table : operationval create_schema_migrations_sql_dialect : dialect:Driver.dialect -> stringval create_schema_migrations_sql : stringval placeholder_for_dialect : Driver.dialect -> int -> stringval insert_migration_sql_dialect : dialect:Driver.dialect -> stringval insert_migration_sql : stringval delete_migration_sql_dialect : dialect:Driver.dialect -> stringval delete_migration_sql : stringval get_applied_versions_sql : stringval get_migration_status_sql : stringtype migration_status = {
applied_versions : int64 list;
pending : t list;
last_applied : int64 option;
}val sort_migrations : t list -> t listval pending_migrations : applied_versions:int64 list -> t list -> t listval generate_up_sql_dialect : dialect:Driver.dialect -> t -> string listval generate_down_sql_dialect : dialect:Driver.dialect -> t -> string listval generate_up_sql : t -> string listval generate_down_sql : t -> string listtype migration_action =
| Migrate of {
sql : string list;
version : int64;
name : string;
}
| Rollback of {
sql : string list;
version : int64;
}
| CreateSchemaTable of string
| RecordMigration of {
version : int64;
name : string;
}
| RemoveMigration of int64val plan_migrate_dialect :
dialect:Driver.dialect ->
applied_versions:int64 list ->
target:int64 option ->
t list ->
migration_action listval plan_migrate :
applied_versions:int64 list ->
target:int64 option ->
t list ->
migration_action listval plan_rollback_dialect :
dialect:Driver.dialect ->
applied_versions:Int64.t list ->
step:int option ->
t list ->
migration_action listval plan_rollback :
applied_versions:Int64.t list ->
step:int option ->
t list ->
migration_action listval action_to_sql : migration_action -> string listval actions_to_sql : migration_action list -> string listval format_status : applied_versions:int64 list -> migrations:t list -> string