gleager/types

Types used by the Gleager migration engine.

The central type is Driver(e), which abstracts over database backends so that gleager can work with any Gleam SQL library. The e type parameter represents the database library’s native error type, which gets mapped to MigrationError by gleager’s internal functions.

Types

A database driver for executing SQL and running parameterised queries.

The e type parameter is the database library’s native error type. gleager maps these errors to MigrationError internally, so the driver does not need to perform any error conversion.

Example — sqlight driver

let driver = Driver(
  migration_dir: "migrations",
  exec: fn(sql, args) {
    case args {
      [] -> sqlight.exec(sql, on: conn)
      _ -> {
        let values = list.map(args, sqlight.text)
        case sqlight.query(sql, on: conn, with: values, expecting: decode.int) {
          Ok(_) -> Ok(Nil)
          Error(e) -> Error(e)
        }
      }
    }
  },
  query: fn(sql, args, decoder) {
    let values = list.map(args, sqlight.text)
    sqlight.query(sql, on: conn, with: values, expecting: decoder)
  },
)
pub type Driver(e) {
  Driver(
    migration_dir: String,
    exec: fn(String, List(String)) -> Result(Nil, e),
    query: fn(String, List(String), decode.Decoder(MigrationRow)) -> Result(
      List(MigrationRow),
      e,
    ),
  )
}

Constructors

  • Driver(
      migration_dir: String,
      exec: fn(String, List(String)) -> Result(Nil, e),
      query: fn(String, List(String), decode.Decoder(MigrationRow)) -> Result(
        List(MigrationRow),
        e,
      ),
    )

    Arguments

    migration_dir

    The filesystem path to the directory containing migration files.

    exec

    Execute one or more SQL statements, optionally with parameters. When called with an empty args list, run the SQL directly. When called with parameters, bind them to the ? placeholders.

    query

    Run a parameterised query and decode the result rows.

A parsed migration consisting of its version and the up and down SQL.

pub type Migration {
  Migration(version: String, up: String, down: String)
}

Constructors

  • Migration(version: String, up: String, down: String)

Errors that can occur during migration operations.

pub type MigrationError {
  FileNotFound(at: String)
  UnableToRead(at: String)
  InvalidMigrationVersion(at: String)
  SchemaTableFailed
  SelectMigrationsFailed
  InsertMigrationFailed(for: String)
  DeleteMigrationFailed(for: String)
  MigrationStatementFailed(for: String)
  UpMigrationFailed(for: String)
  DownMigrationFailed(for: String)
}

Constructors

  • FileNotFound(at: String)

    The migration directory was not found at the given path.

  • UnableToRead(at: String)

    A migration file exists but could not be read.

  • InvalidMigrationVersion(at: String)

    A migration filename does not have a valid version prefix.

  • SchemaTableFailed

    The schema_migrations tracking table could not be created.

  • SelectMigrationsFailed

    The query to list applied migrations failed.

  • InsertMigrationFailed(for: String)

    Recording a migration in the tracking table failed.

  • DeleteMigrationFailed(for: String)

    Removing a migration from the tracking table failed.

  • MigrationStatementFailed(for: String)

    One of the SQL statements in a migration could not be executed.

  • UpMigrationFailed(for: String)

    An up migration failed to apply.

  • DownMigrationFailed(for: String)

    A down migration failed to apply.

Operations that can be performed on migrations.

pub type MigrationOp {
  Up
  Down
  Generate(name: String)
}

Constructors

  • Up

    Apply pending migrations.

  • Down

    Roll back applied migrations.

  • Generate(name: String)

    Generate a new migration file with the given name.

A row from the schema_migrations tracking table.

pub type MigrationRow {
  MigrationRow(version: String)
}

Constructors

  • MigrationRow(version: String)
Search Document