View Source ErrorTracker.Migration behaviour (ErrorTracker v0.1.2)
Create and modify the database tables for ErrorTracker.
Usage
To use ErrorTracker migrations in your application you will need to generate
a regular Ecto.Migration
that performs the relevant calls to ErrorTracker.Migration
.
mix ecto.gen.migration add_error_tracker
Open the generated migration file and call the up
and down
functions on
ErrorTracker.Migration
.
defmodule MyApp.Repo.Migrations.AddErrorTracker do
use Ecto.Migration
def up, do: ErrorTracker.Migration.up()
def down, do: ErrorTracker.Migration.down()
end
This will run every ErrorTracker migration for your database. You can now run the migration and perform the database changes:
mix ecto.migrate
As new versions of ErrorTracker are released you may need to run additional migrations. To do this you can follow the previous process and create a new migration:
mix ecto.gen.migration update_error_tracker_to_vN
Open the generated migration file and call the up
and down
functions on the
ErrorTracker.Migration
passing the desired version
.
defmodule MyApp.Repo.Migrations.UpdateErrorTrackerToVN do
use Ecto.Migration
def up, do: ErrorTracker.Migration.up(version: N)
def down, do: ErrorTracker.Migration.down(version: N)
end
Then run the migrations to perform the database changes:
mix ecto.migrate
Custom prefix
ErrorTracker supports namespacing its own tables using PostgreSQL schemas, also known as "prefixes" in Ecto. With prefixes your error tables can reside outside of your primary schema (which is usually named "public").
To use a prefix you need to specify it in your migrations:
defmodule MyApp.Repo.Migrations.AddErrorTracker do
use Ecto.Migration
def up, do: ErrorTracker.Migration.up(prefix: "custom_schema")
def down, do: ErrorTracker.Migration.down(prefix: "custom_schema")
end
This will automatically create the database schema for you. If the schema does already exist the migration may fail when trying to recreate it. In such cases you can instruct ErrorTracker not to create the schema again:
defmodule MyApp.Repo.Migrations.AddErrorTracker do
use Ecto.Migration
def up, do: ErrorTracker.Migration.up(prefix: "custom_schema", create_schema: false)
def down, do: ErrorTracker.Migration.down(prefix: "custom_schema")
end
If you are using a custom schema other than the default "public" you need to configure ErrorTracker to use that schema:
config :error_tracker, :prefix, "custom_schema"
Summary
Callbacks
@callback current_version(Keyword.t()) :: non_neg_integer()
@callback down(Keyword.t()) :: :ok
@callback up(Keyword.t()) :: :ok