20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/karo/db.rb', line 20
def pull
configuration = Config.load_configuration(options)
local_db_config_file = File.expand_path("config/database.yml")
unless File.exists?(local_db_config_file)
raise Thor::Error, "Please make sure that this file exists locally? '#{local_db_config_file}'"
end
local_db_config = YAML.load(File.read('config/database.yml'))
if local_db_config["development"].nil? || local_db_config["development"].empty?
raise Thor::Error, "Please make sure MySQL development configuration exists within this file? '#{local_db_config_file}'"
end
local_db_config = local_db_config["development"]
say "Loading local database configuration", :green
server_db_config_file = File.join(configuration["path"], "shared/config/database.yml")
host = "deploy@#{configuration["host"]}"
cmd = "ssh #{host} 'cat #{server_db_config_file}'"
server_db_config_output = `#{cmd}`
yaml_without_any_ruby = ERB.new(server_db_config_output).result
server_db_config = YAML.load(yaml_without_any_ruby)
if server_db_config[options[:environment]].nil? || server_db_config[options[:environment]].empty?
raise Thor::Error, "Please make sure MySQL development configuration exists within this file? '#{server_db_config_file}'"
end
server_db_config = server_db_config[options[:environment]]
say "Loading #{options[:environment]} server database configuration", :green
to_run = "mysql -v -h #{local_db_config["host"]} -u#{local_db_config["username"]} -p#{local_db_config["password"]} -e 'DROP DATABASE IF EXISTS `#{local_db_config["database"]}`; CREATE DATABASE IF NOT EXISTS `#{local_db_config["database"]}`;'"
say to_run, :green if options[:verbose]
system to_run
ssh = "ssh #{configuration["user"]}@#{configuration["host"]}"
to_run = "#{ssh} \"mysqldump --opt -C -u#{server_db_config["username"]} -p#{server_db_config["password"]} #{server_db_config["database"]}\" | mysql -v -h #{local_db_config["host"]} -C -u#{local_db_config["username"]} -p#{local_db_config["password"]} #{local_db_config["database"]}"
say to_run, :green if options[:verbose]
system to_run
if options[:migrate]
say "Running rake db:migrations", :green
system "bundle exec rake db:migrate"
end
say "Database sync complete", :green
end
|