AshClickhouse.MigrationRunner (AshClickhouse v0.6.2)

Copy Markdown View Source

Runs generated ClickHouse migration files for a repo.

Migrations are version-tracked in a schema_migrations table so each file is applied at most once. The same versioned pipeline backs both mix ash_clickhouse.migrate (run/2, output via Mix.shell()) and AshClickhouse.Release.migrate/3 (which passes logger: true so output goes through Logger instead — Mix is unavailable inside a release).

Migration modules implement AshClickhouse.Schema. The version comes from version/0 when exported, falling back to the leading timestamp in the filename (e.g. 20240101120000_add_users.exs) so files generated before version/0 support still work.

Summary

Functions

Returns the versions already applied for a repo, sorted.

Removes a migration version from the tracking table (rollback).

Loads and sorts the migration files under a path.

Creates the version-tracking table for a repo if it doesn't exist.

Runs pending migrations for a repo and returns a summary.

Records a migration version as applied.

Rolls back applied migrations for a repo down to (but not including) a target version.

Runs pending migrations for a repo, Mix-style.

DDL used to create the version-tracking table.

The name of the version-tracking table.

Functions

applied_versions(repo)

@spec applied_versions(module()) :: [String.t()]

Returns the versions already applied for a repo, sorted.

delete_applied(repo, version)

@spec delete_applied(module(), String.t()) :: :ok | {:error, term()}

Removes a migration version from the tracking table (rollback).

discover_migrations(path)

@spec discover_migrations(String.t()) :: [%{version: String.t(), module: module()}]

Loads and sorts the migration files under a path.

Returns [%{version: String.t(), module: module()}] sorted by version, including only files that define at least one module. Modules whose version cannot be determined still appear (their version is the full filename).

ensure_schema_migrations_table(repo)

@spec ensure_schema_migrations_table(module()) :: :ok | {:error, term()}

Creates the version-tracking table for a repo if it doesn't exist.

migrate(repo, opts \\ [])

@spec migrate(
  module(),
  keyword()
) :: {:ok, map()} | {:error, term()}

Runs pending migrations for a repo and returns a summary.

Creates the schema_migrations table if needed, skips versions already applied, executes change/0 for the rest, and records each applied version.

Options

  • :migration_path — directory containing *.exs migrations (default "priv/repo/migrations")
  • :dry_run — log statements without executing or recording
  • :logger — emit output via Logger instead of Mix.shell() (for AshClickhouse.Release)

Returns {:ok, %{applied: [module()], skipped: [module()]}} or {:error, term()} — a {module(), term()} tuple when a specific migration failed, or a bare term() when the tracking table could not be created.

record_applied(repo, version)

@spec record_applied(module(), String.t()) :: :ok | {:error, term()}

Records a migration version as applied.

rollback(repo, target, opts \\ [])

@spec rollback(module(), String.t() | non_neg_integer() | :all | nil, keyword()) ::
  {:ok, map()} | {:error, term()}

Rolls back applied migrations for a repo down to (but not including) a target version.

Each rolled-back migration executes down/0 when exported, falling back to AshClickhouse.Migration.reverse_statement/1 over change/0 for files that predate down/0 support. Statements that cannot be reversed are skipped with a warning. :all (or nil/0) rolls back every applied migration.

Returns {:ok, %{rolled_back: [module()], skipped: [module()]}} or {:error, term()} — a {module(), term()} tuple when a specific migration failed, or a bare term() when the tracking table could not be created.

run(repo, opts \\ [])

@spec run(
  module(),
  keyword()
) :: :ok

Runs pending migrations for a repo, Mix-style.

Equivalent to migrate/2 but prints through Mix.shell() and raises on failure, matching the behaviour of mix ash_clickhouse.migrate.

schema_migrations_create_sql()

@spec schema_migrations_create_sql() :: String.t()

DDL used to create the version-tracking table.

schema_migrations_table()

@spec schema_migrations_table() :: String.t()

The name of the version-tracking table.