Skip to content

Commit b729451

Browse files
authored
Merge branch 'develop' into ocrvs-10672
2 parents e0f4669 + ca45723 commit b729451

39 files changed

+4899
-1529
lines changed

README.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,35 @@
2323

2424
This is an example country configuration package for the OpenCRVS core. OpenCRVS requires a country configuration in order to run.
2525

26-
OpenCRVS is designed to be highly configurable for your country needs. It achieves this by seeding reference data that it needs from this module and exposing APIs for certain business critical operations.
26+
OpenCRVS is designed to be highly configurable for your country needs. It achieves this by seeding reference data that it needs from this module and exposing APIs for certain business critical operations.
2727

2828
This module also provides a logical location where you may wish to store the code and run the servers for any custom API integrations, extension modules and innovations to OpenCRVS.
2929

3030
## How do I run the module alongside the OpenCRVS core?
3131

32-
1. Ensure that you are running [OpenCRVS Core](https://github.com/opencrvs/opencrvs-core).
32+
1. Ensure that you are running [OpenCRVS Core](https://github.com/opencrvs/opencrvs-core).
3333

34-
**If you successfully ran the `bash setup.sh` script in OpenCRVS Core you already have this module checked out, the dependencies are installed, the Farajaland database is populated and you can just run the following command.**
34+
**If you successfully ran the `bash setup.sh` script in OpenCRVS Core you already have this module checked out, the dependencies are installed, the Farajaland database is populated and you can just run the following command.**
3535

3636
2. `yarn dev`
3737

3838
Thats it! 🎉
3939

40+
## Database Management
41+
42+
The country configuration includes scripts for managing analytics and metabase databases during development and deployment.
43+
44+
### Development Database Management
45+
46+
- `yarn db:clear:all` - Clears all development databases including:
47+
- PostgreSQL analytics schema and data
48+
The script is designed to be extended by country implementations to clear additional custom databases as needed.
49+
50+
### Production Database Management
51+
52+
For deployed environments, analytics data is cleared when the "reset data" pipeline is triggered, which uses the `infrastructure/clear-all-data.sh` script. This script now includes clearing of:
53+
- **Metabase analytics database** - Both the PostgreSQL analytics schema and Metabase H2 configuration database
54+
4055
## What is in the Farajaland configuration module repository?
4156

4257
One of the key dependencies and enablers for OpenCRVS is country configuration and a reference data source. This source is bespoke for every implementing nation. If you would like to create your own country implementation, we recommend that you duplicate this repository and use it as a template. So what does it contain?
@@ -57,7 +72,7 @@ When the OpenCRVS Core servers start up with un-seeded databases they call the f
5772

5873
1. `GET /application-config`
5974

60-
- Configures general application settings
75+
- Configures general application settings
6176

6277
2. `GET /users`
6378

infrastructure/backups/restore.sh

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,9 @@ echo "🧹 cleanup for indices: $approved_words from $indices"
149149
echo "--------------------------"
150150
for index in ${indices[@]}; do
151151
for approved in $approved_words; do
152+
echo "Checking index $index against approved pattern $approved..."
152153
case "$index" in
153-
"$approved_words"*)
154+
"$approved"*)
154155
echo "Delete index $index..."
155156
docker run --rm --network=$NETWORK appropriate/curl curl -sS -XDELETE "http://$(elasticsearch_host)/$index"
156157
break
@@ -214,7 +215,7 @@ if [ -f "$ROOT_PATH/backups/postgres/events-${LABEL}.dump" ]; then
214215
-e PGPASSWORD=$POSTGRES_PASSWORD \
215216
--network=$NETWORK \
216217
postgres:17.6 \
217-
bash -c "psql -h postgres -U $POSTGRES_USER -c 'DROP DATABASE IF EXISTS events;'"
218+
bash -c "psql -h postgres -U $POSTGRES_USER -c 'DROP DATABASE IF EXISTS events WITH (FORCE);'"
218219
else
219220
echo "PostgreSQL backup not found for label ${LABEL}. Skipping PostgreSQL database drop..."
220221
fi
@@ -252,7 +253,11 @@ if [ -f "$ROOT_PATH/backups/postgres/events-${LABEL}.dump" ]; then
252253
-v $ROOT_PATH/backups/postgres:/backups \
253254
--network=$NETWORK \
254255
postgres:17.6 \
255-
bash -c "createdb -h postgres -U $POSTGRES_USER events && pg_restore -h postgres -U $POSTGRES_USER -d events /backups/events-${LABEL}.dump"
256+
bash -c "createdb -h postgres -U $POSTGRES_USER events && \
257+
psql -h postgres -U $POSTGRES_USER -d events -c 'CREATE SCHEMA app AUTHORIZATION events_migrator; GRANT USAGE ON SCHEMA app TO events_app;' && \
258+
pg_restore -h postgres -U $POSTGRES_USER -d events --schema=app /backups/events-${LABEL}.dump"
259+
echo "Update credentials in Postgres on restore"
260+
docker service update --force opencrvs_postgres-on-update
256261
else
257262
echo "PostgreSQL backup not found for label ${LABEL}. Skipping PostgreSQL database restore..."
258263
fi
@@ -309,3 +314,15 @@ tar -xzvf $ROOT_PATH/backups/vsexport/ocrvs-$LABEL.tar.gz -C $ROOT_PATH/vsexport
309314
if [ "$IS_LOCAL" = false ]; then
310315
docker service update --force --update-parallelism 1 opencrvs_migration
311316
fi
317+
318+
##
319+
# ------ REINDEX -----
320+
##
321+
docker run --rm \
322+
-v /opt/opencrvs/infrastructure/deployment:/workspace \
323+
-w /workspace \
324+
--network $NETWORK \
325+
-e 'AUTH_URL=http://auth:4040/' \
326+
-e 'EVENTS_URL=http://gateway:7070/events' \
327+
alpine \
328+
sh -c 'apk add --no-cache curl jq && sh reindex.sh'
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/bin/bash
2+
# This Source Code Form is subject to the terms of the Mozilla Public
3+
# License, v. 2.0. If a copy of the MPL was not distributed with this
4+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
#
6+
# OpenCRVS is also distributed under the terms of the Civil Registration
7+
# & Healthcare Disclaimer located at http://opencrvs.org/license.
8+
#
9+
# Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
10+
11+
set -e
12+
13+
echo "🧹 Clearing all development data..."
14+
15+
# Default PostgreSQL connection parameters for development
16+
POSTGRES_HOST=${POSTGRES_HOST:-localhost}
17+
POSTGRES_PORT=${POSTGRES_PORT:-5432}
18+
POSTGRES_USER=${POSTGRES_USER:-postgres}
19+
POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-postgres}
20+
ANALYTICS_POSTGRES_USER=${ANALYTICS_POSTGRES_USER:-events_analytics}
21+
22+
print_usage_and_exit() {
23+
echo 'Usage: ./clear-all-data-dev.sh'
24+
echo ""
25+
echo "Environment variables (with defaults for development):"
26+
echo "POSTGRES_HOST=${POSTGRES_HOST}"
27+
echo "POSTGRES_PORT=${POSTGRES_PORT}"
28+
echo "POSTGRES_USER=${POSTGRES_USER}"
29+
echo "POSTGRES_PASSWORD=${POSTGRES_PASSWORD}"
30+
echo "ANALYTICS_POSTGRES_USER=${ANALYTICS_POSTGRES_USER}"
31+
echo ""
32+
echo "This script clears all development databases including:"
33+
echo "- PostgreSQL analytics schema and data"
34+
echo "- Metabase configuration database (H2)"
35+
echo "- Any additional custom databases (can be extended by country configurations)"
36+
exit 1
37+
}
38+
39+
# Check if PostgreSQL is accessible
40+
if ! PGPASSWORD="$POSTGRES_PASSWORD" psql -h "$POSTGRES_HOST" -p "$POSTGRES_PORT" -U "$POSTGRES_USER" -d postgres -c '\q' 2>/dev/null; then
41+
echo "❌ Cannot connect to PostgreSQL at ${POSTGRES_HOST}:${POSTGRES_PORT}"
42+
echo " Make sure PostgreSQL is running and credentials are correct."
43+
print_usage_and_exit
44+
fi
45+
46+
# Clear PostgreSQL analytics schema
47+
echo "🗑️ Clearing PostgreSQL analytics schema..."
48+
PGPASSWORD="$POSTGRES_PASSWORD" psql -h "$POSTGRES_HOST" -p "$POSTGRES_PORT" -U "$POSTGRES_USER" -d events -v ON_ERROR_STOP=1 <<EOSQL || echo "⚠️ Analytics schema may not exist yet"
49+
-- Drop analytics schema and recreate it
50+
DROP SCHEMA IF EXISTS analytics CASCADE;
51+
CREATE SCHEMA analytics;
52+
53+
-- Drop and recreate analytics user
54+
DROP ROLE IF EXISTS "$ANALYTICS_POSTGRES_USER";
55+
EOSQL
56+
57+
echo "✅ PostgreSQL analytics data cleared"
58+
59+
echo ""
60+
echo "🎉 All development data cleared successfully!"
61+
echo ""

