# `Cardbox.Migrations`
[🔗](https://github.com/hattoriai/cardbox/blob/v0.1.0/lib/cardbox/migrations.ex#L1)

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`.

# `check`

```elixir
@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`

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

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

# `latest_version`

```elixir
@spec latest_version() :: pos_integer()
```

The latest version of Cardbox's tables.

# `migrated_version`

```elixir
@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`

```elixir
@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.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
