Module: Cloner::Ar

Included in:
Internal
Defined in:
lib/cloner/ar.rb

Instance Method Summary collapse

Instance Method Details

#ar_confObject



7
8
9
10
11
12
13
# File 'lib/cloner/ar.rb', line 7

def ar_conf
  if multi_db?
    read_ar_conf[@current_database]
  else
    read_ar_conf
  end
end

#ar_r_confObject



59
60
61
62
63
64
65
# File 'lib/cloner/ar.rb', line 59

def ar_r_conf
  if multi_db?
    read_ar_r_conf[@current_database]
  else
    read_ar_r_conf
  end
end

#ar_toObject



28
29
30
# File 'lib/cloner/ar.rb', line 28

def ar_to
  ar_conf['database']
end

#clone_arObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/cloner/ar.rb', line 86

def clone_ar
  if multi_db?
    dblist = clone_databases
    if dblist.nil?
      dblist = read_ar_conf.keys
    end
    puts "cloning multiple databases: #{dblist.join(", ")}"
    dblist.each do |dbn|
      @current_database = dbn
      run_clone_ar
    end
    @current_database = nil
  else
    run_clone_ar
  end
end

#clone_databasesObject



19
20
21
22
# File 'lib/cloner/ar.rb', line 19

def clone_databases
  # clone all databases by default
  nil
end

#db_file_nameObject



51
52
53
54
55
56
57
# File 'lib/cloner/ar.rb', line 51

def db_file_name
  if multi_db?
    "cloner_" + @current_database
  else
    "cloner"
  end
end

#env_databaseObject



24
25
26
# File 'lib/cloner/ar.rb', line 24

def env_database
  Rails.env
end

#multi_db?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/cloner/ar.rb', line 15

def multi_db?
  false
end

#read_ar_confObject



2
3
4
5
6
# File 'lib/cloner/ar.rb', line 2

def read_ar_conf
  @conf ||= begin
    YAML.load(ERB.new(File.read(Rails.root.join('config', 'database.yml'))).result)[env_database]
  end
end

#read_ar_r_confObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cloner/ar.rb', line 32

def read_ar_r_conf
  @ar_r_conf ||= begin
    do_ssh do |ssh|
      ret = ssh_exec!(ssh, "cat #{e(remote_app_path + '/config/database.yml')}")
      check_ssh_err(ret)
      begin
        res = YAML.load(ERB.new(ret[0]).result)[env_from]
        raise 'no data' if res.blank?
        #res['host'] ||= '127.0.0.1'
      rescue Exception => e
        puts "unable to read remote database.yml for env #{env_from}."
        puts "Remote file contents:"
        puts ret[0]
      end
      res
    end
  end
end

#run_clone_arObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cloner/ar.rb', line 67

def run_clone_ar
  if ar_conf["adapter"] != ar_r_conf["adapter"]
    puts "Error: ActiveRecord adapter mismatch: local #{ar_conf["adapter"]}, remote #{ar_r_conf["adapter"]}"
    puts "it is not possible to convert from one database to another via this tool."
    exit
  end

  case ar_conf["adapter"]
  when 'postgresql'
    clone_pg
  when 'mysql2'
    clone_my
  else
    puts "unknown activerecord adapter: #{ar_conf["adapter"]}"
    puts "currently supported adapters: mysql2, postgresql"
    exit
  end
end