Sha256: 934e55040de938549b0374b978a2536e3c6bc6accbd6701417197499adf78d13
Contents?: true
Size: 1.56 KB
Versions: 1
Compression:
Stored size: 1.56 KB
Contents
require 'yaml' module Gemika class Database def initialize(options = {}) config_folder = options.fetch(:config_folder, 'spec/support') config_filename = travis? ? 'database.travis.yml' : 'database.yml' config_path = File.join(config_folder, config_filename) File.exists?(config_path) or raise ArgumentError, "Missing database configuration file: #{database_config_file}" @config = YAML.load_file(config_path) @connected = false end def connect unless @connected if pg? adapter_config = (@config['postgresql'] || @config['postgres'] || @config['pg']).merge(adapter: 'postgresql') elsif mysql2? adapter_config = (@config['mysql2'] || @config['mysql']).merge(adapter: 'mysql2', encoding: 'utf8') else raise "Unknown database type" end ActiveRecord::Base.establish_connection(adapter_config) @connected = true end end def drop_tables! connect connection.tables.each do |table| connection.drop_table table end end def migrate(&block) connect ActiveRecord::Migration.class_eval(&block) end def rewrite_schema!(&block) connect drop_tables! migrate(&block) end private def connection ActiveRecord::Base.connection end def pg? not mysql2? end def mysql2? gemfile_contents =~ /\bmysql2\b/ end def travis? !!ENV['TRAVIS'] end def gemfile_contents File.read(ENV['BUNDLE_GEMFILE']) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
gemika-0.1.0 | lib/gemika/database.rb |