Module: DbVcs::Utils

Defined in:
lib/db_vcs/utils.rb

Class Method Summary collapse

Class Method Details

.current_branchString

Returns current branch name.

Returns:

  • (String)

    current branch name



7
8
9
# File 'lib/db_vcs/utils.rb', line 7

def current_branch
  `git rev-parse --abbrev-ref HEAD`.chomp
end

.db_name(environment, branch) ⇒ String

Generate db name, based on branch name and environment.

Parameters:

  • environment (String)

    application’s environment name. E.g. “development”, “test”

  • branch (String)

Returns:

  • (String)


20
21
22
23
24
# File 'lib/db_vcs/utils.rb', line 20

def db_name(environment, branch)
  [DbVcs.config.db_basename, environment, branch].map do |str|
    normalize_db_part(str)
  end.join("_")
end

.git_branchesArray<String>

Returns array of local branches names.

Returns:

  • (Array<String>)

    array of local branches names



12
13
14
# File 'lib/db_vcs/utils.rb', line 12

def git_branches
  `git for-each-ref refs/heads --format='%(refname:short)'`.scan(/[[:graph:]]+/)
end

.normalize_db_part(str) ⇒ String

Removes special characters from string that is used as a part of database name

Parameters:

  • str (String)

Returns:

  • (String)


29
30
31
# File 'lib/db_vcs/utils.rb', line 29

def normalize_db_part(str)
  str.gsub(/[\W]/, "_")
end

.resolve_exec_path(exec, fallback_exec: nil) ⇒ String

Returns path to executable.

Parameters:

  • exec (String)

    a name of executable

  • fallback_exec (String) (defaults to: nil)

    a name of executable to fallback to if exec was not resolved

Returns:

  • (String)

    path to executable



36
37
38
39
40
41
# File 'lib/db_vcs/utils.rb', line 36

def resolve_exec_path(exec, fallback_exec: nil)
  path = `which #{exec}`.chomp
  return resolve_exec_path(fallback_exec) if fallback_exec && path.empty?

  path.empty? ? exec : path
end