Class: Reflection::Rails::Database

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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 command_line_options(opts = {})
  options = []
  options <<  "-A" if opts[:no_rehash] && opts[:no_rehash] == true
  options <<  "-h #{configuration['host']}"
  options <<  "-u#{configuration['username']}"
  options <<  "-p#{configuration['password']}" if configuration['password'] && !configuration['password'].empty?
  options <<  "#{configuration['database']}" unless opts[:skip] && opts[:skip] == :database
  options.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