2021-02-01 17:58:25 +00:00
|
|
|
require 'rake'
|
|
|
|
|
|
|
|
|
|
module JamRuby
|
|
|
|
|
class TestSupport
|
|
|
|
|
|
|
|
|
|
#helper for resetting test database
|
2021-02-15 04:32:27 +00:00
|
|
|
#drop create and execute db migrations
|
2021-02-01 17:58:25 +00:00
|
|
|
def self.recreate_database
|
|
|
|
|
ENV['RAILS_ENV'] = 'test'
|
|
|
|
|
Rake.application.init
|
|
|
|
|
Rake.application.load_rakefile
|
|
|
|
|
begin
|
2021-02-15 04:32:27 +00:00
|
|
|
Rake::Task['db:jam_ruby:drop'].invoke
|
2021-02-01 17:58:25 +00:00
|
|
|
Rake::Task['db:jam_ruby:create'].invoke
|
|
|
|
|
Rake::Task['db:jam_ruby:migrate'].invoke
|
|
|
|
|
rescue ActiveRecord::NoDatabaseError
|
|
|
|
|
puts "Database does not exist. Creating.."
|
|
|
|
|
Rake::Task['db:jam_ruby:create'].invoke
|
|
|
|
|
rescue ActiveRecord::ConnectionNotEstablished
|
|
|
|
|
puts "Database connection error"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.migrate_database
|
|
|
|
|
ENV['RAILS_ENV'] = 'test'
|
2021-02-15 04:32:27 +00:00
|
|
|
# invoke init in this way; otherwise any args passed to rspec will pass through to the rake task and blow it up.
|
|
|
|
|
# for instance, bundle exec rspec spec/some.rb -e "specific test" will cause a weird error
|
|
|
|
|
Rake.application.init('rake', [])
|
2021-02-01 17:58:25 +00:00
|
|
|
Rake.application.load_rakefile
|
|
|
|
|
Rake::Task['db:jam_ruby:migrate'].invoke
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
end
|