Class: Database::Remote

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

Constant Summary

Constants inherited from Base

Base::DBCONFIG_BEGIN_FLAG, Base::DBCONFIG_END_FLAG

Instance Attribute Summary

Attributes inherited from Base

#config, #output_file

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

#initialize(cap_instance) ⇒ Remote

Returns a new instance of Remote.



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/capistrano-db-tasks/database.rb', line 106

def initialize(cap_instance)
  super(cap_instance)
  puts "Loading remote database config"
  @cap.within @cap.current_path do
    @cap.with rails_env: @cap.fetch(:rails_env) do
      dirty_config_content = @cap.capture(:rails, "runner \"puts '#{DBCONFIG_BEGIN_FLAG}' + ActiveRecord::Base.connection.instance_variable_get(:@config).to_yaml + '#{DBCONFIG_END_FLAG}'\"", '2>/dev/null')
      # Remove all warnings, errors and artefacts produced by bunlder, rails and other useful tools
      config_content = dirty_config_content.match(/#{DBCONFIG_BEGIN_FLAG}(.*?)#{DBCONFIG_END_FLAG}/m)[1]
      @config = YAML.load(config_content).each_with_object({}) { |(k, v), h| h[k.to_s] = v }
    end
  end
end

Instance Method Details

#clean_dump_if_neededObject



128
129
130
131
132
133
134
# File 'lib/capistrano-db-tasks/database.rb', line 128

def clean_dump_if_needed
  if @cap.fetch(:db_remote_clean)
    @cap.execute "rm -f #{db_dump_file_path}"
  else
    puts "leaving #{db_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



124
125
126
# File 'lib/capistrano-db-tasks/database.rb', line 124

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

#dumpObject



119
120
121
122
# File 'lib/capistrano-db-tasks/database.rb', line 119

def dump
  @cap.execute "cd #{@cap.current_path} && #{dump_cmd} | #{compressor.compress('-', db_dump_file_path)}"
  self
end

#load(file, cleanup) ⇒ Object

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



137
138
139
140
141
142
# File 'lib/capistrano-db-tasks/database.rb', line 137

def load(file, cleanup)
  unzip_file = File.join(File.dirname(file), File.basename(file, ".#{compressor.file_extension}"))
  # @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} && #{compressor.decompress(file)} && RAILS_ENV=#{@cap.fetch(:rails_env)} && #{import_cmd(unzip_file)}"
  @cap.execute("cd #{@cap.current_path} && rm #{unzip_file}") if cleanup
end