EctoImmigrant.Migrator (ecto_immigrant v0.3.0)

This module provides the data migration API.

Example

defmodule MyApp.MigrationExample do
  use EctoImmigrant.Migration
  alias MyApp.Repo
  alias MyApp.Person

  def up do
    Repo.insert(%Person{id: 123, first_name: "John", last_name: "Doe", age: 78})
  end

  def down do
    Repo.delete(%Person{id: 123, first_name: "John", last_name: "Doe", age: 78})
  end

end

EctoImmigrant.Migrator.up(Repo, 20080906120000, MyApp.MigrationExample)
EctoImmigrant.Migrator.down(Repo, 20080906120000, MyApp.MigrationExample)

Link to this section Summary

Functions

Reverts an applied data migration on the given repository.

Gets all migrated versions.

Returns an array of tuples as the migration status of the given repo, without actually running any migrations.

Apply migrations to a repository with a given strategy.

Runs an up data migration on the given repository.

Link to this section Functions

Link to this function

down(repo, version, module, opts \\ [])

Reverts an applied data migration on the given repository.

Link to this function

migrated_versions(repo, opts \\ [])

Specs

migrated_versions(Ecto.Repo.t(), Keyword.t()) :: [integer()]

Gets all migrated versions.

This function ensures the data migration table exists if no table has been defined yet.

Options

  • :log - the level to use for logging. Defaults to :info. Can be any of Logger.level/0 values or false.
  • :prefix - the prefix to run the migrations on
Link to this function

migrations(repo, directory)

Returns an array of tuples as the migration status of the given repo, without actually running any migrations.

Link to this function

run(repo, migration_source, direction, opts)

Specs

run(Ecto.Repo.t(), binary() | [{integer(), module()}], atom(), Keyword.t()) :: [
  integer()
]

Apply migrations to a repository with a given strategy.

The second argument identifies where the migrations are sourced from. A file path may be passed, in which case the migrations will be loaded from this during the migration process. The other option is to pass a list of tuples that identify the version number and migration modules to be run, for example:

EctoImmigrant.Migrator.run(Repo, [{0, MyApp.Migration1}, {1, MyApp.Migration2}, ...], :up, opts)

A strategy must be given as an option.

Options

  • :all - runs all available if true
  • :log - the level to use for logging. Defaults to :info. Can be any of Logger.level/0 values or false.
  • :prefix - the prefix to run the migrations on
Link to this function

up(repo, version, module, opts \\ [])

Specs

up(Ecto.Repo.t(), integer(), module(), Keyword.t()) ::
  :ok | :already_up | no_return()
up(Ecto.Repo.t(), integer(), module(), Keyword.t()) :: :ok | :already_down

Runs an up data migration on the given repository.

Options

  • :log - the level to use for logging. Defaults to :info. Can be any of Logger.level/0 values or false.
  • :prefix - the prefix to run the migrations on