Class: DatabaseFork

Inherits:
Object
  • Object
show all
Includes:
Commands, Logging
Defined in:
lib/database_fork.rb

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_*')], force: true
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_connectionObject



87
88
89
90
# File 'lib/database_fork.rb', line 87

def app_connection
  @database_config ||= LoadDatabaseConfig.new(@root_dir)
  @database_config.config
end

#ask_user(question) ⇒ Object



82
83
84
85
# File 'lib/database_fork.rb', line 82

def ask_user(question)
  log_info question
  IO.new(IO.sysopen('/dev/tty'), 'r').gets.chomp
end

#configObject



102
103
104
# File 'lib/database_fork.rb', line 102

def config
  @config ||= DEFAULTS.merge(YAML.load(open(@config_file).read)) rescue DEFAULTS
end

#current_branchObject



92
93
94
# 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) ⇒ Boolean



77
78
79
80
# File 'lib/database_fork.rb', line 77

def ignored_branch?(branch_name)
  config['ignore'] ||= []
  config['ignore'].include?(branch_name)
end

#qualified_branch?(branch_name) ⇒ Boolean



73
74
75
# File 'lib/database_fork.rb', line 73

def qualified_branch?(branch_name)
  Regexp.new(config['check_branch_name_regex']).match(branch_name)
end

#runObject



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 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_configObject



106
107
108
109
110
111
# 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