-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DB mocks and sample RTM scenario (#183)
- Loading branch information
1 parent
2e8d7ad
commit 83c5eb6
Showing
21 changed files
with
1,112 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
-- cleanup for the sake idempotent run | ||
drop table if exists contact_history; | ||
drop table if exists blocked_list; | ||
drop table if exists client; | ||
drop table if exists pos; | ||
drop type if exists consent_enum; | ||
drop type if exists client_type_enum; | ||
|
||
create table pos | ||
( | ||
id SERIAL PRIMARY KEY, | ||
location_lat NUMERIC(10, 6) NOT NULL, | ||
location_lon NUMERIC(10, 6) NOT NULL, | ||
open_hour TIME NOT NULL, | ||
close_hour TIME NOT NULL | ||
); | ||
|
||
create type consent_enum AS ENUM ('SMS', 'EMAIL', 'PUSH', 'SMS_EMAIL', 'EMAIL_PUSH', 'SMS_PUSH', 'SMS_EMAIL_PUSH'); | ||
create type client_type_enum AS ENUM ('INDIVIDUAL', 'BUSINESS'); | ||
|
||
create table client | ||
( | ||
id SERIAL PRIMARY KEY, | ||
pos_id SERIAL REFERENCES pos(id) NOT NULL, | ||
msisdn CHAR(11), | ||
email VARCHAR(100), | ||
consents consent_enum, | ||
client_type client_type_enum NOT NULL | ||
); | ||
|
||
create table blocked_list( | ||
client_id INT PRIMARY KEY references client(id) | ||
); | ||
|
||
create table contact_history( | ||
id SERIAL PRIMARY KEY, | ||
client_id INT NOT NULL references client(id), | ||
event_time TIMESTAMP NOT NULL | ||
); | ||
|
||
---- POS | ||
insert into pos(id, location_lat, location_lon, open_hour, close_hour) VALUES (1, 52.237049, 21.017532, '00:00:00', '23:59:59'); | ||
insert into pos(id, location_lat, location_lon, open_hour, close_hour) VALUES (2, 50.049683, 19.944544, '08:00:00', '15:00:00'); | ||
insert into pos(id, location_lat, location_lon, open_hour, close_hour) VALUES (3, 51.107883, 17.038538, '00:00:00', '23:59:59'); | ||
|
||
---- Clients | ||
insert into client(id, pos_id, msisdn, email, consents, client_type) VALUES (1, 1, '48500500500', '[email protected]', 'SMS', 'INDIVIDUAL'); | ||
insert into client(id, pos_id, msisdn, email, consents, client_type) VALUES (2, 1, '48500500501', '[email protected]', 'SMS_EMAIL', 'BUSINESS'); | ||
insert into client(id, pos_id, msisdn, email, consents, client_type) VALUES (3, 1, '48500500502', '[email protected]', 'PUSH', 'INDIVIDUAL'); | ||
insert into client(id, pos_id, msisdn, email, consents, client_type) VALUES (4, 2, '48500500503', '[email protected]', 'SMS_EMAIL_PUSH', 'INDIVIDUAL'); | ||
insert into client(id, pos_id, msisdn, email, consents, client_type) VALUES (5, 2, '48500500504', '[email protected]', 'EMAIL', 'BUSINESS'); | ||
insert into client(id, pos_id, msisdn, email, consents, client_type) VALUES (6, 2, '48500500505', '[email protected]', null, 'BUSINESS'); | ||
insert into client(id, pos_id, msisdn, email, consents, client_type) VALUES (7, 2, '48500500506', '[email protected]', 'EMAIL_PUSH', 'INDIVIDUAL'); | ||
insert into client(id, pos_id, msisdn, email, consents, client_type) VALUES (8, 3, '48500500507', '[email protected]', 'SMS_PUSH', 'BUSINESS'); | ||
insert into client(id, pos_id, msisdn, email, consents, client_type) VALUES (9, 3, '48500500508', '[email protected]', 'EMAIL', 'INDIVIDUAL'); | ||
|
||
---- Blocked | ||
insert into blocked_list(client_id) values (5); | ||
|
||
-- Contact history | ||
insert into contact_history(client_id, event_time) VALUES (9, NOW() - INTERVAL '1 minutes'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/bash -e | ||
|
||
strip_extension() { | ||
local file="$1" | ||
echo "${file%.*}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/bin/bash -e | ||
|
||
cd "$(dirname "$0")" | ||
. postgres_operations.sh | ||
. common.sh | ||
|
||
init_db() { | ||
init_bg_log_file | ||
init_data_dir | ||
init_custom_conf_dir | ||
configure_pg_config | ||
configure_authentication | ||
} | ||
|
||
configure_users() { | ||
create_user | ||
create_custom_database | ||
grant_privileges | ||
alter_pg_user_pass | ||
} | ||
|
||
execute_ddls() { | ||
local schema_name | ||
local ddl_content | ||
for file in "$PG_DDL_DIR"/*; do | ||
if [ -f "$file" ]; then | ||
schema_name=$(basename "$(strip_extension "$file")") | ||
echo "Creating schema: $schema_name" | ||
create_schema "$PG_USER" "$schema_name" | ||
ddl_content=$(wrap_sql_with_current_schema "$schema_name" "$(cat "$file")") | ||
echo "Executing ddl: $file with content: $ddl_content" | ||
echo "$ddl_content" | execute_sql "" "$PG_USER" "$PG_PASS" | ||
fi | ||
done | ||
} | ||
|
||
init_db | ||
start_bg | ||
wait_until_started | ||
configure_users | ||
execute_ddls | ||
stop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#!/bin/bash -e | ||
|
||
init_data_dir() { | ||
if [ ! -e "$PG_DATA_DIR" ]; then | ||
mkdir -p "$PG_DATA_DIR" | ||
chown postgres "$PG_DATA_DIR" | ||
/sbin/setuser postgres "$PG_BIN_DIR"/initdb -D "$PG_DATA_DIR" | ||
fi | ||
} | ||
|
||
init_custom_conf_dir() { | ||
if [ ! -e "$PG_CUSTOM_CONF_DIR" ]; then | ||
mkdir -p "$PG_CUSTOM_CONF_DIR" | ||
chown postgres "$PG_CUSTOM_CONF_DIR" | ||
fi | ||
} | ||
|
||
configure_authentication() { | ||
if [ ! -f "$PG_HBA_FILE" ]; then | ||
cp "$PG_DATA_DIR/pg_hba.conf" "$PG_HBA_FILE" | ||
chown postgres "$PG_HBA_FILE" | ||
echo "#<Custom configuration>" >> "$PG_HBA_FILE" | ||
echo "host all all all md5" >> "$PG_HBA_FILE" | ||
fi | ||
} | ||
|
||
configure_pg_config() { | ||
if [ ! -f "$PG_CONF_FILE" ]; then | ||
cp "$PG_DATA_DIR/postgresql.conf" "$PG_CONF_FILE" | ||
chown postgres "$PG_CONF_FILE" | ||
echo "#<Custom configuration>" >> "$PG_CONF_FILE" | ||
echo "listen_addresses = '*'" >> "$PG_CONF_FILE" | ||
fi | ||
} | ||
|
||
init_bg_log_file() { | ||
local log_file | ||
log_file="/var/log/postgres_bg.log" | ||
if [ ! -f "$log_file" ]; then | ||
touch "$log_file" | ||
chown postgres "$log_file" | ||
fi | ||
} | ||
|
||
wait_until_started() { | ||
local max_startup_timeout_in_s=10 | ||
while ! pg_isready >/dev/null 2>&1; do | ||
sleep 1 | ||
max_startup_timeout_in_s=$((max_startup_timeout_in_s - 1)) | ||
if ((max_startup_timeout_in_s <= 0)); then | ||
echo "Postgres is not started" | ||
exit 1 | ||
fi | ||
done | ||
echo "Postgres started" | ||
} | ||
|
||
create_custom_database() { | ||
local db_name="${1:-$PG_DB_NAME}" | ||
DB_EXISTS=$(echo "SELECT 1 FROM pg_database WHERE datname='$db_name'" | execute_sql "" "postgres" "" "-tA") | ||
if [ "$DB_EXISTS" != "1" ]; then | ||
echo "CREATE DATABASE \"$db_name\"" | execute_sql "" "postgres" "" | ||
else | ||
echo "DB already exists - creation skipped" | ||
fi | ||
} | ||
|
||
create_user() { | ||
ROLE_EXISTS=$(echo "SELECT 1 FROM pg_roles WHERE rolname='$PG_USER'" | execute_sql "" "postgres" "" "-tA") | ||
if [ "$ROLE_EXISTS" != "1" ]; then | ||
echo "CREATE ROLE \"${PG_USER}\" WITH LOGIN PASSWORD '${PG_PASS}';" | execute_sql "" "postgres" "" | ||
else | ||
echo "ROLE already exists - creation skipped" | ||
fi | ||
} | ||
|
||
grant_privileges() { | ||
local user="${1:-$PG_USER}" | ||
local db_name="${2:-$PG_DB_NAME}" | ||
execute_sql "" "postgres" "" <<EOF | ||
GRANT ALL PRIVILEGES ON DATABASE "${db_name}" TO "${user}"; | ||
ALTER DATABASE "${db_name}" OWNER TO "${user}"; | ||
EOF | ||
} | ||
|
||
create_schema() { | ||
local user="${1:-$PG_USER}" | ||
local schema_name="${2:-PUBLIC}" | ||
echo "CREATE SCHEMA IF NOT EXISTS \"$schema_name\" AUTHORIZATION \"$user\"" | execute_sql "$PG_DB_NAME" "postgres" "" | ||
} | ||
|
||
wrap_sql_with_current_schema() { | ||
local schema_name="${1:-PUBLIC}" | ||
local sql="$2" | ||
cat <<EOF | ||
SET search_path TO $schema_name; | ||
$sql | ||
RESET search_path; | ||
EOF | ||
} | ||
|
||
alter_pg_user_pass() { | ||
echo "ALTER ROLE postgres WITH PASSWORD 'postgres';" | execute_sql "" "postgres" "" | ||
} | ||
|
||
execute_sql() { | ||
local -r db="${1:-}" | ||
local -r user="${2:-postgres}" | ||
local -r pass="${3:-}" | ||
local opts | ||
read -r -a opts <<<"${@:4}" | ||
local args=("-U" "$user" "-p" "${PG_PORT:-5432}" "-h" "127.0.0.1") | ||
[[ -n "$db" ]] && args+=("-d" "$db") | ||
[[ "${#opts[@]}" -gt 0 ]] && args+=("${opts[@]}") | ||
PGPASSWORD=$pass psql "${args[@]}" | ||
} | ||
|
||
start_bg() { | ||
/sbin/setuser postgres "$PG_BIN_DIR"/pg_ctl start -D "$PG_DATA_DIR" -l /var/log/postgres_bg.log | ||
} | ||
|
||
start() { | ||
/sbin/setuser postgres "$PG_BIN_DIR"/postgres -D "$PG_DATA_DIR" "--hba_file=$PG_HBA_FILE" "--config-file=$PG_CONF_FILE" | ||
} | ||
|
||
stop() { | ||
/sbin/setuser postgres "$PG_BIN_DIR"/pg_ctl stop -w -D "$PG_DATA_DIR" | ||
} | ||
|
||
"$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/bin/sh | ||
|
||
export PG_DB_NAME="mocks" | ||
export PG_USER="mocks" | ||
export PG_PASS="mocks_pass" | ||
|
||
export PG_BIN_DIR="/usr/lib/postgresql/16/bin" | ||
export PG_BASE_DIR="/home/postgres" | ||
export PG_DATA_DIR="$PG_BASE_DIR/data" | ||
export PG_CUSTOM_BIN_DIR="$PG_BASE_DIR/scripts" | ||
export PG_DDL_DIR="$PG_BASE_DIR/mocks/__ddl" | ||
export PG_CUSTOM_CONF_DIR="$PG_BASE_DIR/conf" | ||
export PG_CONF_FILE="$PG_CUSTOM_CONF_DIR/postgresql.conf" | ||
export PG_HBA_FILE="$PG_CUSTOM_CONF_DIR/pg_hba.conf" | ||
|
||
"$PG_CUSTOM_BIN_DIR"/configure.sh | ||
exec "$PG_CUSTOM_BIN_DIR"/postgres_operations.sh start |
Oops, something went wrong.