Class: Brancher::AutoCopying::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/brancher/auto_copying.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Executor

Returns a new instance of Executor.



22
23
24
# File 'lib/brancher/auto_copying.rb', line 22

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



20
21
22
# File 'lib/brancher/auto_copying.rb', line 20

def config
  @config
end

Instance Method Details

#auto_copyObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/brancher/auto_copying.rb', line 26

def auto_copy
  return if config[:database] == config[:original_database]

  database_name = config[:database]
  original_database_name = config[:original_database]

  case config[:adapter]
  when /mysql/
    mysql_copy(original_database_name, database_name)
  when /postgresql/
    pg_copy(original_database_name, database_name)
  end
end

#mysql_copy(original_database_name, database_name) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/brancher/auto_copying.rb', line 40

def mysql_copy(original_database_name, database_name)
  ActiveRecord::Tasks::DatabaseTasks.create(config.with_indifferent_access)

  cmd = ["mysqldump", "-u", config[:username]]
  cmd.concat(["-h", config[:host]]) if config[:host].present?
  cmd.concat(["-p#{config[:password]}"]) if config[:password].present?
  cmd << original_database_name
  cmd.concat(["|", "mysql", "-u", config[:username]])
  cmd.concat(["-h", config[:host]]) if config[:host].present?
  cmd.concat(["-p#{config[:password]}"]) if config[:password].present?
  cmd << database_name
  system(cmd.join(" "))
end

#pg_copy(original_database_name, database_name) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/brancher/auto_copying.rb', line 54

def pg_copy(original_database_name, database_name)
  env = {}
  env["PGUSER"] = config[:username] if config[:username].present?
  env["PGPASSWORD"] = config[:password] if config[:password].present?
  env["PGHOST"] = config[:host] if config[:host].present?

  system(env, "createdb", "-T", original_database_name, database_name)
end