Migrations are applied incorrectly in all repos

In my system I’m adding a second repo to connect. To do that I did the following changes:

config/config.exs

config :marketplace, Marketplace.ApRepo,
  username: "postgres",
  password: "postgres",
  hostname: "localhost",
  database: "ap_dev",
  port: 5432,
  show_sensitive_data_on_connection_error: true,
  pool_size: 10

lib/ap_repo.ex

defmodule Marketplace.ApRepo do
  use AshPostgres.Repo, otp_app: :marketplace
end

lib/application.ex

defmodule Marketplace.Application do
  def start(_, _) do
    children = [
      ...
      ApRepo
    ]
    ...
  end
end

Then I added one resource to it and generated the migration.

A new migration file was correctly added to the priv/ap_repo/migrations directory.

But, when I ran the mix ash_postgres.migrate, the migration process applied all the migrations I have in my original Repo to the ApRepo and not only that new resource I created.

I did notice this warning from the documentation:

This is only really useful if your api or apis only use a single repo. If you
have multiple repos and you want to run a single migration and/or migrate/roll
them back to different points, you will need to use the ecto specific task, mix
ecto.migrate and provide your repo name.

So, does this means that I need to start using ecto.migrate from now on or only if more than one repo uses the same api?

:thinking: that sounds like a bug. You should be able to use that task with multiple repos, the main drawback is if you want to only migrate a specific repo. We can probably make our task support a --repo option if it doesn’t already.

So yeah if we’re actually applying migrations from both repos to one repo that is a bug somewhere. An example project showing this happening would be really useful. I’ve had multiple repos set up in an application and not seen that issue before.

Took me some time, but here is a small project that triggers the bug GitHub - sezaru/test_ash_multi_repo

Basically it has 3 resources, resource 1 and 2 uses repo 1, resource 3 uses repo 2.

If you run mix reset Ash will generate the 3 resources in both DBs.

1 Like

Just a small bump, @zachdaniel were you able to reproduce the bug with the above example project?

Honestly, this completely fell of my radar :frowning: sorry about that. Can you open an issue for this on ash_postgres? I will look into it soon

done Migrations are applied incorrectly with multiple repos · Issue #197 · ash-project/ash_postgres · GitHub

Fixed in AshPostgres main branch.

1 Like

I had the same issue and after your fix migrate is working fine. However I tried to rollback and it applied for both my database

I used:
mix ash_postgres.rollback -n 1

I expected it to rollback the last migrate, but it applied the last rollback of each of my databases

Specifying it this way:
mix ecto.rollback --migrations-path <path> -r <Repo> -n 1
works fine

Any idea?

You would want to mix ash_postgres.rollback -n 1 -r TheOnlyRepo. mix ash_postgres.rollback -n 1 couldn’t know which repo you want to do by default.

1 Like