Module: Capistrano::Postgresql::PsqlHelpers

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

Instance Method Summary collapse

Instance Method Details

#database_exists?Boolean

Returns:

  • (Boolean)


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

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)


19
20
21
# File 'lib/capistrano/postgresql/psql_helpers.rb', line 19

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

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



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

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