Hello! I faced problem with adapters at RAILS_ENV=some_env rake test. #5669

Closed
wants to merge 1 commit into
from

3 participants

@akostrikov

Scenario:
I run tests on couple environments:
test, lite, etc.
But I get purge only on my "test" database when I invoke tests with RAILS_ENV=lite because of

task :purge => :environment do
  abcs = ActiveRecord::Base.configurations
  case abcs['test']['adapter']

It is wrong behavior and it breaks tests with connection error if adapters for test and lite differs.

Makes me very sad about that. I had to monkeypatch my Rake file like so:

Add your own tasks in files placed in lib/tasks ending in .rake,
for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.



require File.expand_path('../config/application', __FILE__)
require 'rake'

SomeApp::Application.load_tasks

if %w(test lite).include? Rails.env

tasks = Rake.application.instance_variable_get '@tasks'
tasks.delete 'db:test:load'
tasks.delete 'db:test:purge'

db_namespace = namespace :db do
namespace :test do
task :load => 'db:test:purge' do
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[Rails.env])
ActiveRecord::Schema.verbose = false
db_namespace['schema:load'].invoke
end
task :purge => :environment do
abcs = ActiveRecord::Base.configurations
case abcs[Rails.env]['adapter']
when /mysql/
ActiveRecord::Base.establish_connection(:test)
ActiveRecord::Base.connection.recreate_database(abcs['test']['database'], mysql_creation_options(abcs['test']))
when /postgresql/
 ActiveRecord::Base.clear_active_connections!
   drop_database(abcs['test'])
    create_database(abcs['test'])
    when /sqlite/
   dbfile = abcs['test']['database'] || abcs['test']['dbfile']
   File.delete(dbfile) if File.exist?(dbfile)
    when 'sqlserver'
    test = abcs.deep_dup['test']
    test_database = test['database']
   test['database'] = 'master'
   ActiveRecord::Base.establish_connection(test)
     ActiveRecord::Base.connection.recreate_database!(test_database)
    when "oci", "oracle"
     ActiveRecord::Base.establish_connection(:test)
      ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl|
      ActiveRecord::Base.connection.execute(ddl)
       end
      when 'firebird'
      ActiveRecord::Base.establish_connection(:test)
      ActiveRecord::Base.connection.recreate_database!
      else
     raise "Task not supported by '#{abcs['test']['adapter']}'"
     end
    end
  end
end

end

Sorry for broken mardown. Didn`t find out how to properly use ruby

Similar:
http://stackoverflow.com/questions/991038/why-do-testunits-and-testfunctionals-insist-on-running-in-development-environm
http://stackoverflow.com/questions/1090176/how-do-i-force-rails-env-in-a-rake-task

Hope to hear Your opinion! It works for me but it surely not expected behavior for RAILS_ENV=some_not_named_test_environment_for_tests rake test.
Thanks,
Alexander at alexandr.kostrikov@gmail.com

@akostrikov akostrikov Hello! I faced problem with adapters.
Scenario:
I run tests on couple environments:
test, lite, etc.
But I get purge only on my "test" database when I invoke tests with RAILS_ENV=lite because of
    task :purge => :environment do
      abcs = ActiveRecord::Base.configurations
      case abcs['test']['adapter']
It is wrong behavior and it breaks tests with connection error if adapters for test and lite differs.

Makes me very sad about that. I had to monkeypatch my Rake file like so:

# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require File.expand_path('../config/application', __FILE__)
require 'rake'

EUAdvantage::Application.load_tasks

if %w(lite moscow).include? Rails.env

  tasks = Rake.application.instance_variable_get '@tasks'
  tasks.delete 'db:test:load'
  tasks.delete 'db:test:purge'

  db_namespace = namespace :db do
    namespace :test do
      task :load => 'db:test:purge' do
        ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[Rails.env])
        ActiveRecord::Schema.verbose = false
        db_namespace['schema:load'].invoke
      end
      task :purge => :environment do
        abcs = ActiveRecord::Base.configurations
        case abcs[Rails.env]['adapter']
          when /mysql/
            ActiveRecord::Base.establish_connection(:test)
            ActiveRecord::Base.connection.recreate_database(abcs['test']['database'], mysql_creation_options(abcs['test']))
          when /postgresql/
            ActiveRecord::Base.clear_active_connections!
            drop_database(abcs['test'])
            create_database(abcs['test'])
          when /sqlite/
            dbfile = abcs['test']['database'] || abcs['test']['dbfile']
            File.delete(dbfile) if File.exist?(dbfile)
          when 'sqlserver'
            test = abcs.deep_dup['test']
            test_database = test['database']
            test['database'] = 'master'
            ActiveRecord::Base.establish_connection(test)
            ActiveRecord::Base.connection.recreate_database!(test_database)
          when "oci", "oracle"
            ActiveRecord::Base.establish_connection(:test)
            ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl|
              ActiveRecord::Base.connection.execute(ddl)
            end
          when 'firebird'
            ActiveRecord::Base.establish_connection(:test)
            ActiveRecord::Base.connection.recreate_database!
          else
            raise "Task not supported by '#{abcs['test']['adapter']}'"
        end
      end
    end
  end

end


Hope to hear Your opinion! It works for me but it surely not expected behavior for RAILS_ENV=some_not_named_test_environment_for_tests rake test.
Thanks,
Alexander at alexandr.kostrikov@gmail.com
6b62fd6
@steveklabnik
Ruby on Rails member

Basically, what you're saying is that when you run RAILS_ENV=lite rake test your tests don't run in the lite environment, they run in the test environment, correct?

@akostrikov

Yes, it is. It is not clear for me.

@steveklabnik
Ruby on Rails member

The rails convention is to run your tests in the test environment. I'm not sure how many people want to make that environment configurable. So I'm closing this as 'not really a bug.'

If you'd like to add this as a new feature, please do and submit a pull request, but I think this would add complexity for very little gain. You can always ask rails-core for feedback too, before working on it. Thanks!

@mhuggins

This is an issue for my team as well. We use test env when testing on our own machines, but ci-test for our CI machine build, which has a different database setup (different adapters and all). I don't understand why this issue is closed, as not everyone will be testing in the test env no matter what.

@steveklabnik
Ruby on Rails member

As I said above, if you'd like this feature, feel free to implement it and send a pull request, but we don't keep feature requests open in the Issues tracker.

@akostrikov

Nice! I`ll reopen it with implementation

@mhuggins

Cool, thanks for the info steveklabnik!

@steveklabnik
Ruby on Rails member

@akostrikov you'd have to open a new issue, github does not let you attach code to an existing pull request.

@steveklabnik
Ruby on Rails member

@mhuggins any time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment