Module: Capistrano::Postgresql::PsqlHelpers

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

Instance Method Summary collapse

Instance Method Details

#database_exists?Boolean

Returns:

  • (Boolean)


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

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

#database_user_exists?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/capistrano/postgresql/psql_helpers.rb', line 22

def database_user_exists?
  psql 'test', fetch(:pg_system_db),'-tAc', %Q{"SELECT 1 FROM pg_roles WHERE rolname='#{fetch(:pg_username)}';" | grep -q 1}
end

#database_user_password_different?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/capistrano/postgresql/psql_helpers.rb', line 26

def database_user_password_different?
  current_password_md5 = psql 'capture', fetch(:pg_system_db),'-tAc', %Q{"select passwd from pg_shadow WHERE usename='#{fetch(:pg_username)}';"}
  new_password_md5 = "md5#{Digest::MD5.hexdigest("#{fetch(:pg_password)}#{fetch(:pg_username)}")}"
  current_password_md5 == new_password_md5 ? false : true
end

#psql(type, database, *args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/capistrano/postgresql/psql_helpers.rb', line 5

def psql(type, database, *args)
  if fetch(:pg_without_sudo)
    # Add the :pg_system_user to psql command since we aren't using sudo anymore
    cmd = [ :psql, "-d #{database}", *args.unshift("-U #{fetch(:pg_system_user)}") ]
  else
    cmd = [:sudo, "-i -u #{fetch(:pg_system_user)}", *cmd]
  end
  # Allow us to execute the different sshkit commands
  if type == 'test'
    test *cmd
  elsif type == 'capture'
    capture *cmd
  else
    execute *cmd
  end
end