Module: Capistrano::Postgresql::HelperMethods

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

Instance Method Summary collapse

Instance Method Details

#archetype_database_yml_fileObject

location of archetypal database.yml file created on primary db role when user and database are first created



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

def archetype_database_yml_file
  raise(":deploy_to in your app/config/deploy/#{fetch(:rails_env)}.rb file cannot contain ~") if shared_path.to_s.include?('~') # issues/27
  deploy_path.join('db/database.yml')
end

#database_yml_fileObject

location of database.yml file on clients



67
68
69
70
# File 'lib/capistrano/postgresql/helper_methods.rb', line 67

def database_yml_file
  raise(":deploy_to in your app/config/deploy/#{fetch(:rails_env)}.rb file cannot contain ~") if shared_path.to_s.include?('~') # issues/27
  shared_path.join('config/database.yml')
end

#extension_exists?(extension) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/capistrano/postgresql/helper_methods.rb', line 7

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

#generate_database_yml_io(password = fetch(:pg_password)) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/capistrano/postgresql/helper_methods.rb', line 23

def generate_database_yml_io(password=fetch(:pg_password))
  StringIO.open do |s|
    s.puts "#{fetch(:pg_env)}:"
    {
        adapter: 'postgresql',
        encoding: fetch(:pg_encoding),
        database: fetch(:pg_database),
        pool: fetch(:pg_pool),
        username: fetch(:pg_username),
        password: password,
        host: fetch(:pg_host),
        socket: fetch(:pg_socket),
        port: fetch(:pg_port),
        timeout: fetch(:pg_timeout)
    }.each { |option_name,option_value| s.puts "  #{option_name}: #{option_value}" } # Yml does not support tabs. There are two spaces leading the config option line
    s.string
  end
end

#pg_template(update = false, archetype_file = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/capistrano/postgresql/helper_methods.rb', line 42

def pg_template(update=false,archetype_file=nil)
  config_file = "#{fetch(:pg_templates_path)}/postgresql.yml.erb"
  if update
    raise('Regeneration of archetype database.yml need the original file to update from.') if archetype_file.nil?
    raise('Cannot update a custom postgresql.yml.erb file.') if File.exists?(config_file) # Skip custom postgresql.yml.erb if we're updating. It's not supported
    # Update yml file from settings
    if fetch(:pg_generate_random_password) || !fetch(:pg_password) # We need to prevent updating the archetype file if we've done a random or "ask"ed password
      current_password = archetype_file.split("\n").grep(/password/)[0].split('password:')[1].strip
      generate_database_yml_io(current_password)
    else
      generate_database_yml_io
    end
  else
    if File.exists?(config_file) # If there is a customized file in your rails app template directory, use it and convert any ERB
      StringIO.new ERB.new(File.read(config_file)).result(binding)
    else # Else there's no customized file in your rails app template directory, proceed with the default.
      # Build yml file from settings
      ## We build the file line by line to avoid overwriting existing files
      generate_database_yml_io
    end
  end

end

#remove_extensionsObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/capistrano/postgresql/helper_methods.rb', line 11

def remove_extensions
  if Array( fetch(:pg_extensions) ).any?
    on roles :db do
      # remove in reverse order if extension is present
      Array( fetch(:pg_extensions) ).reverse.each do |ext|
        next if [nil, false, ""].include?(ext)
        psql 'execute', fetch(:pg_database), '-c', %Q{"DROP EXTENSION IF EXISTS #{ext};"} if extension_exists?(ext)
      end
    end
  end
end