infrastructure/clear-all-data.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,10 @@ docker run --rm --network=$NETWORK --entrypoint=/bin/sh minio/mc:RELEASE.2025-05
127127
#-------------------------------
128128

129129
POSTGRES_DB="events"
130+
ANALYTICS_POSTGRES_DB="analytics"
130131
EVENTS_MIGRATOR_ROLE="events_migrator"
131132
EVENTS_APP_ROLE="events_app"
133+
ANALYTICS_POSTGRES_ROLE=${ANALYTICS_POSTGRES_USER:-"events_analytics"}
132134

133135
echo "🔁 Dropping database '${POSTGRES_DB}' and roles..."
134136

@@ -138,17 +140,21 @@ docker run --rm --network=$NETWORK \
138140
-e POSTGRES_DB="${POSTGRES_DB}" \
139141
-e EVENTS_MIGRATOR_ROLE="${EVENTS_MIGRATOR_ROLE}" \
140142
-e EVENTS_APP_ROLE="${EVENTS_APP_ROLE}" \
143+
-e ANALYTICS_POSTGRES_ROLE="${ANALYTICS_POSTGRES_ROLE}" \
144+
-e ANALYTICS_POSTGRES_DB="${ANALYTICS_POSTGRES_DB}" \
141145
postgres:17.6 bash -c '
142146
psql -h postgres -U "$POSTGRES_USER" -d postgres -v ON_ERROR_STOP=1 <<EOF
143147
DROP DATABASE IF EXISTS "$POSTGRES_DB" WITH (FORCE);
148+
DROP DATABASE IF EXISTS "$ANALYTICS_POSTGRES_DB" WITH (FORCE);
144149
145150
DROP ROLE IF EXISTS "$EVENTS_MIGRATOR_ROLE";
146151
DROP ROLE IF EXISTS "$EVENTS_APP_ROLE";
152+
DROP ROLE IF EXISTS "$ANALYTICS_POSTGRES_ROLE";
147153
EOF
148154
'
149155
echo "✅ Database and roles dropped."
150156
echo "🚀 Reinitializing Postgres with on-deploy.sh..."
151157

152158
docker service update --force opencrvs_postgres-on-update
153159

154-
echo "✅ All data cleared."
160+
echo "✅ All data cleared."

infrastructure/metabase/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,20 @@
1010

1111
# OpenCRVS Dashboards
1212

13+
### Requirements
14+
15+
```
16+
❯ java -version
17+
openjdk version "21.0.8" 2025-07-15
18+
```
19+
1320
### Run in development mode
1421

1522
By default, Metabase is not started as part of the OpenCRVS stack as running it requires quite a bit of resources. You can use the following commands to use metabase in development:
1623

1724
`yarn metabase` – Starts Metabase in port http://localhost:4444
1825
`yarn db:shell` - In the metabase directory – open a SQL shell for the database Metabase created
26+
`yarn db:clear:all` - Clear data from analytics dashboard
1927

2028
### Default credentials
2129

0 commit comments

Comments
 (0)