Run and recover migrations

Copy Markdown View Source

Use Arcadic.Migrator to apply versioned changes, reverse a range, or adopt a verified schema. Connect to the target database over HTTP or gRPC outside an existing transaction.

Upgrade an existing history

Stop every older Arcadic migrator for the database before the first migration write with the new version. Older migrators neither acquire the lease nor write a namespace. The new migrator backfills legacy rows into the default stream and creates a unique index that requires both namespace and version. Resolve any reported duplicate ledger versions before retrying. Keep every applied version registered when running a reset.

Use Arcadic.Migrator.history/2 to inspect the applied and interrupted entries and the rollback floor for a namespace. Legacy rows appear in the default namespace. The read leaves the ledger and lease untouched.

Apply a history

Define a migration for each version. Use the supplied connection for synchronous queries and commands with eager results. Declare a migration schema-only if both directions can safely replay after any partial attempt. Keep commands on that database and use a separate operator connection for server administration. If renewal cannot be confirmed before the lease deadline, the migration connection refuses further statements. Statements already sent may still finish.

defmodule MyApp.Migrations.V1 do
  @behaviour Arcadic.Migration

  @impl true
  def version, do: 1

  @impl true
  def schema_only?, do: true

  @impl true
  def up(conn) do
    Arcadic.command!(conn, "CREATE VERTEX TYPE User IF NOT EXISTS", %{}, language: "sql")
    :ok
  end

  @impl true
  def down(conn) do
    Arcadic.command!(conn, "DROP TYPE User IF EXISTS", %{}, language: "sql")
    :ok
  end
end

{:ok, 1} = Arcadic.Migrator.migrate(conn, [MyApp.Migrations.V1])

A registry module also works. In a release, ship trusted .exs files in priv and load them with Arcadic.MigrationSource.load/1.

dir = Path.join(to_string(:code.priv_dir(:my_app)), "migrations")
{:ok, modules} = Arcadic.MigrationSource.load(dir)
{:ok, _} = Arcadic.Migrator.migrate(conn, modules, namespace: "accounts")

Use the same namespace for subsequent operations on that history. The database-wide lease serializes operations across namespaces. See Arcadic.Migrator for lease, ledger upgrade, and status behavior.

Roll back or adopt a schema

Use rollback/3 with to: version to reverse newer migrations. Use to: 0 to reverse the whole unbaselined history. Implement Arcadic.Migration.preflight_down/1 for any reversal that depends on existing data or schema. All selected preflights run before any reversal. For schema-only migrations, account for schema objects the interrupted attempt may already have removed.

Use Arcadic.Migrator.preflight_rollback/3 to check a rollback target or step count before applying it. It reads the selected namespace and runs the same reversal checks as rollback. Rollback repeats those checks under the lease before changing the schema or data.

To adopt an existing schema, call Arcadic.Migrator.baseline/4 with the version that matches it. Run your live schema check in the :verify callback so the check and recording use the same lease. Verify any data effects separately and pass their versions in :assert_applied. The resulting floor prevents reversing the adopted history.

Drop a migration-managed database

Use an admin-capable HTTP or gRPC connection scoped to the database. Arcadic.Migrator.preflight_drop_database/1 checks every migration namespace without taking the lease. A positive rollback floor or an interrupted migration in any namespace blocks the drop.

Arcadic.Migrator.drop_database/1 takes the database-wide migration lease, checks those conditions again, and drops the database. It returns :ok if the database is already absent.

Recover an interrupted attempt

ArcadeDB preserves some effects, including DDL, across a failed or rolled-back session. For a schema-only version the migrator replays the marked direction. For other versions, use Arcadic.Migrator.interrupted/1 to find the marker. Inspect the actual schema and data and reconcile any partial effects. Record :applied if the migration's effects are present, or :not_applied if absent.

{:ok, [version]} = Arcadic.Migrator.interrupted(conn)
:ok = Arcadic.Migrator.resolve(conn, version, :applied)