Class: Railscheck::Test::DataBase

Inherits:
Railscheck::TestCase show all
Defined in:
lib/test/tc_database.rb

Instance Method Summary collapse

Methods inherited from Railscheck::TestCase

#default_test

Instance Method Details

#test_database_connection_workingObject

Test that test database does not overlap with development/production database.



7
8
9
10
11
12
13
14
# File 'lib/test/tc_database.rb', line 7

def test_database_connection_working
  assert File.file?("#{RAILS_ROOT}/config/database.yml"), "Database configuration file database.yml must be present"
  
  assert_nothing_raised("Could not open database connection / no migration schema_info table in database") do
   ActiveRecord::Base.establish_connection
   ActiveRecord::Base.connection.select_all('select * from schema_info')
  end
end

#test_database_consistent_with_migration_versionObject

Test that all migrations has been migrated. Thanks to Mike Naberezny for the orginal code.



44
45
46
47
48
49
50
51
# File 'lib/test/tc_database.rb', line 44

def test_database_consistent_with_migration_version
   db_version =  ActiveRecord::Migrator.current_version rescue 0
   highest_migration_version_in_project = Dir.glob("#{RAILS_ROOT}/db/migrate/*.rb" ).map { |f|
     f.match(/(\d+)_.*\.rb$/) ? $1.to_i : 0
    }.max || 0
    
   assert_equal highest_migration_version_in_project, db_version, "Database version #{db_version} and migration version #{highest_migration_version_in_project} is inconsistent"+(db_version<highest_migration_version_in_project ? " (forgot to run db:migrate?)." : ".")
end

#test_driver_suitable_for_productionObject

Test if driver used is suitable for production. Thanks to Mike Naberezny for the orginal code.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/test/tc_database.rb', line 29

def test_driver_suitable_for_production
  config = ActiveRecord::Base.configurations['production']
  if config['adapter'] == 'mysql' 
    ActiveRecord::Base.require_mysql
    ruby_based_mysql_driver =  Mysql::VERSION.to_s.include?('-ruby')
    
    if RAILS_ENV == 'production'
     assert !ruby_based_mysql_driver, "Ruby-based MySQL driver is not suitable for production use"
    else
     warn "Ruby-based MySQL driver is not suitable for production use. Remember to change driver before switching to production use."
    end
  end
end