elixir-templates icon indicating copy to clipboard operation
elixir-templates copied to clipboard

Manual Ecto migrations addon

Open andyduong1920 opened this issue 4 years ago • 6 comments

Following the https://dashbit.co/blog/automatic-and-manual-ecto-migrations it nice to have the manual_migration pre-setting

1/ Adding migrate_manual to the https://github.com/nimblehq/elixir-templates/blob/develop/priv/templates/nimble.phx.gen.template/lib/otp_app/release_tasks.ex.eex

  def migrate_manual do
    load_app()

    for repo <- repos() do
      run_migrations(repo, "priv/repo/manual_migrations")
    end
  end

2/ Adjust aliases in mix.ex file

defp aliases() do
  [
    "ecto.migrate_all": ["ecto.migrate --migrations-path=priv/repo/migrations --migrations-path=priv/repo/manual_migrations"],
    "ecto.setup": ["ecto.create", "ecto.migrate_all", "run priv/repo/seeds.exs"],
    test: ["ecto.create --quiet", "ecto.migrate_all --quiet", "test"],
    ...
  ]
end

andyduong1920 avatar Nov 12 '20 06:11 andyduong1920

@nimblehq/elixir-guild Do you think it worth to add this Addon into the template?

andyduong1920 avatar Nov 19 '20 04:11 andyduong1920

@andyduong1920 I believe this now becomes the data_migrations, corrrect?

There is a specific config that we've added into one of the project. Because we're trying to avoid the data migration on the test.

With the config:

defp aliases() do
  [
    "ecto.migrate_all": ["ecto.migrate --migrations-path=priv/repo/migrations --migrations-path=priv/repo/manual_migrations"],
    "ecto.setup": ["ecto.create", "ecto.migrate_all", "run priv/repo/seeds.exs"],
    test: ["ecto.create --quiet", "ecto.migrate --quiet", "test"]

This works perfectly, when running mix test, it is not running the data migration. However there is another problem when we run MIX_ENV=test mix ecto.setup, the data migration is run. 💡

So we did:

defp aliases() do
  [
    "ecto.setup": ["ecto.create", &migrate/1, "run priv/repo/seeds.exs"],
    test: ["ecto.create --quiet", &migrate/1, "test"],
    ...
  ]
end

defp migrate(_) do
  if Mix.env() == :test do
    Mix.Task.run("ecto.migrate", ["--quiet"])
  else
    Mix.Task.run("ecto.migrate", [
      "--migrations-path=priv/repo/migrations",
      "--migrations-path=priv/repo/data_migrations"
    ])
  end
end

(We did remove migrate_all because we always run it. But we can, of course, keep the alias for convenient 👍 )

Not sure if anyone facing the same issue. I'll just leave the snippet here in case we might want to add it to the template.

rosle avatar Jan 04 '22 04:01 rosle

Not really @rosle manual ecto migration vs data migration are 2 topics, in our workflow data migration is triggered automatically via the Release task while the manual migration topic means the migration will be triggered manually. Is it clear :)

andyduong1920 avatar Jan 04 '22 04:01 andyduong1920

@andyduong1920 got it. But we also not have the data_migrations in the template, correct? 🤔

rosle avatar Jan 04 '22 05:01 rosle

But we also not have the data_migrations in the template, correct? 🤔

That is correct @rosle :)

andyduong1920 avatar Jan 04 '22 07:01 andyduong1920

@andyduong1920 got it. But we also not have the data_migrations in the template, correct? 🤔

I have opened a PR for the data_migrations part here FYI @rosle https://github.com/nimblehq/elixir-templates/pull/148

andyduong1920 avatar Jan 04 '22 08:01 andyduong1920