Cardbox.Migrations (Cardbox v0.1.0)

Copy Markdown View Source

Cardbox's tables, in versions, for the migrations of your app.

$ mix cardbox.gen.migration

writes a migration that pins the latest version:

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

  def up, do: Cardbox.Migrations.up(version: 1)
  def down, do: Cardbox.Migrations.down(to: 0)
end

A later Cardbox that changes the tables has a new version, and mix cardbox.gen.migration writes the migration that takes them there, from the version your migrations already pin:

def up, do: Cardbox.Migrations.up(version: 2)
def down, do: Cardbox.Migrations.down(to: 1)

So a migration, once written, does the same thing whichever Cardbox runs it: your history is the same for a new developer, in CI and in production. The version a database is at is kept in a table of one row, cardbox_schema, and up/1 runs only the versions after it, so the migrations of an app that made the tables another way run once.

The versions run on PostgreSQL and on SQLite.

Versions

  1. The five tables: cardbox_collections, cardbox_fields, cardbox_records (the values a JSON map, with a GIN index and a trigram index of the search text on PostgreSQL), cardbox_links and cardbox_views.

Summary

Functions

:ok when the tables of a repo are at the latest version, else an error that says which migration to write: for a release check, or a health check of your app.

Takes the tables back down to :to (0, no tables, unless given). Call it in a migration.

The latest version of Cardbox's tables.

The version the tables of a repo are at: 0 before Cardbox's migration has run.

Takes the tables up to :version (the latest unless given: pin it, as mix cardbox.gen.migration does). Call it in a migration.

Functions

check(repo)

@spec check(Ecto.Repo.t()) :: :ok | {:error, String.t()}

:ok when the tables of a repo are at the latest version, else an error that says which migration to write: for a release check, or a health check of your app.

down(opts \\ [])

@spec down(keyword()) :: :ok

Takes the tables back down to :to (0, no tables, unless given). Call it in a migration.

latest_version()

@spec latest_version() :: pos_integer()

The latest version of Cardbox's tables.

migrated_version(repo)

@spec migrated_version(Ecto.Repo.t()) :: non_neg_integer()

The version the tables of a repo are at: 0 before Cardbox's migration has run.

up(opts \\ [])

@spec up(keyword()) :: :ok

Takes the tables up to :version (the latest unless given: pin it, as mix cardbox.gen.migration does). Call it in a migration.