class DatabaseFork
Constants
- DEFAULTS
Public Class Methods
new(root_dir, logger = Logger.new(STDOUT))
click to toggle source
use DatabaseFork.new
.run in your post-checkout hook
# 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
reset_all_environments!(root_dir, logger = Logger.new(STDOUT))
click to toggle source
# 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_*')], force: true end
setup_env(env, root_dir)
click to toggle source
call this at the end of your application.rb file
# 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
Public Instance Methods
app_connection()
click to toggle source
# File lib/database_fork.rb, line 87 def app_connection @database_config ||= LoadDatabaseConfig.new(@root_dir) @database_config.config end
ask_user(question)
click to toggle source
# File lib/database_fork.rb, line 82 def ask_user(question) log_info question IO.new(IO.sysopen('/dev/tty'), 'r').gets.chomp end
config()
click to toggle source
# File lib/database_fork.rb, line 102 def config @config ||= DEFAULTS.merge(YAML.load(open(@config_file).read)) rescue DEFAULTS end
current_branch()
click to toggle source
# File lib/database_fork.rb, line 92 def current_branch @current_branch ||= `git rev-parse --abbrev-ref HEAD`.strip.gsub('/', '_') end
ignored_branch?(branch_name)
click to toggle source
# File lib/database_fork.rb, line 77 def ignored_branch?(branch_name) config['ignore'] ||= [] config['ignore'].include?(branch_name) end
qualified_branch?(branch_name)
click to toggle source
# File lib/database_fork.rb, line 73 def qualified_branch?(branch_name) Regexp.new(config['check_branch_name_regex']).match(branch_name) end
run()
click to toggle source
# File lib/database_fork.rb, line 41 def run if qualified_branch?(current_branch) && !ignored_branch?(current_branch) log_info 'branch qualified for database forking' config['environments'].each do |env| # TODO: chose adapter based on connection information (Rails database.yml "adapter") 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()
click to toggle source
# File lib/database_fork.rb, line 106 def save_config config['ignore'].uniq! File.open(@config_file, 'w') do |f| f.puts @config.to_yaml end end