Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Currently
* It only supports mysql and postgresql (both side remote and local)
* Synchronize assets remote to local and local to remote

Commands mysql, mysqldump (or pg_dump, psql), bzip2 and unbzip2 must be in your PATH
Commands mysql, mysqldump (or pg\_dump, psql), bzip2 and unbzip2 (or gzip) must be in your PATH

Feel free to fork and to add more database support or new tasks.

Expand Down Expand Up @@ -46,6 +46,9 @@ Add to config/deploy.rb:

# if you are highly paranoid and want to prevent any push operation to the server
set :disallow_pushing, true

# if you are prefer bzip2/unbzip2 instead of gzip
set :compressor, :bzip2
```

Add to .gitignore
Expand Down
5 changes: 5 additions & 0 deletions lib/capistrano-db-tasks/compressors/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Compressors
class Base

end
end
40 changes: 40 additions & 0 deletions lib/capistrano-db-tasks/compressors/bzip2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Compressors
class Bzip2 < Base

class << self

def file_extension
"bz2"
end

def compress(from, to = nil)
to = case to
when "-"
"-c --stdout"
when nil
""
else
"-c --stdout > #{to}"
end

"bzip2 #{from} #{to}"
end

def decompress(from, to = nil)
from = "-f #{from}" unless from == "-"

to = case to
when "-"
"-c --stdout"
when nil
""
else
"-c --stdout > #{to}"
end

"bunzip2 -f #{from} #{to}"
end

end
end
end
40 changes: 40 additions & 0 deletions lib/capistrano-db-tasks/compressors/gzip.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Compressors
class Gzip < Base

class << self

def file_extension
"gz"
end

def compress(from, to = nil)
from = from == :stdin ? "-" : from
to = case to
when '-'
"-c --stdout"
when nil
""
else
"-c --stdout > #{to}"
end

"gzip #{from} #{to}"
end

def decompress(from, to = nil)
from = from == :stdin ? "-" : from
to = case to
when :stdout
"-c --stdout"
when nil
""
else
"-c --stdout > #{to}"
end

"gzip -d #{from} #{to}"
end

end
end
end
26 changes: 18 additions & 8 deletions lib/capistrano-db-tasks/database.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Database
class Base

attr_accessor :config, :output_file

def initialize(cap_instance)
@cap = cap_instance
end
Expand Down Expand Up @@ -41,13 +43,21 @@ def current_time
end

def output_file
@output_file ||= "db/#{database}_#{current_time}.sql.bz2"
@output_file ||= "db/#{database}_#{current_time}.sql.#{compressor.file_extension}"
end

def pgpass
"PGPASSWORD='#{@config['password']}'" if @config['password']
end

def compressor
@compressor ||= begin
compressor_klass = @cap.fetch(:compressor).to_s.split('_').collect(&:capitalize).join
klass = Object.module_eval("::Compressors::#{compressor_klass}", __FILE__, __LINE__)
klass
end
end

private

def dump_cmd
Expand Down Expand Up @@ -77,7 +87,7 @@ def initialize(cap_instance)
end

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

Expand All @@ -95,9 +105,9 @@ def clean_dump_if_needed

# cleanup = true removes the mysqldump file after loading, false leaves it in db/
def load(file, cleanup)
unzip_file = File.join(File.dirname(file), File.basename(file, '.bz2'))
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} && bunzip2 -f #{file} && RAILS_ENV=#{@cap.fetch(:rails_env)} && #{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

Expand All @@ -117,10 +127,10 @@ def initialize(cap_instance)

# cleanup = true removes the mysqldump file after loading, false leaves it in db/
def load(file, cleanup)
unzip_file = File.join(File.dirname(file), File.basename(file, '.bz2'))
unzip_file = File.join(File.dirname(file), File.basename(file, ".#{compressor.file_extension}"))
# system("bunzip2 -f #{file} && bundle exec rake db:drop db:create && #{import_cmd(unzip_file)} && bundle exec rake db:migrate")
@cap.info "executing local: bunzip2 -f #{file} && #{import_cmd(unzip_file)}"
system("bunzip2 -f #{file} && #{import_cmd(unzip_file)}")
@cap.info "executing local: #{compressor.decompress(file)}" && #{import_cmd(unzip_file)}"
system("#{compressor.decompress(file)} && #{import_cmd(unzip_file)}")
if cleanup
@cap.info "removing #{unzip_file}"
File.unlink(unzip_file)
Expand All @@ -131,7 +141,7 @@ def load(file, cleanup)
end

def dump
system "#{dump_cmd} | bzip2 - - > #{output_file}"
system "#{dump_cmd} | #{compressor.compress('-', output_file)}"
self
end

Expand Down
6 changes: 5 additions & 1 deletion lib/capistrano-db-tasks/dbtasks.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require File.expand_path("#{File.dirname(__FILE__)}/util")
require File.expand_path("#{File.dirname(__FILE__)}/database")
require File.expand_path("#{File.dirname(__FILE__)}/asset")
require File.expand_path("#{File.dirname(__FILE__)}/compressors/base")
require File.expand_path("#{File.dirname(__FILE__)}/compressors/bzip2")
require File.expand_path("#{File.dirname(__FILE__)}/compressors/gzip")

set :local_rails_env, ENV['RAILS_ENV'] || 'development' unless fetch(:local_rails_env)
set :rails_env, fetch(:stage) || 'production' unless fetch(:rails_env)
Expand All @@ -9,6 +12,7 @@
set :local_assets_dir, 'public' unless fetch(:local_assets_dir)
set :skip_data_sync_confirm, (ENV['SKIP_DATA_SYNC_CONFIRM'].to_s.downcase == 'true')
set :disallow_pushing, false unless fetch(:disallow_pushing)
set :compressor, :gzip unless fetch(:compressor)

namespace :capistrano_db_tasks do
task :check_can_push do
Expand Down Expand Up @@ -117,4 +121,4 @@

desc 'Synchronize your remote assets AND database using local assets and database'
task :push => "app:remote:sync"
end
end