EctoEvolver

Versioned database migrations for Elixir libraries using Ecto.

EctoEvolver provides infrastructure for library authors to ship versioned database schemas that support incremental upgrades. Inspired by how Oban handles migrations.

EctoEvolver Lab

Philosophy

EctoEvolver uses raw SQL instead of Ecto's schema DSL. This enables:

Adapters

EctoEvolver uses an adapter system for database-specific operations. The adapter is auto-detected from your Ecto repo's configuration at runtime.

Currently supported:

Future adapters: The adapter architecture is designed to support other databases that Ecto supports (SQLite, MySQL, etc.). Contributions welcome.

Installation

def deps do
  [
    {:ecto_evolver, "~> 0.1.0"}
  ]
end

Usage

1. Define a Migration Module

defmodule MyLibrary.Migration do
  use EctoEvolver,
    otp_app: :my_library,
    default_prefix: "my_library",
    versions: [MyLibrary.Migrations.V01],
    tracking_object: {:table, "my_main_table"}
end

2. Define Version Modules

defmodule MyLibrary.Migrations.V01 do
  use EctoEvolver.Version,
    otp_app: :my_library,
    version: "01",
    sql_path: "my_library/sql/versions"
end

3. Create SQL Files

Place SQL files in your priv/ directory:

priv/my_library/sql/versions/
└── v01/
    ├── v01_up.sql
    └── v01_down.sql

Use $SCHEMA$ as a placeholder for the schema name and --SPLIT-- to separate statements:

CREATE SCHEMA IF NOT EXISTS $SCHEMA$;

--SPLIT--

CREATE TABLE $SCHEMA$.my_table (
  id BIGSERIAL PRIMARY KEY,
  name TEXT NOT NULL
);

4. Users Generate an Ecto Migration

defmodule MyApp.Repo.Migrations.AddMyLibrary do
  use Ecto.Migration

  def up, do: MyLibrary.Migration.up()
  def down, do: MyLibrary.Migration.down()
end

Options

use EctoEvolver

use EctoEvolver.Version

Version Tracking

Version tracking is adapter-specific. The PostgreSQL adapter uses object comments:

COMMENT ON TABLE schema.my_table IS 'MyLibrary version=1';

This allows EctoEvolver to detect the current version and apply only necessary migrations during upgrades.

License

MIT