Skip to content
This repository

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

Closed
wants to merge 1 commit into from

3 participants

Александр Костриков Steve Klabnik Matt Huggins
Александр Костриков

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 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
Steve Klabnik
Collaborator

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?

Александр Костриков

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

Steve Klabnik
Collaborator

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!

Matt Huggins

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.

Steve Klabnik
Collaborator

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.

Александр Костриков

Nice! I`ll reopen it with implementation

Matt Huggins

Cool, thanks for the info steveklabnik!

Steve Klabnik
Collaborator

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

Steve Klabnik
Collaborator

@mhuggins any time.

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

Showing 1 unique commit by 1 author.

Mar 30, 2012
Александр Костриков 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
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 45 additions and 0 deletions. Show diff stats Hide diff stats

  1. +45 0 activerecord/lib/active_record/railties/databases.rake
45 activerecord/lib/active_record/railties/databases.rake
@@ -488,6 +488,51 @@ db_namespace = namespace :db do
488 488 db_namespace["structure:load"].invoke
489 489 ensure
490 490 ENV['RAILS_ENV'] = old_env
  491 + #here problem rise! I have environment lite(for tests on sqlite)
  492 + #when I invoke RAILS_ENV=lite rake test I purge test database
  493 + #but my lite database for tests untouched=( and I get connection error.
  494 + #could we add something like
  495 + #if %w(production development).include? Rails.env
  496 + # ENV['RAILS_ENV'] = 'test'
  497 + #end
  498 + #or add to adapters parameter like testable(dropable,purgable,etc):
  499 + #production:
  500 + # adapter: sqlserver
  501 + # dataserver: development
  502 + # database: development
  503 + # username: development
  504 + # password: development
  505 + # pool: 5
  506 + # timeout: 5000
  507 +
  508 + #staging:
  509 + # adapter: sqlserver
  510 + # host: staging
  511 + # port: staging
  512 + # database: staging
  513 + # username: staging
  514 + # password: staging
  515 + # pool: 5
  516 + # timeout: 5000
  517 + # testable(dropable,purgable,etc): false
  518 +
  519 + #lite:
  520 + # adapter: sqlite3
  521 + # database: db/test.sqlite3
  522 + # pool: 5
  523 + # timeout: 5000
  524 + # testable: true
  525 +
  526 + #test:
  527 + # adapter: sqlserver
  528 + # host: test
  529 + # port: test
  530 + # database: test
  531 + # username: test
  532 + # password: test
  533 + # pool: 5
  534 + # timeout: 5000
  535 + # testable(dropable,purgable,etc): test
491 536 end
492 537 end
493 538

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.