BitcrowdEcto.Migrator behaviour (bitcrowd_ecto v0.1.0) View Source
Release migration logic for repositories.
Can deal with normal repositories & multi-tenant repositories.
Usage
In the simplest case, add the Migrator to your repository:
defmodule MyApp.Repo do
use Ecto.Repo, otp_app: :my_app, adapter: Ecto.Adapters.Postgres
use BitcrowdEcto.Migrator
end
and call the migrator from your release:
bin/my_app eval 'MyApp.Repo.up()'
Multi-Tenant repositories
For multi-tenant repositories, you need to provide a list of tenants (= PG schemas) by
overriding the known_prefixes/0
function on your repository:
defmodule MyApp.Repo do
use Ecto.Repo, otp_app: :my_app, adapter: Ecto.Adapters.Postgres
use BitcrowdEcto.Migrator
def known_prefixes do
["tenant_a", "tenant_b"]
end
end
This will make the migrator apply migrations from the priv/repo/tenant_migrations
directory
onto schemas tenant_a
and tenant_b
. The schemas will be created if necessary.
Mix tasks for development
In normal development without multi-tenancy, the usual Ecto mix tasks will work just fine.
When using tenant schemas, the normal Ecto mix tasks will only apply the "global" (i.e.
non-prefixed migrations) from priv/repo/migrations
, which is not enough. You can define
your own Mix tasks calling the up/0
and down/1
functions on your repository:
defmodule Mix.Tasks.MyApp.Migrate do
use Mix.Task
@shortdoc "Migrates our repository"
@moduledoc "Migrates the repository including the tenant schemas"
@impl true
def run(args) do
Mix.Task.run("app.config", args)
MyApp.Repo.up()
end
end
defmodule Mix.Tasks.MyApp.Rollback do
use Mix.Task
@shortdoc "Rolls back our repository"
@moduledoc "Rolls back the repository including the tenant schemas"
@impl true
def run(args) do
Mix.Task.run("app.config", args)
{[to: to], _} = OptionParser.parse!(args, strict: [to: :integer], aliases: [])
MyApp.Repo.down(to)
end
end
Link to this section Summary
Callbacks
Rolls back both the main schemas/tables and the tenant schemas to a given version.
Returns the list of prefixes used on this repository.
Migrates both the "main" (i.e. non-tenant) schemas/tables and the tenant schemas to their latest version.
Link to this section Callbacks
Specs
down(to :: non_neg_integer()) :: :ok
Rolls back both the main schemas/tables and the tenant schemas to a given version.
Specs
known_prefixes() :: [String.t()]
Returns the list of prefixes used on this repository.
Specs
up() :: :ok
Migrates both the "main" (i.e. non-tenant) schemas/tables and the tenant schemas to their latest version.