This repository has been archived by the owner on Oct 26, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdb.rb
52 lines (45 loc) · 1.65 KB
/
db.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
# encoding: utf-8
#
# Tasks:
#
# - vlad:db:create
# - vlad:db:setup
# - vlad:db:seed
# - vlad:db:rollback
# - vlad:db:drop
# - vlad:db:clone
require 'vlad/rails'
namespace :vlad do
namespace :db do
desc 'Clone the remote database into the local database'
task :clone => :environment do |t, args|
puts "[Database] Cloning #{rails_env} into development database"
# check adapters
loc = ActiveRecord::Base.configurations['development']
rem = ActiveRecord::Base.configurations[rails_env]
adapter = loc['adapter'] == rem['adapter'] ? loc['adapter'] : nil
raise "Development and #{rails_env} adapters must be similar" unless adapter
# perform
case adapter
when 'mysql', 'mysql2'
rem_cmd = "mysqldump --add-drop-table #{VladExtras::Database.mysql_config(rem)}"
loc_cmd = "mysql #{VladExtras::Database.mysql_config(loc)}"
when 'postgresql'
rem_cmd = "pg_dump -cxO #{VladExtras::Database.psql_config(rem)}"
loc_cmd = "psql #{VladExtras::Database.psql_config(loc)}"
else
puts "Unsupported database adapter: #{adapter}"
end
target_host = Rake::RemoteTask.hosts_for(:app).first
system "ssh #{target_host} \"cd #{current_path}; #{rem_cmd}\" | #{loc_cmd}"
end
%w(create drop rollback seed setup).each do |task|
desc "#{task.capitalize} database"
remote_task task.to_sym, :roles => :app do
break unless target_host == Rake::RemoteTask.hosts_for(:app).first
puts "[Database] #{task.capitalize} database"
run "cd #{current_path} && RAILS_ENV=#{rails_env} #{rake_cmd} db:#{task}"
end
end
end
end