From 3ca45c41cd0c0d2497bbf8e589364a8ec49330d0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Filip=20Pit=C3=A1k?= <xpitak@fi.muni.cz>
Date: Fri, 10 May 2024 20:02:22 +0200
Subject: [PATCH] Create bash scripts for clearing and seeding DB

---
 .docker/db/scripts/clear_db.sh                | 26 +++++++++++++++++++
 .docker/db/scripts/{ => init}/0_init_db.sql   |  0
 .../1_create_account_management_db.sql        |  0
 .../{ => init}/1_create_account_query_db.sql  |  0
 .../{ => init}/1_create_transaction_db.sql    |  0
 .../2_generate_account_management_db.sql      |  0
 .../2_generate_account_query_db.sql           |  0
 .../{ => init}/2_generate_transaction_db.sql  |  0
 .docker/db/scripts/seed_db.sh                 | 26 +++++++++++++++++++
 README.md                                     | 14 +++++++---
 docker-compose.yaml                           |  3 ++-
 11 files changed, 64 insertions(+), 5 deletions(-)
 create mode 100644 .docker/db/scripts/clear_db.sh
 rename .docker/db/scripts/{ => init}/0_init_db.sql (100%)
 rename .docker/db/scripts/{ => init}/1_create_account_management_db.sql (100%)
 rename .docker/db/scripts/{ => init}/1_create_account_query_db.sql (100%)
 rename .docker/db/scripts/{ => init}/1_create_transaction_db.sql (100%)
 rename .docker/db/scripts/{ => init}/2_generate_account_management_db.sql (100%)
 rename .docker/db/scripts/{ => init}/2_generate_account_query_db.sql (100%)
 rename .docker/db/scripts/{ => init}/2_generate_transaction_db.sql (100%)
 create mode 100644 .docker/db/scripts/seed_db.sh

diff --git a/.docker/db/scripts/clear_db.sh b/.docker/db/scripts/clear_db.sh
new file mode 100644
index 0000000..2676c48
--- /dev/null
+++ b/.docker/db/scripts/clear_db.sh
@@ -0,0 +1,26 @@
+#!/bin/bash
+
+DB_NAME="banking"
+USERNAME="root"
+PASSWORD="passwd"
+# Uncomment for local use
+HOST="localhost"
+
+#Set DB password as env variable
+export PGPASSWORD="$PASSWORD"
+
+
+# Wait for PostgreSQL to be ready
+until psql -h "$HOST" -U "$USERNAME" -d "$DB_NAME" -c '\q'; do
+  >&2 echo "Postgres is unavailable - sleeping"
+  sleep 1
+done
+
+# Empty tables in the 'bank_user' schema
+psql -h "$HOST" -U "$USERNAME" -d "$DB_NAME" -c "TRUNCATE TABLE bank_user.bank_user, bank_user.bank_account, bank_user.scheduled_payment;"
+
+# Empty tables in the 'bank_account' schema
+psql -h "$HOST" -U "$USERNAME" -d "$DB_NAME" -c "TRUNCATE TABLE bank_account.balance, bank_account.bal_transaction;"
+
+# Empty tables in the 'bank_transaction' schema
+psql -h "$HOST" -U "$USERNAME" -d "$DB_NAME" -c "TRUNCATE TABLE bank_transaction.proc_reg, bank_transaction.proc_transaction;"
diff --git a/.docker/db/scripts/0_init_db.sql b/.docker/db/scripts/init/0_init_db.sql
similarity index 100%
rename from .docker/db/scripts/0_init_db.sql
rename to .docker/db/scripts/init/0_init_db.sql
diff --git a/.docker/db/scripts/1_create_account_management_db.sql b/.docker/db/scripts/init/1_create_account_management_db.sql
similarity index 100%
rename from .docker/db/scripts/1_create_account_management_db.sql
rename to .docker/db/scripts/init/1_create_account_management_db.sql
diff --git a/.docker/db/scripts/1_create_account_query_db.sql b/.docker/db/scripts/init/1_create_account_query_db.sql
similarity index 100%
rename from .docker/db/scripts/1_create_account_query_db.sql
rename to .docker/db/scripts/init/1_create_account_query_db.sql
diff --git a/.docker/db/scripts/1_create_transaction_db.sql b/.docker/db/scripts/init/1_create_transaction_db.sql
similarity index 100%
rename from .docker/db/scripts/1_create_transaction_db.sql
rename to .docker/db/scripts/init/1_create_transaction_db.sql
diff --git a/.docker/db/scripts/2_generate_account_management_db.sql b/.docker/db/scripts/init/2_generate_account_management_db.sql
similarity index 100%
rename from .docker/db/scripts/2_generate_account_management_db.sql
rename to .docker/db/scripts/init/2_generate_account_management_db.sql
diff --git a/.docker/db/scripts/2_generate_account_query_db.sql b/.docker/db/scripts/init/2_generate_account_query_db.sql
similarity index 100%
rename from .docker/db/scripts/2_generate_account_query_db.sql
rename to .docker/db/scripts/init/2_generate_account_query_db.sql
diff --git a/.docker/db/scripts/2_generate_transaction_db.sql b/.docker/db/scripts/init/2_generate_transaction_db.sql
similarity index 100%
rename from .docker/db/scripts/2_generate_transaction_db.sql
rename to .docker/db/scripts/init/2_generate_transaction_db.sql
diff --git a/.docker/db/scripts/seed_db.sh b/.docker/db/scripts/seed_db.sh
new file mode 100644
index 0000000..33351fd
--- /dev/null
+++ b/.docker/db/scripts/seed_db.sh
@@ -0,0 +1,26 @@
+#!/bin/bash
+
+DB_NAME="banking"
+USERNAME="root"
+PASSWORD="passwd"
+# Uncomment for local use
+HOST="localhost"
+
+#Set DB password as env variable
+export PGPASSWORD="$PASSWORD"
+
+
+# Wait for PostgreSQL to be ready
+until psql -h "$HOST" -U "$USERNAME" -d "$DB_NAME" -c '\q'; do
+  >&2 echo "Postgres is unavailable - sleeping"
+  sleep 1
+done
+
+# Seed tables in the 'bank_user' schema
+psql -h "$HOST" -U "$USERNAME" -d "$DB_NAME" -f "/scripts/init/2_generate_account_management_db.sql"
+
+# Seed tables in the 'bank_account' schema
+psql -h "$HOST" -U "$USERNAME" -d "$DB_NAME" -f "/scripts/init/2_generate_account_query_db.sql"
+
+# Seed tables in the 'bank_transaction' schema
+psql -h "$HOST" -U "$USERNAME" -d "$DB_NAME" -f "/scripts/init/2_generate_transaction_db.sql"
diff --git a/README.md b/README.md
index 995b83d..0f935f3 100644
--- a/README.md
+++ b/README.md
@@ -19,10 +19,6 @@ In order to start the applications please follow the following instructions:
 This builds all the required maven artifacts in the correct order, creates images on your local machine and then runs the specified
 docker-compose file.
 
