Class: Reflection::Rails::Database
- Inherits:
-
Object
- Object
- Reflection::Rails::Database
- Defined in:
- lib/reflection/rails/database.rb
Instance Attribute Summary collapse
-
#configuration ⇒ Object
Returns the value of attribute configuration.
-
#environment ⇒ Object
Returns the value of attribute environment.
-
#rails_root ⇒ Object
Returns the value of attribute rails_root.
Instance Method Summary collapse
- #clean_dump_file(target_directory) ⇒ Object
- #command_line_options(opts = {}) ⇒ Object
- #create ⇒ Object
- #drop ⇒ Object
- #dump_to_directory(target_directory) ⇒ Object
-
#initialize(new_rails_root, new_environment) ⇒ Database
constructor
A new instance of Database.
- #load_dump_from_file(target_directory) ⇒ Object
- #migrate! ⇒ Object
- #read_configuration_yml ⇒ Object
- #recreate! ⇒ Object
- #run(command) ⇒ Object
Constructor Details
#initialize(new_rails_root, new_environment) ⇒ Database
Returns a new instance of Database.
9 10 11 12 13 |
# File 'lib/reflection/rails/database.rb', line 9 def initialize(new_rails_root, new_environment) self.rails_root = new_rails_root self.environment = new_environment self.configuration = read_configuration_yml end |
Instance Attribute Details
#configuration ⇒ Object
Returns the value of attribute configuration.
5 6 7 |
# File 'lib/reflection/rails/database.rb', line 5 def configuration @configuration end |
#environment ⇒ Object
Returns the value of attribute environment.
6 7 8 |
# File 'lib/reflection/rails/database.rb', line 6 def environment @environment end |
#rails_root ⇒ Object
Returns the value of attribute rails_root.
7 8 9 |
# File 'lib/reflection/rails/database.rb', line 7 def rails_root @rails_root end |
Instance Method Details
#clean_dump_file(target_directory) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/reflection/rails/database.rb', line 24 def clean_dump_file(target_directory) dump_file = dump_file_path(target_directory) if File.exist?(dump_file) run "rm #{dump_file}" end end |
#command_line_options(opts = {}) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/reflection/rails/database.rb', line 62 def (opts = {}) = [] << "-A" if opts[:no_rehash] && opts[:no_rehash] == true << "-h #{configuration['host']}" << "-u#{configuration['username']}" << "-p#{configuration['password']}" if configuration['password'] && !configuration['password'].empty? << "#{configuration['database']}" unless opts[:skip] && opts[:skip] == :database .join(' ') end |
#create ⇒ Object
37 38 39 |
# File 'lib/reflection/rails/database.rb', line 37 def create run("echo \"CREATE DATABASE #{self.configuration['database']};\" | mysql #{command_line_options(:skip => :database)}") end |
#drop ⇒ Object
41 42 43 |
# File 'lib/reflection/rails/database.rb', line 41 def drop run("echo \"DROP DATABASE #{self.configuration['database']};\" | mysql #{command_line_options(:skip => :database)}") end |
#dump_to_directory(target_directory) ⇒ Object
15 16 17 |
# File 'lib/reflection/rails/database.rb', line 15 def dump_to_directory(target_directory) run("mysqldump #{command_line_options} > #{dump_file_path(target_directory)}") end |
#load_dump_from_file(target_directory) ⇒ Object
19 20 21 22 |
# File 'lib/reflection/rails/database.rb', line 19 def load_dump_from_file(target_directory) Reflection.log.debug "Loading dump.." run("mysql #{command_line_options(:no_rehash => true)} < #{dump_file_path(target_directory)}") end |
#migrate! ⇒ Object
45 46 47 48 |
# File 'lib/reflection/rails/database.rb', line 45 def migrate! Reflection.log.debug "Migrating database.." run("(cd #{self.rails_root} && RAILS_ENV=#{self.environment} rake db:migrate)") end |
#read_configuration_yml ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/reflection/rails/database.rb', line 50 def read_configuration_yml begin if configuration = YAML.load_file(database_config_file_path)[self.environment] return configuration else return Support.exit_with_error("Rails database configuration for '#{self.environment}' isn't available in #{database_config_file_path}") end rescue => e return Support.exit_with_error("Error while parsing Rails database configuration: #{e}") end end |
#recreate! ⇒ Object
31 32 33 34 35 |
# File 'lib/reflection/rails/database.rb', line 31 def recreate! Reflection.log.debug "Recreating database.." drop create end |
#run(command) ⇒ Object
72 73 74 75 |
# File 'lib/reflection/rails/database.rb', line 72 def run(command) # Reflection.log.debug "-- #{command}" %x(#{command}) end |