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 27 28 29 30 |
# File 'lib/gemika/database.rb', line 15 def initialize( = {}) yaml_config_folder = .fetch(:config_folder, 'spec/support') yaml_config_filename = if Env.github? 'database.github.yml' else 'database.yml' end yaml_config_path = File.join(yaml_config_folder, yaml_config_filename) if File.exist?(yaml_config_path) @yaml_config = YAML.load_file(yaml_config_path) else @yaml_config = {} warn "No database configuration in #{yaml_config_path}, using defaults: #{adapter_config.inspect}" end @connected = false end |
Instance Method Details
#adapter_config ⇒ Object
Returns a hash of ActiveRecord adapter options for the currently activated database gem.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/gemika/database.rb', line 89 def adapter_config default_config = {} default_config['database'] = guess_database_name if Env.gem?('pg') default_config['adapter'] = 'postgresql' default_config['password'] = '' user_config = @yaml_config['postgresql'] || @yaml_config['postgres'] || @yaml_config['pg'] || {} elsif Env.gem?('mysql2') default_config['adapter'] = 'mysql2' default_config['encoding'] = 'utf8' user_config = (@yaml_config['mysql'] || @yaml_config['mysql2']) || {} elsif Env.gem?('sqlite3') default_config['adapter'] = 'sqlite3' default_config['database'] = ':memory:' user_config = (@yaml_config['sqlite'] || @yaml_config['sqlite3']) || {} else raise UnknownAdapter, "Unknown database type. Either 'pg', 'mysql2', or 'sqlite3' gem should be in your current bundle." end default_config.merge(user_config).symbolize_keys end |
#connect ⇒ Object
Connects ActiveRecord to the database configured in spec/support/database.yml.
35 36 37 38 39 40 |
# File 'lib/gemika/database.rb', line 35 def connect unless @connected ActiveRecord::Base.establish_connection(**adapter_config) @connected = true end end |
#drop_tables! ⇒ Object
Drops all tables from the current database.
45 46 47 48 49 50 |
# File 'lib/gemika/database.rb', line 45 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
63 64 65 66 |
# File 'lib/gemika/database.rb', line 63 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
80 81 82 83 84 |
# File 'lib/gemika/database.rb', line 80 def rewrite_schema!(&block) connect drop_tables! migrate(&block) end |