-
Notifications
You must be signed in to change notification settings - Fork 38
/
utils.sh.erb
75 lines (63 loc) · 2.28 KB
/
utils.sh.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash -exu
source /var/vcap/jobs/postgres/bin/pgconfig.sh
function pgexec() {
local database
database="${1}"
local command
command="${2}"
"${PACKAGE_DIR}/bin/psql" \
-U "vcap" \
-p "${PORT}" \
-d "${database}" \
-c "${command}"
}
function is_major() {
[ "${pgversion_current%.*}" != "${pgversion_upgrade_from%.*}" ]
}
function create_roles() {
echo "Creating roles..."
<% p("databases.roles", []).each do |role| %>
echo "Trying to create role <%= role["name"] %>..."
set +e
# TODO remove unused roles automatically
# Default permissions are: nosuperuser nologin inherit nocreatedb.
# Will fail if role already exists, which is OK
pgexec postgres "CREATE ROLE \"<%= role["name"] %>\" WITH LOGIN"
set -e
<% if role["password"] %>
echo "Setting password for role <%= role["name"] %>..."
pgexec postgres "ALTER ROLE \"<%= role["name"] %>\" WITH LOGIN PASSWORD '<%= role["password"] %>'"
<% end %>
<% if role["permissions"] %>
echo "Adding permissions <%= role["permissions"].join(' ') %> for role <%= role["name"] %>..."
pgexec postgres "ALTER ROLE \"<%= role["name"] %>\" WITH <%= role["permissions"].join(' ') %>"
<% end %>
<% end %>
}
function create_databases() {
echo "Creating databases..."
<% p("databases.databases", []).each do |database| %>
echo "Trying to create database <%= database["name"] %>..."
set +e
"${PACKAGE_DIR}/bin/createdb" "<%= database["name"] %>" -p ${PORT}
set -e
<% if database["citext"] %>
echo "Trying to install citext..."
set +e
pgexec "<%= database["name"] %>" "CREATE EXTENSION citext"
pgexec "<%= database["name"] %>" "CREATE EXTENSION citext FROM UNPACKAGED"
set -e
<% end %>
echo "Enabling pgcrypto extension..."
pgexec "<%= database["name"] %>" "CREATE EXTENSION IF NOT EXISTS pgcrypto"
<% if p("databases.collect_statement_statistics") %>
echo "Enabling pg_stat_statements extension..."
pgexec "<%= database["name"] %>" "CREATE EXTENSION IF NOT EXISTS pg_stat_statements"
<% end %>
<% if database["run_on_every_startup"] %>
<% database["run_on_every_startup"].each do |query| %>
pgexec "<%= database["name"] %>" "<%= query %>"
<% end %>
<% end %>
<% end %>
}