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

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

Closed
wants to merge 1 commit into from
Closed

Commits on Mar 30, 2012

  1. 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
    akostrikov committed Mar 30, 2012
    Copy the full SHA
    6b62fd6 View commit details
    Browse the repository at this point in the history