Skip to content

Commit 676537c

Browse files
authored
MG-4: Add OpenELIS db setup script and update PostgreSQL init process (#226)
1 parent 0fbe635 commit 676537c

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

distro/data/postgresql/create_db.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@
55
# Workaround for PostgreSQL Docker image not running files from sub-dirs.
66
# https://github.com/docker-library/postgres/issues/605#issuecomment-567236795
77
#
8+
# Only execute shell scripts (*.sh) found in the subdirectories so that
9+
# SQL files are not interpreted by the shell (they should be applied via
10+
# psql from the shell scripts that know how to apply them).
811
#
912
set -e
1013
directory=/docker-entrypoint-initdb.d/db/
1114
if [ -d "${directory}" ]
1215
then
13-
find ${directory}* -type f -execdir ls {} \; -execdir bash {} \;
16+
# Find and execute only shell scripts. execdir runs the command from the
17+
# directory where the matched file is located, which preserves relative
18+
# paths used by the scripts.
19+
find "${directory}" -type f -name '*.sh' -execdir ls {} \; -execdir bash {} \;
1420
else
1521
echo "Directory '${directory}' does not exist. No database to create. Exit 0."
1622
fi
17-
exit 0
23+
exit 0
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
echo "Setting up OpenELIS database, users and loading schema..."
5+
6+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7+
8+
echo "Using script dir: ${SCRIPT_DIR}"
9+
10+
export db_name=$OPENELIS_DB_NAME
11+
export db_schema=$OPENELIS_DB_SCHEMA
12+
export db_username=$OPENELIS_DB_USER
13+
export db_password=$OPENELIS_DB_PASSWORD
14+
export db_admin_password=$OPENELIS_DB_ADMIN_PASSWORD
15+
16+
createuser "${db_username}"
17+
createdb "${db_name}"
18+
19+
psql -d "${db_name}" -c "alter user ${db_username} with password '${db_password}';"
20+
psql -d "${db_name}" -c "grant all privileges on database ${db_name} to ${db_username};"
21+
psql -d "${db_name}" -c "create schema ${db_schema};"
22+
psql -d "${db_name}" -c "alter schema ${db_schema} owner to ${db_username};"
23+
24+
# Assign ownership of the database to the created user
25+
psql -d "${db_name}" -c "ALTER DATABASE ${db_name} OWNER TO ${db_username};"
26+
echo "Database '${db_name}' ownership assigned to user '${db_username}'."
27+
28+
echo "Database '${db_name}' and user '${db_username}' created."
29+
30+
# create a superuser for the database
31+
psql -d "${db_name}" -c "create user admin with superuser;"
32+
psql -d "${db_name}" -c "alter user admin with password '${db_admin_password}';"
33+
echo "Superuser 'admin' created."
34+
35+
# Path to SQL files
36+
OPENELIS_SQL="${SCRIPT_DIR}/OpenELIS-Global.sql"
37+
SITEINFO_SQL="${SCRIPT_DIR}/siteInfo.sql"
38+
39+
# Ensure files exist
40+
if [[ ! -f "${OPENELIS_SQL}" ]]; then
41+
echo "ERROR: ${OPENELIS_SQL} not found" >&2
42+
exit 1
43+
fi
44+
if [[ ! -f "${SITEINFO_SQL}" ]]; then
45+
echo "ERROR: ${SITEINFO_SQL} not found" >&2
46+
exit 1
47+
fi
48+
49+
if [[ -f "${OPENELIS_SQL}" ]]; then
50+
awk '{
51+
orig=$0
52+
lo=tolower($0)
53+
if (lo ~ /^[[:space:]]*create[[:space:]]+schema[[:space:]]+clinlims[[:space:]]*;[[:space:]]*$/ && lo !~ /if[[:space:]]+not[[:space:]]+exists/) {
54+
if (orig !~ /^[[:space:]]*--/) {
55+
print "-- " orig
56+
next
57+
}
58+
}
59+
print
60+
}' "${OPENELIS_SQL}" > "${OPENELIS_SQL}.tmp"
61+
62+
if ! cmp -s "${OPENELIS_SQL}" "${OPENELIS_SQL}.tmp"; then
63+
echo "Backing up ${OPENELIS_SQL} to ${OPENELIS_SQL}.bak"
64+
cp "${OPENELIS_SQL}" "${OPENELIS_SQL}.bak"
65+
mv "${OPENELIS_SQL}.tmp" "${OPENELIS_SQL}"
66+
echo "Commented out plain CREATE SCHEMA clinlims; in ${OPENELIS_SQL}"
67+
else
68+
rm "${OPENELIS_SQL}.tmp"
69+
echo "No plain 'CREATE SCHEMA clinlims;' found or already commented/has IF NOT EXISTS; no change."
70+
fi
71+
fi
72+
73+
# Load schema/data into the newly created database
74+
echo "Loading ${OPENELIS_SQL} into ${db_name}..."
75+
psql -v ON_ERROR_STOP=1 --username admin --dbname "$db_name" -f "${OPENELIS_SQL}"
76+
77+
echo "Loading ${SITEINFO_SQL} into ${db_name}..."
78+
psql -v ON_ERROR_STOP=1 --username admin --dbname "$db_name" -f "${SITEINFO_SQL}"
79+
80+
echo "SQL files imported successfully."

0 commit comments

Comments
 (0)