Class: Karo::Db

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/karo/db.rb

Instance Method Summary collapse

Instance Method Details

#consoleObject



75
76
77
78
79
80
81
82
# File 'lib/karo/db.rb', line 75

def console
  configuration = Config.load_configuration(options)

  path = File.join(configuration["path"], "current")
  ssh  = "ssh #{configuration["user"]}@#{configuration["host"]} -t"
  cmd  = "cd #{path} && bundle exec rails dbconsole #{options[:environment]} -p"
  system "#{ssh} '#{cmd}'"
end

#pullObject



19
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
# File 'lib/karo/db.rb', line 19

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

   # Drop local database and re-create
   system "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"]}`;'"

   ssh  = "ssh #{configuration["user"]}@#{configuration["host"]}"

   system "#{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"]}"

   if options[:migrate]
     say "Running rake db:migrations", :green
     system "bundle exec rake db:migrate"
   end

   say "Database sync complete", :green
end

#pushObject



70
71
72
# File 'lib/karo/db.rb', line 70

def push
   say "Pending Implementation...", :yellow
end