Skip to content

Commit 74c59b9

Browse files
authored
Merge pull request #1041 from opencrvs/ocrvs-10559
Add development time yarn db:clear:all script for easy analytics data clearing
2 parents 96b39f7 + adf4fae commit 74c59b9

File tree

4 files changed

+95
-5
lines changed

4 files changed

+95
-5
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

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: 14 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,28 @@ 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_USER="${ANALYTICS_POSTGRES_USER}" \
141144
postgres:17.6 bash -c '
142145
psql -h postgres -U "$POSTGRES_USER" -d postgres -v ON_ERROR_STOP=1 <<EOF
143146
DROP DATABASE IF EXISTS "$POSTGRES_DB" WITH (FORCE);
147+
DROP DATABASE IF EXISTS "$ANALYTICS_POSTGRES_DB" WITH (FORCE);
144148
145149
DROP ROLE IF EXISTS "$EVENTS_MIGRATOR_ROLE";
146150
DROP ROLE IF EXISTS "$EVENTS_APP_ROLE";
151+
DROP ROLE IF EXISTS "$ANALYTICS_POSTGRES_ROLE";
147152
EOF
148153
'
149154
echo "✅ Database and roles dropped."
150155
echo "🚀 Reinitializing Postgres with on-deploy.sh..."
151156

152157
docker service update --force opencrvs_postgres-on-update
153158

154-
echo "✅ All data cleared."
159+
docker service scale opencrvs_dashboards=0
160+
docker service scale opencrvs_dashboards=1
161+
162+
# Restart events service
163+
#-----------------------------
164+
docker service scale opencrvs_events=0
165+
docker service scale opencrvs_events=1
166+
167+
echo "✅ All data cleared."

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"test:compilation": "tsc --noEmit",
2323
"lint": "eslint -c .eslintrc.js",
2424
"setup-analytics": "docker exec -i -e POSTGRES_HOST=localhost -e POSTGRES_PORT=5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e ANALYTICS_POSTGRES_USER=events_analytics -e ANALYTICS_POSTGRES_PASSWORD=analytics_password postgres bash -s < ./infrastructure/postgres/setup-analytics.sh",
25+
"db:clear:all": "bash infrastructure/clear-all-data-dev.sh && yarn setup-analytics",
2526
"start": "yarn setup-analytics && cross-env NODE_ENV=development NODE_OPTIONS=--dns-result-order=ipv4first nodemon --exec ts-node -r tsconfig-paths/register src/index.ts",
2627
"start:prod": "ts-node --transpile-only -r tsconfig-paths/register src/index.ts",
2728
"metabase": "bash infrastructure/metabase/run-dev.sh",

0 commit comments

Comments
 (0)