Class: BranchDb::Switcher

Inherits:
Object
  • Object
show all
Defined in:
lib/branch_db/switcher.rb

Direct Known Subclasses

MysqlSwitcher, PostgresqlSwitcher, SqliteSwitcher

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rails_env, config, branch, options = {}) ⇒ Switcher

Returns a new instance of Switcher.



26
27
28
29
30
# File 'lib/branch_db/switcher.rb', line 26

def initialize(rails_env, config, branch, options = {})
  @rails_env, @config, @branch = rails_env, config, branch
  @overwrite = options[:overwrite]
  @verbose = options[:verbose]
end

Class Method Details

.branches(rails_env, config) ⇒ Object



22
23
24
# File 'lib/branch_db/switcher.rb', line 22

def self.branches(rails_env, config)
  self.which(config).show_branches(rails_env, config)
end

.can_handle?(config) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



14
15
16
# File 'lib/branch_db/switcher.rb', line 14

def self.can_handle?(config)
  raise Error, "Subclasses of BranchDb::Switcher must implement #can_handle?(config)."
end

.create(rails_env, config, branch, options = {}) ⇒ Object



18
19
20
# File 'lib/branch_db/switcher.rb', line 18

def self.create(rails_env, config, branch, options = {})
  which(config).new(rails_env, config, branch, options)
end

.which(config) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/branch_db/switcher.rb', line 5

def self.which(config)
  switcher = switchers.detect { |sw| sw.can_handle?(config) }
  unless switcher
    $stderr.puts "Your database adapter (#{config['adapter']}) is not supported yet."
    switcher = self # double as null switcher
  end
  switcher
end

Instance Method Details

#copy_from(from_branch) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/branch_db/switcher.rb', line 68

def copy_from(from_branch)
  ensure_branch_db_exists!(from_branch)

  create_empty_database do
    puts "Copying data from #{from_branch}..."
    copy_database(from_branch, @branch)
  end
end

#create_empty_databaseObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/branch_db/switcher.rb', line 46

def create_empty_database
  db = branch_db(@branch)
  if branch_db_exists?(@branch)
    if !@overwrite
      $stderr.puts "Database #{db} exists already."
      return
    else
      puts "Dropping existing database #{db}..."
      drop_database(@branch)
    end
  end
  puts "Creating fresh database #{db}..."
  create_database(@branch)
  yield if block_given?
end

#currentObject



32
33
34
# File 'lib/branch_db/switcher.rb', line 32

def current
  # Must be implemented in subclasses.
end

#delete_databaseObject



62
63
64
65
66
# File 'lib/branch_db/switcher.rb', line 62

def delete_database
  ensure_branch_db_exists!(@branch)
  puts "Dropping existing database #{branch_db(@branch)}..."
  drop_database(@branch)
end

#exists?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/branch_db/switcher.rb', line 36

def exists?
  branch_db_exists?(@branch)
end

#switch!Object



40
41
42
43
44
# File 'lib/branch_db/switcher.rb', line 40

def switch!
  if exists?
    @config.replace(branch_config(@branch))
  end
end