How to run migrations of another app from main app

I am following umbrella apps structure in my project. and I am writing access layer app which doesn’t have separate repo but requires to have certain tables and relations migrated for it to work. I dont wanna create separate repo for it and make it complex to maintain configs etc.

I have tried following two approaches

defmodule Data.Repo.Migrations.CreateActions do
  use Ecto.Migration

  def change do
    # First approach
    opts = []
    file_path = ExAcl.SeedHelper.priv_path_for("20181129092126_create_actions.exs", app: :acl)
    Ecto.Migrator.run(Data.Repo, file_path, :up, opts)

    {:ok, _} = Application.ensure_all_started(:acl)

    # Second approach
    Ecto.Migrator.up(HaiData.Repo, 20181129092126, ExAcl.Repo.Migrations.CreateActions)
  end
end

But It doesn’t run migrations in another app.

Maybe I am not going in right direction. I am not sure what is the best way to run migration in another app

Goal: run migrations in acl app via main data app.

:wave:

But It doesn’t run migrations in another app.

Are there any errors?

No. Migrations would just stuck at this migration

Do they access the same database?

You can’t. Migrations are always only searched for in the priv dir of the repos application.

Actually, what controls which migrations run is the :ecto_repos configuration option inside your application. So you can always do this:

config :my_app, :ecto_repos, [MyApp.Repo, OtherApp.Repo]

If you don’t want that to always happen though, you can always pass the repository to the migrate command:

$ mix ecto.migrate --repo OtherApp.Repo

And using the lower level APIs, such as Ecto.Migrator, should work too!

2 Likes

And still, each repo searches in its own application. The OP though wants a single repo with migrations in 2 applications.

2 Likes

yes same db

is there a way, I can define another migrations folder for one main repo? because its one database. And I dont see a very heavy need for second repo

Ah, I misunderstood. Thanks for clarifying.

The only way this can be achieved then is by calling mix ecto.migrate --migration-path "/path/to/another/path". So you would have to call it twice.

1 Like

This seems the only way for now. Thank you so much

Although defining multiple paths within repo configuration was more ideal.

Creating alias in root app doesnt accept migration path argument. Can I create alias for two migration paths in root app and/or in data app

This accepts migration path in latest version of ecto but doesnt run migrations of acl app. I have tried giving absolute path, relative path from app, from root. Nothing works