Sha256: ba20c0975af6ee941fd67046e44a7e6bd22653110e55adefd44032b5cc8ffba3

Contents?: true

Size: 1.62 KB

Versions: 5

Compression:

Stored size: 1.62 KB

Contents

require 'logger'

db_name = ENV['DB'] || 'sqlite3'
database_yml = File.expand_path('../../database.yml', __FILE__)

if File.exists?(database_yml)
  active_record_configuration = YAML.load_file(database_yml)
  
  ActiveRecord::Base.configurations = active_record_configuration
  config = ActiveRecord::Base.configurations[db_name]
  
  begin
    ActiveRecord::Base.establish_connection(db_name)
    ActiveRecord::Base.connection
  rescue
    case db_name
    when /mysql/      
      ActiveRecord::Base.establish_connection(config.merge('database' => nil))
      ActiveRecord::Base.connection.create_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.create_database(config['database'], config.merge('encoding' => 'utf8'))
    end
    
    ActiveRecord::Base.establish_connection(config)
  end
    
  ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), "/../debug.log"))
  ActiveRecord::Base.default_timezone = :utc
  
  ActiveRecord::Base.silence do
    ActiveRecord::Migration.verbose = false
    
    load(File.dirname(__FILE__) + '/../schema.rb')
    load(File.dirname(__FILE__) + '/../models.rb')
  end
else
  raise "Please create #{database_yml} first to configure your database. Take a look at: #{database_yml}.sample"
end

def clean_database!
  models = [Tagalong::TagalongTag, Tagalong::TagalongTagging]
  models.each do |model|
    ActiveRecord::Base.connection.execute "DELETE FROM #{model.table_name}"
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
tagalong-0.1.4 spec/support/setup_database.rb
tagalong-0.1.3 spec/support/setup_database.rb
tagalong-0.1.2 spec/support/setup_database.rb
tagalong-0.1.1 spec/support/setup_database.rb
tagalong-0.1.0 spec/support/setup_database.rb