Module: Orats::Postgres

Included in:
Common
Defined in:
lib/orats/postgres.rb

Overview

manage the postgres process

Instance Method Summary collapse

Instance Method Details

#create_databaseObject



12
13
14
15
16
17
18
# File 'lib/orats/postgres.rb', line 12

def create_database
  if local_postgres?
    run_rake 'db:create:all'
  else
    manually_create_postgres_db
  end
end

#drop_database(name) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/orats/postgres.rb', line 20

def drop_database(name)
  if local_postgres?
    run_rake 'db:drop:all'
  else
    manually_delete_postgres_db File.basename(name)
  end
end

#exit_if_database_existsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/orats/postgres.rb', line 37

def exit_if_database_exists
  task 'Check if the postgres database exists'

  # detect if the database already exists
  database = "#{File.basename(@target_path)}_development"
  return if run("#{postgres_bin} -d #{database} -l | " + \
                "grep #{database} | wc -l", capture: true).chomp == '0'

  error "'#{database}' database already exists",
        'attempt to check database existence'
  puts

  exit 1 unless yes?('Would you like to continue anyways? (y/N)',
                     :cyan)
end

#exit_if_postgres_unreachableObject



28
29
30
31
32
33
34
35
# File 'lib/orats/postgres.rb', line 28

def exit_if_postgres_unreachable
  task 'Check if you can connect to postgres'

  return if run("#{postgres_bin} -c 'select 1'")

  error 'Cannot connect to postgres', 'attempt to SELECT 1'
  exit 1
end

#postgres_bin(bin_name = 'psql') ⇒ Object



4
5
6
7
8
9
10
# File 'lib/orats/postgres.rb', line 4

def postgres_bin(bin_name = 'psql')
  exec = "#{bin_name} -h #{@options[:pg_location]} -U " + \
                  "#{@options[:pg_username]}"

  return exec if @options[:pg_password].empty?
  exec.prepend("PGPASSWORD=#{@options[:pg_password]} ")
end