class Worktree::Feature::CloneDbs

Public Class Methods

new(project_dir:, branch:) click to toggle source
# File lib/worktree/feature/clone_dbs.rb, line 9
def initialize(project_dir:, branch:)
  @project_dir = project_dir
  @branch = branch
end

Public Instance Methods

run!() click to toggle source
# File lib/worktree/feature/clone_dbs.rb, line 14
def run!
  @db_manager = DbManager.new("#{@project_dir}/#{@branch}/config/database.yml")
  @db_manager.createdb!(db_name)

  write!
rescue StandardError => e
  # bypass error
  Worktree.logger.error { e.message }
end

Private Instance Methods

db_name() click to toggle source
# File lib/worktree/feature/clone_dbs.rb, line 37
def db_name
  # db name cannot be > 63 bytes
  db_suffix = '_development'
  max_db_prefix_length = 62 - db_suffix.length
  db_prefix = @branch[0..max_db_prefix_length]
  "#{db_prefix}#{db_suffix}"
end
write!() click to toggle source
# File lib/worktree/feature/clone_dbs.rb, line 26
def write!
  new_spec = @db_manager.spec.dup
  if @db_manager.multi?
    new_spec['development']['primary']['database'] = db_name
  else
    new_spec['development']['database'] = db_name
  end
  # write changed database config back
  File.write("#{@project_dir}/#{@branch}/config/database.yml", new_spec.to_yaml)
end