# `Arcadic.Migrator`
[🔗](https://github.com/baselabs/arcadic/blob/baselabs_arcadic-v4.0.1/lib/arcadic/migrator.ex#L5)

Runs ordered ArcadeDB migrations and records their state in a durable ledger.

Use an HTTP or gRPC connection outside a transaction. Operations that modify
migration history hold one database-wide lease from ledger preparation
through their final transition.
The lease renews during callbacks. A held lease returns `:migration_locked`.
A lost lease prevents the next guarded statement and ledger transition.

## History and crash recovery

Pass a registry module or a list of migration modules. The `:namespace` option
selects a history stream (default `"default"`). The first operation upgrades
old history into the default stream and creates its compound unique index.
Duplicate versions return `{:error, {:duplicate_migration_versions, versions}}`.

The migrator commits an `up_started` or `down_started` marker before calling
the migration. Only a successful callback finalizes the attempt. An error or
process exit leaves the marker. A process that dies with a command in flight
leaves its lease to expire. `:lease_ms` sets its duration (default 60 seconds).

`migrate/3`, `rollback/3` and `reset/3` replay the recorded direction only for
migrations declaring `c:Arcadic.Migration.schema_only?/0`. For other migrations,
inspect schema and data and record the established outcome with `resolve/3`.
In-flight DDL can still complete after a connection loss. Use the supplied
connection for each synchronous statement, including any recovery checks.

`history/2`, `status/3`, and `interrupted/2` read without acquiring the
lease or creating or upgrading the ledger. `status/3` refuses while a marker
exists in the selected history. Use `history/2` to inspect the recorded state.

# `history`

```elixir
@type history() :: %{floor: non_neg_integer(), entries: [history_entry()]}
```

The inclusive rollback floor (zero if unset) and entries in version order.

# `history_entry`

```elixir
@type history_entry() :: %{
  version: pos_integer(),
  status: :applied | :up_started | :down_started
}
```

A recorded migration and its completed or interrupted state.

# `result`

```elixir
@type result() :: {:ok, non_neg_integer()} | {:error, term()}
```

# `source`

```elixir
@type source() :: module() | [module()]
```

# `baseline`

```elixir
@spec baseline(Arcadic.Conn.t(), source(), pos_integer(), keyword()) :: result()
```

Record a verified history prefix and its inclusive rollback floor in one transaction.

Pass a registered version. `:assert_applied` lists prefix versions whose effects
were confirmed separately. Every migration in the prefix without a schema-only
declaration requires an assertion. Returns the number of newly recorded versions.

The caller is responsible for verifying the live schema. Pass a read-only
`:verify` callback returning `:ok` or `{:error, reason}` to do that under the
same lease before recording the baseline.

# `drop_database`

```elixir
@spec drop_database(Arcadic.Conn.t()) ::
  :ok | {:error, atom() | Arcadic.Error.t() | Arcadic.TransportError.t()}
```

Drop the database in `conn` under its database-wide migration lease.

Supply a session-free, admin-capable HTTP or gRPC connection. Every namespace
must have a zero rollback floor and no interrupted migration. A missing database
returns `:ok`. Unsupported transports return an `Arcadic.Error` with reason
`:not_supported`. Coordinate separately with callers that administer the
database outside the migrator.

# `history`

```elixir
@spec history(Arcadic.Conn.t(), keyword()) ::
  {:ok, history()}
  | {:error,
     Arcadic.Error.t()
     | Arcadic.TransportError.t()
     | {:duplicate_migration_versions, [integer()]}}
```

Read the selected namespace's applied and interrupted history and rollback floor.

Pass `:namespace` to select a stream (defaults to `"default"`). Legacy
rows belong to the default stream. This read takes no lease and neither
creates nor upgrades the ledger.

# `interrupted`

```elixir
@spec interrupted(Arcadic.Conn.t(), keyword()) ::
  {:ok, [pos_integer()]} | {:error, term()}
```

List the namespace's interrupted versions in ascending order.

# `migrate`

```elixir
@spec migrate(Arcadic.Conn.t(), source(), keyword()) :: result()
```

Apply pending migrations in ascending order and return the number applied.

# `pending_migrations`

```elixir
@spec pending_migrations([module()], [integer()]) :: [module()]
```

Return pending modules in ascending version order, without contacting ArcadeDB.

# `preflight_drop_database`

```elixir
@spec preflight_drop_database(Arcadic.Conn.t()) ::
  :ok | {:error, atom() | Arcadic.Error.t() | Arcadic.TransportError.t()}
```

Check whether the migration state permits dropping the database in `conn`.

Any positive rollback floor or interrupted marker in any namespace blocks the
drop. An absent database passes. This read takes no lease and creates no state.
Use `drop_database/1` to repeat the check under the lease and perform the drop.

# `preflight_rollback`

```elixir
@spec preflight_rollback(Arcadic.Conn.t(), source(), pos_integer() | keyword()) ::
  :ok | {:error, term()}
```

Run the reversal checks for the same source and target as `rollback/3`.

Reads the selected history and runs the selected read-only preflight callbacks,
passing through their errors. It takes no lease, writes no history, and runs
neither reversal nor recovery. Rollback prepares the ledger and repeats the
reversal checks under its lease.

# `reset`

```elixir
@spec reset(Arcadic.Conn.t(), source(), keyword()) :: result()
```

Reverse the entire history under one lease, then apply the registered migrations.

# `resolve`

```elixir
@spec resolve(Arcadic.Conn.t(), integer(), :applied | :not_applied, keyword()) ::
  :ok | {:error, term()}
```

Record an interrupted migration's reconciled outcome.

Pass `:applied` if its effects are present or `:not_applied` if they are
absent. Reconcile partial effects before recording either outcome.

# `rollback`

```elixir
@spec rollback(Arcadic.Conn.t(), source(), pos_integer() | keyword()) :: result()
```

Reverse the last `n` applied migrations or reverse all versions above `:to`.

Pass a positive integer for the count (default 1), or keyword options with
`:to` (a nonnegative version) or `:steps` (a positive count). The range is
preflighted newest first before any reversal. The baseline floor is inclusive.

# `status`

```elixir
@spec status(Arcadic.Conn.t(), source(), keyword()) ::
  {:ok, [{pos_integer(), :up | :down}]} | {:error, term()}
```

Return each registered migration's recorded status, ascending by version.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
