|
| 1 | +defmodule ErrorTracker.Migrations do |
| 2 | + @moduledoc """ |
| 3 | + Create and modify the database tables for the ErrorTracker. |
| 4 | +
|
| 5 | + ## Usage |
| 6 | +
|
| 7 | + To use the ErrorTracker migrations in your application you will need to generate |
| 8 | + a regular `Ecto.Migration` that performs the relevant calls to `ErrorTracker.Migration`. |
| 9 | +
|
| 10 | + ```bash |
| 11 | + mix ecto.gen.migration add_error_tracker |
| 12 | + ``` |
| 13 | +
|
| 14 | + Open the generated migration file and call the `up` and `down` functions on |
| 15 | + `ErrorTracker.Migration`. |
| 16 | +
|
| 17 | + ```elixir |
| 18 | + defmodule MyApp.Repo.Migrations.AddErrorTracker do |
| 19 | + use Ecto.Migration |
| 20 | +
|
| 21 | + def up, do: ErrorTracker.Migrations.up() |
| 22 | + def down, do: ErrorTracker.Migrations.down() |
| 23 | + end |
| 24 | + ``` |
| 25 | +
|
| 26 | + This will run every ErrorTracker migration for your database. You can now run the migration |
| 27 | + and perform the database changes: |
| 28 | +
|
| 29 | + ```bash |
| 30 | + mix ecto.migrate |
| 31 | + ``` |
| 32 | +
|
| 33 | + As new versions of the ErrorTracker are released you may need to run additional migrations. |
| 34 | + To do this you can follow the previous process and create a new migration: |
| 35 | +
|
| 36 | + ```bash |
| 37 | + mix ecto.gen.migration update_error_tracker_to_vN |
| 38 | + ``` |
| 39 | +
|
| 40 | + Open the generated migration file and call the `up` and `down` functions on the |
| 41 | + `ErrorTracker.Migration` passing the desired `version`. |
| 42 | +
|
| 43 | + ```elixir |
| 44 | + defmodule MyApp.Repo.Migrations.UpdateErrorTrackerToVN do |
| 45 | + use Ecto.Migration |
| 46 | +
|
| 47 | + def up, do: ErrorTracker.Migrations.up(version: N) |
| 48 | + def down, do: ErrorTracker.Migrations.down(version: N) |
| 49 | + end |
| 50 | + ``` |
| 51 | +
|
| 52 | + Then run the migrations to perform the database changes: |
| 53 | +
|
| 54 | + ```bash |
| 55 | + mix ecto.migrate |
| 56 | + ``` |
| 57 | +
|
| 58 | + ## Custom prefix |
| 59 | +
|
| 60 | + ErrorTracker supports namespacing its own tables using PostgreSQL schemas, also known |
| 61 | + as "prefixes" in Ecto. With prefixes your error tables can reside outside of your primary |
| 62 | + schema (which is usually named "public"). |
| 63 | +
|
| 64 | + To use a prefix you need to specify it in your migrations: |
| 65 | +
|
| 66 | + ```elixir |
| 67 | + defmodule MyApp.Repo.Migrations.AddErrorTracker do |
| 68 | + use Ecto.Migration |
| 69 | +
|
| 70 | + def up, do: ErrorTracker.Migrations.up(prefix: "custom_schema") |
| 71 | + def down, do: ErrorTracker.Migrations.down(prefix: "custom_schema") |
| 72 | + end |
| 73 | + ``` |
| 74 | +
|
| 75 | + This will automatically create the database schema for you. If the schema does already exist |
| 76 | + the migration may fail when trying to recreate it. In such cases you can instruct the ErrorTracker |
| 77 | + not to create the schema again: |
| 78 | +
|
| 79 | + ```elixir |
| 80 | + defmodule MyApp.Repo.Migrations.AddErrorTracker do |
| 81 | + use Ecto.Migration |
| 82 | +
|
| 83 | + def up, do: ErrorTracker.Migrations.up(prefix: "custom_schema", create_schema: false) |
| 84 | + def down, do: ErrorTracker.Migrations.down(prefix: "custom_schema") |
| 85 | + end |
| 86 | + ``` |
| 87 | +
|
| 88 | + If you are using a custom schema other than the default "public" you need to configure the |
| 89 | + ErrorTracker to use that schema: |
| 90 | +
|
| 91 | + ```elixir |
| 92 | + config :error_tracker, :prefix, "custom_schema" |
| 93 | + ``` |
| 94 | + """ |
| 95 | + defdelegate up(opts \\ []), to: ErrorTracker.Migration |
| 96 | + defdelegate down(opts \\ []), to: ErrorTracker.Migration |
| 97 | +end |
| 98 | + |
| 99 | +defmodule ErrorTracker.Migration do |
| 100 | + @moduledoc false |
| 101 | + |
| 102 | + @callback up(Keyword.t()) :: :ok |
| 103 | + @callback down(Keyword.t()) :: :ok |
| 104 | + @callback current_version(Keyword.t()) :: non_neg_integer() |
| 105 | + |
| 106 | + def up(opts \\ []) when is_list(opts) do |
| 107 | + migrator().up(opts) |
| 108 | + end |
| 109 | + |
| 110 | + def down(opts \\ []) when is_list(opts) do |
| 111 | + migrator().down(opts) |
| 112 | + end |
| 113 | + |
| 114 | + def migrated_version(opts \\ []) when is_list(opts) do |
| 115 | + migrator().migrated_version(opts) |
| 116 | + end |
| 117 | + |
| 118 | + defp migrator do |
| 119 | + case ErrorTracker.repo().__adapter__() do |
| 120 | + Ecto.Adapters.Postgres -> ErrorTracker.Migrations.Postgres |
| 121 | + adapter -> raise "ErrorTracker does not support #{adapter}" |
| 122 | + end |
| 123 | + end |
| 124 | +end |
0 commit comments