Sha256: 7740de1b4f8d2773cf71071ba372b5458427f87c34c85b14e7d7162193ca4712

Contents?: true

Size: 1.71 KB

Versions: 4

Compression:

Stored size: 1.71 KB

Contents

database_folder = "#{File.dirname(__FILE__)}/../db"
database_adapter = ENV['DB'] ||= 'mysql'

if ENV['STDOUT_LOGGING']
  log = Logger.new(STDOUT)
  log.sev_threshold = Logger::DEBUG
  ActiveRecord::Base.logger = log
end

ActiveRecord::Migration.verbose = false
ActiveRecord::Base.table_name_prefix = ENV['DB_PREFIX'].to_s
ActiveRecord::Base.table_name_suffix = ENV['DB_SUFFIX'].to_s
ActiveRecord::Base.configurations = YAML::load(ERB.new(IO.read("#{database_folder}/database.yml")).result)

config = ActiveRecord::Base.configurations[database_adapter]

unless config['database'] == ':memory:'
  # Postgresql or Mysql
  config['database'].concat ENV['TRAVIS_JOB_NUMBER'].to_s.gsub(/\W/, '_')
end

begin
  case database_adapter
  when 'sqlite'
    ActiveRecord::Base.establish_connection(database_adapter.to_sym)
  when 'mysql'
    ActiveRecord::Base.establish_connection(config.merge('database' => nil))
    ActiveRecord::Base.connection.recreate_database(config['database'], {charset: 'utf8', collation: 'utf8_unicode_ci'})
  when 'postgresql'
    ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
    ActiveRecord::Base.connection.recreate_database(config['database'], config.merge('encoding' => 'utf8'))
  end
end unless ENV['NONUKES']

ActiveRecord::Base.establish_connection(config)
Foreigner.load

require "#{database_folder}/schema"
require "#{database_folder}/models"

# See http://stackoverflow.com/a/22388177/1268016
def count_queries(&block)
  count = 0
  counter_fn = ->(name, started, finished, unique_id, payload) do
    count += 1 unless payload[:name].in? %w[ CACHE SCHEMA ]
  end
  ActiveSupport::Notifications.subscribed(counter_fn, "sql.active_record", &block)
  count
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
closure_tree-4.6.3 spec/support/database.rb
closure_tree-4.6.2 spec/support/database.rb
closure_tree-4.6.1 spec/support/database.rb
closure_tree-4.6.0 spec/support/database.rb