ilyakatz / data-migrate

Migrate and update data alongside your database structure.
MIT License
1.4k stars 192 forks source link

Is it possible to check for pending data migrations automatically? #294

Open matttarantino opened 10 months ago

matttarantino commented 10 months ago

Like schema migrations, you will get an error locally if you have pending schema migrations to run. Is this possible for data migrations as well? It looks like it once existed (https://github.com/ilyakatz/data-migrate/issues/199) but I am not seeing any code anymore.

Do you otherwise need to use the rake tasks to check manually?

jwoldan commented 1 week ago

I'm using this approach plus this check to make sure I'm not currently running a rake task in the development environment file to achieve this. I'd love to know if there's a better approach, but this seems to be working for our use case so sharing in case it's helpful to others. You end up with something like this:

  config.after_initialize do
    # Check if we are currently in a rake task, see https://stackoverflow.com/questions/15538587
    unless (ENV['RACK_ENV'].blank? || ENV['RAILS_ENV'].blank? || !("#{ENV.inspect}" =~ /worker/i).blank?)
      Rails.application.load_tasks
      Rake::Task['data:abort_if_pending_migrations'].invoke
    end
  end