Class: Gemika::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/gemika/database.rb

Overview

Helpers for creating a test database.

Defined Under Namespace

Classes: Error, UnknownAdapter

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Database

Returns a new instance of Database.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gemika/database.rb', line 15

def initialize(options = {})
  yaml_config_folder = options.fetch(:config_folder, 'spec/support')
  yaml_config_filename = Env.travis? ? 'database.travis.yml' : 'database.yml'
  yaml_config_path = File.join(yaml_config_folder, yaml_config_filename)
  if File.exists?(yaml_config_path)
    @yaml_config = YAML.load_file(yaml_config_path)
  else
    warn "No database configuration in #{yaml_config_path}, using defaults: #{adapter_config.inspect}"
    @yaml_config = {}
  end
  @connected = false
end

Instance Method Details

#adapter_configObject

Returns a hash of ActiveRecord adapter options for the currently activated database gem.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/gemika/database.rb', line 85

def adapter_config
  default_config = {}
  default_config['database'] = guess_database_name
  if Env.gem?('pg')
    default_config['adapter'] = 'postgresql'
    default_config['username'] = 'postgres' if Env.travis?
    default_config['password'] = ''
    user_config = @yaml_config['postgresql'] || @yaml_config['postgres'] || @yaml_config['pg'] || {}
  elsif Env.gem?('mysql2')
    default_config['adapter'] = 'mysql2'
    default_config['username'] = 'travis' if Env.travis?
    default_config['encoding'] = 'utf8'
    user_config = (@yaml_config['mysql'] || @yaml_config['mysql2']) || {}
  else
    raise UnknownAdapter, "Unknown database type. Either 'pg' or 'mysql2' gem should be in your current bundle."
  end
  default_config.merge(user_config)
end

#connectObject

Connects ActiveRecord to the database configured in spec/support/database.yml.



31
32
33
34
35
36
# File 'lib/gemika/database.rb', line 31

def connect
  unless @connected
    ActiveRecord::Base.establish_connection(adapter_config)
    @connected = true
  end
end

#drop_tables!Object

Drops all tables from the current database.



41
42
43
44
45
46
# File 'lib/gemika/database.rb', line 41

def drop_tables!
  connect
  connection.tables.each do |table|
    connection.drop_table table
  end
end

#migrate(&block) ⇒ Object

Runs the ActiveRecord database migration described in block.

end

Examples:

Gemika::Database.new.migrate do
  create_table :users do |t|
    t.string :name
    t.string :email
    t.string :city
  end


59
60
61
62
# File 'lib/gemika/database.rb', line 59

def migrate(&block)
  connect
  ActiveRecord::Migration.class_eval(&block)
end

#rewrite_schema!(&block) ⇒ Object

Drops all tables, then runs the ActiveRecord database migration described in block.

end

Examples:

Gemika::Database.new.rewrite_schema! do
  create_table :users do |t|
    t.string :name
    t.string :email
    t.string :city
  end


76
77
78
79
80
# File 'lib/gemika/database.rb', line 76

def rewrite_schema!(&block)
  connect
  drop_tables!
  migrate(&block)
end