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

Automatically reload test databases on migration #334

Open
dankohn opened this issue Aug 30, 2014 · 2 comments
Open

Automatically reload test databases on migration #334

dankohn opened this issue Aug 30, 2014 · 2 comments

Comments

@dankohn
Copy link

dankohn commented Aug 30, 2014

parlallel_tests, while amazing, doesn't support the Rails 4 feature of automatically reloading the test databases after migration. The following works for me, though it's not elegant:

#lib/tasks/custom.rake
namespace :custom do
  desc "Reset test database"
  task test_reset: :environment do
    sh 'RAILS_ENV=test rake parallel:load_schema'
  end

  desc "Hard reset test database"
  task test_reset_hard: :environment do
    sh 'rake custom:pg_terminate RAILS_ENV=test parallel:drop parallel:create parallel:load_schema'
  end

  desc "Prepare test database"
  task parallel_prepare: :environment do
    sh 'RAILS_ENV=test rake custom:parallel_prepare_complete'
  end

  desc "Finish preparing test databases"
  task parallel_prepare_complete: :environment do
    if ActiveRecord::Base.maintain_test_schema && ActiveRecord::Migrator.needs_migration?
      puts 'Loading schema.rb into each of the parallel test databases'
      Rake::Task['parallel:load_schema'].reenable
      Rake::Task['parallel:load_schema'].invoke
    end
  end

  desc "Show if test migration needed"
  task test_migration_needed: :environment do
    command = %q{RAILS_ENV=test rails runner 'puts "#{ActiveRecord::Migrator.needs_migration?}"'}
    sh command
  end

  desc "Terminate Postgres users to enable db:drop"
  task pg_terminate: :environment do
    sh "echo 'SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE datname = current_database() AND pg_stat_activity.pid <> pg_backend_pid();' | rails db"
  end
end

Then replace ActiveRecord::Migration.maintain_test_schema! in spec/rails_helper.rb:

if ActiveRecord::Base.maintain_test_schema && ActiveRecord::Migrator.needs_migration?
  require 'rake'
  Chewy::Application.load_tasks
  Rake::Task['parallel:load_schema'].invoke
end

And redefine the default task in `lib/tasks/default.rake:

task(:default).clear.enhance ['custom:parallel_prepare', 'parallel:spec','parallel:features']

I'd be happy to add these as a pull request, but others can probably suggest a less hacky solution. This is a good overview of changing RAILS_ENV in rake tasks.

@ronwsmith
Copy link

👍

@jeremywadsack
Copy link

As most of these just run a shell command, do you need them to load :environment? Seems like removing that dependency would make them much faster. Especially as the shell command then reloads the Rails environment under test.

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

3 participants