lode/migration

A migration: a versioned, reversible set of schema changes.

Build one with new(version) then pipe operations. Each operation records both its forward (up) form and an automatically-derived reverse (down) form, so a single definition migrates and rolls back — like Ecto’s change. For irreversible or custom steps use execute(up_sql, down_sql).

migration.new(20_260_604_120_000) |> migration.create_table(“users”, [ ddl.column(“id”, ddl.Serial) |> ddl.primary_key, ddl.column(“name”, ddl.Text) |> ddl.not_null, ddl.column(“age”, ddl.Integer) |> ddl.not_null, ]) |> migration.create_index(“users”, [“name”], unique: True)

Types

pub opaque type Migration

Values

pub fn add_column(
  migration m: Migration,
  table table: String,
  column col: ddl.Column,
) -> Migration

Add a column; reverses to DROP COLUMN.

pub fn create_index(
  migration m: Migration,
  table table: String,
  columns columns: List(String),
  unique unique: Bool,
) -> Migration

Create an index; reverses to DROP INDEX. The index name is derived from the table and columns.

pub fn create_schema(
  migration m: Migration,
  name name: String,
) -> Migration

Create a PostgreSQL schema/namespace (for prefix-based multi-tenancy); reverses to DROP SCHEMA ... CASCADE.

pub fn create_table(
  migration m: Migration,
  name name: String,
  columns columns: List(ddl.Column),
) -> Migration

Create a table; reverses to DROP TABLE.

pub fn down_sql(
  m: Migration,
  dialect dialect: ddl.Dialect,
) -> List(String)

The reverse SQL statements, in order (already reversed relative to up), in the given dialect.

pub fn drop_schema(
  migration m: Migration,
  name name: String,
) -> Migration

Drop a PostgreSQL schema (DROP SCHEMA ... CASCADE). Reverses to a bare CREATE SCHEMA (its former contents are not restored).

pub fn drop_table(
  migration m: Migration,
  name name: String,
) -> Migration

Drop a table. Not auto-reversible; down is a no-op (override with execute if you need to recreate it).

pub fn execute(
  migration m: Migration,
  up up_sql: String,
  down down_sql: String,
) -> Migration

A raw SQL step with an explicit reverse.

pub fn new(version: Int) -> Migration

Start a migration with a unique, orderable version (e.g. a UTC timestamp like 20_260_604_120_000).

pub fn up_sql(
  m: Migration,
  dialect dialect: ddl.Dialect,
) -> List(String)

The forward SQL statements, in order, in the given dialect.

pub fn version(m: Migration) -> Int
Search Document