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



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

def console
  configuration = Config.load_configuration(options)

  path = File.join(configuration["path"], "current")
  ssh  = "ssh #{configuration["user"]}@#{configuration["host"]}"

  # Forces pseudo-tty allocation
  ssh << " -t" if options[:tty]

  cmd  = "cd #{path} && bundle exec rails dbconsole #{options[:environment]} -p"

  to_run = "#{ssh} '#{cmd}'"

  say to_run, :green if options[:verbose]
  system to_run
end

#pullObject



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

   # Drop local database and re-create
   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

#pushObject



79
80
81
# File 'lib/karo/db.rb', line 79

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