Module: Capistrano::Postgresql::HelperMethods

Defined in:
lib/capistrano/postgresql/helper_methods.rb

Instance Method Summary collapse

Instance Method Details

#ask_for_or_generate_passwordObject

This method is invoked only if ‘:postgresql_password` is not already set in `config/deploy.rb`. Directly setting `:postgresql_password` has precedence.



20
21
22
23
24
25
26
# File 'lib/capistrano/postgresql/helper_methods.rb', line 20

def ask_for_or_generate_password
  if fetch(:postgresql_ask_for_password)
    ask :postgresql_password, "Postgresql database password for the app: "
  else
    set :postgresql_password, generate_random_password
  end
end

#create_database(db_name, user_name) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/capistrano/postgresql/helper_methods.rb', line 55

def create_database(db_name, user_name)
  if psql "-c", %Q{"CREATE database #{db_name} owner #{user_name};"}
    info "postgresql: database '#{db_name}' created"
  else
    error "postgresql: creating database '#{db_name}' failed!"
    exit 1
  end
end

#create_db_user(name, password) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/capistrano/postgresql/helper_methods.rb', line 36

def create_db_user(name, password)
  if psql "-c", %Q{"CREATE user #{name} WITH password '#{password}';"}
    info "postgresq: database user '#{name}' created"
  else
    error "postgresql: creating database user failed!"
    exit 1
  end
end

#database_exists?(db_name) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/capistrano/postgresql/helper_methods.rb', line 51

def database_exists?(db_name)
  psql "-tAc", %Q{"SELECT 1 FROM pg_database WHERE datname='#{db_name}';" | grep -q 1}
end

#database_yml_template(template_name, target) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/capistrano/postgresql/helper_methods.rb', line 8

def database_yml_template(template_name, target)
  config_file = "#{fetch(:postgresql_templates_path)}/#{template_name}"
  # If there's no customized file in your rails app template directory,
  # proceed with the default.
  unless File.exists?(config_file)
    config_file = File.join(File.dirname(__FILE__), "../../generators/capistrano/postgresql/templates/#{template_name}")
  end
  upload! StringIO.new(ERB.new(File.read(config_file)).result(binding)), target
end

#db_user_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/capistrano/postgresql/helper_methods.rb', line 32

def db_user_exists?(name)
  psql "-tAc", %Q{"SELECT 1 FROM pg_roles WHERE rolname='#{name}';" | grep -q 1}
end

#ensure_database_created(db_name, user_name) ⇒ Object



64
65
66
67
68
# File 'lib/capistrano/postgresql/helper_methods.rb', line 64

def ensure_database_created(db_name, user_name)
  unless database_exists?(db_name)
    create_database(db_name, user_name)
  end
end

#ensure_db_user_created(name, password) ⇒ Object



45
46
47
48
49
# File 'lib/capistrano/postgresql/helper_methods.rb', line 45

def ensure_db_user_created(name, password)
  unless db_user_exists?(name)
    create_db_user(name, password)
  end
end

#generate_random_passwordObject



28
29
30
# File 'lib/capistrano/postgresql/helper_methods.rb', line 28

def generate_random_password
  SecureRandom.hex(10)
end

#psql(*args) ⇒ Object

returns true or false depending on the remote command exit status



71
72
73
# File 'lib/capistrano/postgresql/helper_methods.rb', line 71

def psql(*args)
  test :sudo, "-u postgres psql", *args
end

#remote_file_exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/capistrano/postgresql/helper_methods.rb', line 75

def remote_file_exists?(path)
  test "[ -e #{path} ]"
end