Class: Nvoi::External::Database::Postgres
- Inherits:
-
Provider
- Object
- Provider
- Nvoi::External::Database::Postgres
show all
- Defined in:
- lib/nvoi/external/database/postgres.rb
Overview
PostgreSQL provider using pg_dump/psql via kubectl exec
Instance Method Summary
collapse
Methods inherited from Provider
#extension, #needs_container?
Instance Method Details
#app_env(creds, host:) ⇒ Object
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/nvoi/external/database/postgres.rb', line 29
def app_env(creds, host:)
{
"DATABASE_URL" => build_url(creds, host:),
"POSTGRES_HOST" => host,
"POSTGRES_PORT" => creds.port,
"POSTGRES_USER" => creds.user,
"POSTGRES_PASSWORD" => creds.password,
"POSTGRES_DB" => creds.database
}
end
|
#build_url(creds, host: nil) ⇒ Object
16
17
18
19
|
# File 'lib/nvoi/external/database/postgres.rb', line 16
def build_url(creds, host: nil)
h = host || creds.host
"postgresql://#{creds.user}:#{creds.password}@#{h}:#{creds.port}/#{creds.database}"
end
|
#container_env(creds) ⇒ Object
21
22
23
24
25
26
27
|
# File 'lib/nvoi/external/database/postgres.rb', line 21
def container_env(creds)
{
"POSTGRES_USER" => creds.user,
"POSTGRES_PASSWORD" => creds.password,
"POSTGRES_DB" => creds.database
}
end
|
#create_database(ssh, opts) ⇒ Object
72
73
74
75
76
77
78
|
# File 'lib/nvoi/external/database/postgres.rb', line 72
def create_database(ssh, opts)
cmd = "kubectl exec -n default #{opts.pod_name} -- " \
"psql -U #{opts.user} -c \"CREATE DATABASE #{opts.database}\""
ssh.execute(cmd)
rescue Errors::SshCommandError => e
raise Errors::DatabaseError.new("create_database", "failed to create database: #{e.message}")
end
|
#default_port ⇒ Object
8
9
10
|
# File 'lib/nvoi/external/database/postgres.rb', line 8
def default_port
"5432"
end
|
#dump(ssh, opts) ⇒ Object
40
41
42
43
44
45
46
|
# File 'lib/nvoi/external/database/postgres.rb', line 40
def dump(ssh, opts)
cmd = "kubectl exec -n default #{opts.pod_name} -- " \
"pg_dump -U #{opts.user} -d #{opts.database} --no-owner --no-acl"
ssh.execute(cmd)
rescue Errors::SshCommandError => e
raise Errors::DatabaseError.new("dump", "pg_dump failed: #{e.message}")
end
|
#parse_url(url) ⇒ Object
12
13
14
|
# File 'lib/nvoi/external/database/postgres.rb', line 12
def parse_url(url)
parse_standard_url(url, default_port)
end
|
#restore(ssh, data, opts) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/nvoi/external/database/postgres.rb', line 48
def restore(ssh, data, opts)
create_database(ssh, Types::CreateOptions.new(
pod_name: opts.pod_name,
database: opts.database,
user: opts.user,
password: opts.password
))
temp_file = "/tmp/restore_#{opts.database}.sql"
write_cmd = "cat > #{temp_file} << 'SQLDUMP'\n#{data}\nSQLDUMP"
ssh.execute(write_cmd)
ssh.execute("kubectl cp #{temp_file} default/#{opts.pod_name}:#{temp_file}")
restore_cmd = "kubectl exec -n default #{opts.pod_name} -- " \
"psql -U #{opts.user} -d #{opts.database} -f #{temp_file}"
ssh.execute(restore_cmd)
ssh.execute_ignore_errors("rm -f #{temp_file}")
ssh.execute_ignore_errors("kubectl exec -n default #{opts.pod_name} -- rm -f #{temp_file}")
rescue Errors::SshCommandError => e
raise Errors::DatabaseError.new("restore", "psql restore failed: #{e.message}")
end
|