Class: Database::Remote

Inherits:
Base
  • Object
show all
Defined in:
lib/capistrano-db-tasks/database.rb

Instance Attribute Summary

Attributes inherited from Base

#config, #output_file

Instance Method Summary collapse

Methods inherited from Base

#credentials, #current_time, #database, #mysql?, #pgpass, #postgresql?

Constructor Details

#initialize(cap_instance) ⇒ Remote

Returns a new instance of Remote.



73
74
75
76
77
# File 'lib/capistrano-db-tasks/database.rb', line 73

def initialize(cap_instance)
  super(cap_instance)
  @config = @cap.capture("cat #{@cap.current_path}/config/database.yml")
  @config = YAML.load(ERB.new(@config).result)[@cap.fetch(:rails_env).to_s]
end

Instance Method Details

#clean_dump_if_neededObject



88
89
90
91
92
93
94
# File 'lib/capistrano-db-tasks/database.rb', line 88

def clean_dump_if_needed
  if @cap.fetch(:db_remote_clean)
    @cap.execute "rm -f #{dump_file_path}"
  else
    @cap.info "leaving #{dump_file_path} on the server (add \"set :db_remote_clean, true\" to deploy.rb to remove)"
  end
end

#download(local_file = "#{output_file}") ⇒ Object



84
85
86
# File 'lib/capistrano-db-tasks/database.rb', line 84

def download(local_file = "#{output_file}")
  @cap.download! dump_file_path, local_file
end

#dumpObject



79
80
81
82
# File 'lib/capistrano-db-tasks/database.rb', line 79

def dump
  @cap.execute "cd #{@cap.current_path} && #{dump_cmd} | bzip2 - - > #{output_file}"
  self
end

#load(file, cleanup) ⇒ Object

cleanup = true removes the mysqldump file after loading, false leaves it in db/



97
98
99
100
101
102
# File 'lib/capistrano-db-tasks/database.rb', line 97

def load(file, cleanup)
  unzip_file = File.join(File.dirname(file), File.basename(file, '.bz2'))
  # @cap.run "cd #{@cap.current_path} && bunzip2 -f #{file} && RAILS_ENV=#{@cap.rails_env} bundle exec rake db:drop db:create && #{import_cmd(unzip_file)}"
  @cap.execute "cd #{@cap.current_path} && bunzip2 -f #{file} && RAILS_ENV=#{@cap.fetch(:rails_env)} && #{import_cmd(unzip_file)}"
  @cap.execute("cd #{@cap.current_path} && rm #{unzip_file}") if cleanup
end