-
-## Use case diagram
-<img src="./.documentation/useCaseDiagram.png" width="800">
-
 ## Artifacts and Applications
 
 | Name                  | Type       | URL                                                                         | Description                                                                                                                                                                                           |
@@ -38,6 +34,16 @@ docker-compose file.
 | Grafana               | monitoring | [Grafana UI](http://localhost:3000)                                         | Monitoring toolkit for metrics visualization and persistence                                                                                                                                          |
 | Locust                | testing    | [Locust UI](http://localhost:8089)                                          | Load testing tool simulating user interactions for evaluating performance                                                                                                                             |
 
+## Clearing and Seeding DB
+Within the [DB docker config files](./.docker/db/scripts) there are two scripts that may be executed on your maching (if you have psql installed!)
+or via the docker container of the database itself.
+
+- For clearing the DB data please use [this bash script](./.docker/db/scripts/clear_db.sh), alternatively within the DB container call `./scripts/clear_db.sh`
+- For seeding the DB with valid data please use [this bash script](./.docker/db/scripts/seed_db.sh), alternatively within the DB container call `./scripts/seed_db.sh`
+
+## Use case diagram
+<img src="./.documentation/useCaseDiagram.png" width="800">
+
 ## Module Architecture
 Each service is implemented as a separate maven artifact consisting of a Spring-boot application. Within each project
 we used the principles of `Hexagonal architecture`, where the domain itself consists of only pure Java classes,
diff --git a/docker-compose.yaml b/docker-compose.yaml
index ec5815f..89eb164 100644
--- a/docker-compose.yaml
+++ b/docker-compose.yaml
@@ -12,7 +12,8 @@ services:
       - POSTGRES_PASSWORD=passwd
       - POSTGRES_DB=banking
     volumes:
-      - ./.docker/db/scripts:/docker-entrypoint-initdb.d
+      - ./.docker/db/scripts/init:/docker-entrypoint-initdb.d
+      - ./.docker/db/scripts:/scripts
     
   account-management:
     image: account-management
-- 
GitLab