Could I disable creation of schema_migrations

I have a phoenix application that has standard Repo setup and uses postgresql.
I want to connect it to second database that is read_only: true. I manage to do this with the following config

# config/config.exs
config :my_app,
  ecto_repos: [MyApp.Repo, MyApp.SecondRepo],

config :my_app, MyApp.Repo,
  url: "...."

config :my_app, MyApp.SecondRepo,
  url: "..."

and the config for the two Repos
My main repository

# lib/my_app/repo.ex
defmodule MyApp.Repo do
  use Ecto.Repo, 
    otp_app: :my_app,
    adapter: Ecto.Adapters.Postgres
end

My second repository (read_only: true)

# lib/my_app/second_repo.ex
defmodule MyApp.SecondRepo do
  use Ecto.Repo, 
    otp_app: :my_app,
    adapter: Ecto.Adapters.Postgres,
    read_only: true
end

I create also priv/second_repo/migrations folder.
However this approach seems to cause the creation of schema_migrations in the database for the SecondRepo.
I try to avoid this by pointing the SecondRepo

# config/config.exs
...
config :my_app, MyApp.SecondRepo,
  url: "...",
  migration_repo: MyApp.Repo

This seems to be half working but now every time when I try to run
mix ecto.gen.migration ... or mix ecto.migrate or mix ecto.rollback it seems to try to run the same migration two times.
I was wondering if there is some way to disable the check for the schema_migrations
I saw this question but it seems this approach still creates the schema_migrations.

I think you have to run these mix ecto.* commands with -r option, such as:

mix ecto.create -r MyApp.Repo
1 Like

It seems like this is the only work around for local development and to have it with the same migration_repo: MyApp.Repo

Strictly speaking, this workaround works only for situations where Mix is available.

For situations where Mix is not available, you should use Ecto.Migrator directly. Here is a snippet from docs of Ecto.Migrator:

defmodule MyApp.Release do
  @app :my_app

  def migrate do
    for repo <- repos() do
      {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
    end
  end

  def rollback(repo, version) do
    {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
  end

  defp repos do
    Application.load(@app)
    # here, limit it to repos that you want to run migration for
    Application.fetch_env!(@app, :ecto_repos)
  end
end
1 Like