Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manual Ecto migrations addon #44

Open
andyduong1920 opened this issue Nov 12, 2020 · 6 comments
Open

Manual Ecto migrations addon #44

andyduong1920 opened this issue Nov 12, 2020 · 6 comments

Comments

@andyduong1920
Copy link
Member

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
Copy link
Member Author

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

@rosle
Copy link

rosle commented Jan 4, 2022

@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.

@andyduong1920
Copy link
Member Author

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 :)

@rosle
Copy link

rosle commented Jan 4, 2022

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

@andyduong1920
Copy link
Member Author

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

That is correct @rosle :)

@andyduong1920
Copy link
Member Author

@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 #148

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants