Class: DatabaseFork
Constant Summary
collapse
- DEFAULTS =
{
'check_branch_name_regex' => '^feature_',
'ignore' => [],
'environments' => %w(development test)
}
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Commands
#execute_commands, #record_command, #reset_commands!
Methods included from Logging
#log_debug, #log_info
Constructor Details
#initialize(root_dir, logger = Logger.new(STDOUT)) ⇒ DatabaseFork
use DatabaseFork.new.run in your post-checkout hook
33
34
35
36
37
38
39
|
# File 'lib/database_fork.rb', line 33
def initialize(root_dir, logger = Logger.new(STDOUT))
@root_dir = root_dir
@config_file = File.join(@root_dir, '.db_forks.yml')
@logger = logger
reset_commands!
end
|
Class Method Details
.reset_all_environments!(root_dir, logger = Logger.new(STDOUT)) ⇒ Object
25
26
27
28
|
# File 'lib/database_fork.rb', line 25
def reset_all_environments!(root_dir, logger = Logger.new(STDOUT))
logger.info 'removing DATABASE_FORK_* files'
FileUtils.rm Dir[File.join(root_dir, 'tmp', 'DATABASE_FORK_*')]
end
|
.setup_env(env, root_dir) ⇒ Object
call this at the end of your application.rb file
17
18
19
20
21
22
23
|
# File 'lib/database_fork.rb', line 17
def setup_env(env, root_dir)
db_fork_var = "DATABASE_FORK_#{env}".upcase
db_fork_file = File.join(root_dir, 'tmp', db_fork_var)
if File.exists?(db_fork_file)
ENV[db_fork_var] = open(db_fork_file).read.strip
end
end
|
Instance Method Details
#app_connection ⇒ Object
79
80
81
82
|
# File 'lib/database_fork.rb', line 79
def app_connection
@database_config ||= LoadDatabaseConfig.new(@root_dir)
@database_config.config
end
|
#ask_user(question) ⇒ Object
74
75
76
77
|
# File 'lib/database_fork.rb', line 74
def ask_user(question)
log_info question
IO.new(IO.sysopen('/dev/tty'), 'r').gets.chomp
end
|
#config ⇒ Object
94
95
96
|
# File 'lib/database_fork.rb', line 94
def config
@config ||= DEFAULTS.merge(YAML.load(open(@config_file).read)) rescue DEFAULTS
end
|
#current_branch ⇒ Object
84
85
86
|
# File 'lib/database_fork.rb', line 84
def current_branch
@current_branch ||= `git rev-parse --abbrev-ref HEAD`.strip.gsub('/', '_')
end
|
#run ⇒ Object
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
66
67
68
69
70
71
|
# File 'lib/database_fork.rb', line 41
def run
if Regexp.new(config['check_branch_name_regex']).match(current_branch)
log_info 'branch qualified for database forking'
config['environments'].each do |env|
adapter = MysqlFork.new(@root_dir, app_connection[env], env, current_branch, @logger)
if adapter.exists?
log_info "Database #{adapter.target_name} exists. Skipping."
adapter.export_env
else
case ask_user("Create database: '#{adapter.target_name}'? (y(es), n(no), enter=ignore)")
when 'y'
adapter.fork
adapter.export_env
when 'n'
adapter.reset_env
else
config['ignore'] << current_branch
adapter.reset_env
end
end
end
else
self.class.reset_all_environments!(@root_dir)
end
save_config
end
|
#save_config ⇒ Object
98
99
100
101
102
|
# File 'lib/database_fork.rb', line 98
def save_config
File.open(@config_file, 'w') do |f|
f.puts @config.to_yaml
end
end
|