Class: Gemika::Database
- Inherits:
-
Object
- Object
- Gemika::Database
- Defined in:
- lib/gemika/database.rb
Overview
Helpers for creating a test database.
Defined Under Namespace
Classes: Error, UnknownAdapter
Instance Method Summary collapse
-
#adapter_config ⇒ Object
Returns a hash of ActiveRecord adapter options for the currently activated database gem.
-
#connect ⇒ Object
Connects ActiveRecord to the database configured in
spec/support/database.yml
. -
#drop_tables! ⇒ Object
Drops all tables from the current database.
-
#initialize(options = {}) ⇒ Database
constructor
A new instance of Database.
-
#migrate(&block) ⇒ Object
Runs the ActiveRecord database migration described in
block
. -
#rewrite_schema!(&block) ⇒ Object
Drops all tables, then runs the ActiveRecord database migration described in
block
.
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( = {}) yaml_config_folder = .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_config ⇒ Object
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 |
#connect ⇒ Object
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
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
76 77 78 79 80 |
# File 'lib/gemika/database.rb', line 76 def rewrite_schema!(&block) connect drop_tables! migrate(&block) end |