forked from rafaelsales/capistrano-db_sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.rb
65 lines (50 loc) · 1.83 KB
/
configuration.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
53
54
55
56
57
58
59
60
61
62
63
64
65
require 'tmpdir'
require 'active_support/core_ext/hash'
module Capistrano::DBSync
class Configuration
extend Forwardable
DEFAULT_OPTIONS = ->(cap) do
{
# Hash mapping a table name to a query or nil in case no data is wanted for a table.
# E.g.: {
# posts: "SELECT * FROM posts WHERE created_at > NOW() - interval '60 days'",
# comments: "SELECT * FROM comments WHERE created_at > NOW() - interval '30 days'",
# likes: nil
# }
data_selection: {},
data_sync_confirmation: true, # Ask for user input confirmation
local: {
cleanup: false, # If the downloaded dump directory should be removed after restored
pg_jobs: 1, # Number of jobs to run in parallel on pg_restore
working_dir: Dir.tmpdir,
env: ENV.fetch('RAILS_ENV', 'development'),
},
remote: {
cleanup: true, # If the remote dump directory should be removed after downloaded
working_dir: cap.fetch(:tmp_dir, "/tmp"),
env: cap.fetch(:stage).to_s,
},
}
end
def initialize(cap_instance = Capistrano.env)
@cap = cap_instance
@options = load_options
end
def load_options
user_options = cap.fetch(:db_sync_options)
user_options = user_options.reject { |_, v| v.nil? }
DEFAULT_OPTIONS.call(cap).deep_merge(user_options)
end
def data_sync_confirmed?
skip = options[:data_sync_confirmation].to_s.downcase == "false"
skip || prompt("Confirm replace local database with remote database?")
end
def_delegators :@options, :[], :fetch
private
attr_reader :cap, :options
def prompt(message, prompt = "(y)es, (n)o")
cap.ask(:prompt_answer, "#{message} #{prompt}")
(cap.fetch(:prompt_answer) =~ /^y|yes$/i) == 0
end
end
end