Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions .github/workflows/generate-db-dumps.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
name: Generate SQL Database Dumps

on:
workflow_dispatch:
inputs:
refapp_version:
description: 'RefApp version (leave empty for pom.xml default)'
required: false
type: string
openmrs_version:
description: 'OpenMRS Core version (leave empty for pom.xml default)'
required: false
type: string

permissions:
contents: write
pull-requests: write

concurrency:
group: generate-db-dumps
cancel-in-progress: true

jobs:
generate-dumps:
runs-on: ubuntu-latest
timeout-minutes: 90

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'

- name: Set up Docker Compose
run: |
sudo apt-get update
sudo apt-get install -y docker-compose

- name: Pre-run OpenMRS SDK Plugin
run: mvn org.openmrs.maven.plugins:openmrs-sdk-maven-plugin:setup-sdk -B --settings .github/maven-settings.xml

- name: Determine versions
id: versions
run: |
REFAPP_VERSION="${{ github.event.inputs.refapp_version }}"
OPENMRS_VERSION="${{ github.event.inputs.openmrs_version }}"
if [ -z "$REFAPP_VERSION" ]; then
REFAPP_VERSION=$(mvn help:evaluate -Dexpression=refapp.version -q -DforceStdout -B --settings .github/maven-settings.xml)
fi
if [ -z "$OPENMRS_VERSION" ]; then
OPENMRS_VERSION=$(mvn help:evaluate -Dexpression=openmrs.version -q -DforceStdout -B --settings .github/maven-settings.xml)
fi
echo "refapp_version=$REFAPP_VERSION" >> "$GITHUB_OUTPUT"
echo "openmrs_version=$OPENMRS_VERSION" >> "$GITHUB_OUTPUT"
echo "Using RefApp $REFAPP_VERSION, OpenMRS Core $OPENMRS_VERSION"

- name: Generate and build OpenMRS distribution
run: |
mvn -f pom-step-01.xml process-resources -Pci -B \
--settings .github/maven-settings.xml \
-Drefapp.version=${{ steps.versions.outputs.refapp_version }} \
-Dopenmrs.version=${{ steps.versions.outputs.openmrs_version }}

- name: Show generated Docker Compose config
run: cat target/distro/docker-compose.yml

- name: Generate empty database dump
run: |
bash scripts/generate-db-dumps.sh \
target/distro \
"${{ steps.versions.outputs.refapp_version }}" \
src/main/db \
empty

- name: Generate demo database dump
run: |
bash scripts/generate-db-dumps.sh \
target/distro \
"${{ steps.versions.outputs.refapp_version }}" \
src/main/db \
demo

- name: Create Pull Request
uses: peter-evans/create-pull-request@v7
with:
commit-message: |
Update SQL database dumps for RefApp ${{ steps.versions.outputs.refapp_version }}

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
title: "Update SQL database dumps for RefApp ${{ steps.versions.outputs.refapp_version }}"
body: |
This PR updates the SQL database dumps for Reference Application **${{ steps.versions.outputs.refapp_version }}** (OpenMRS Core ${{ steps.versions.outputs.openmrs_version }}).

Generated automatically by the **Generate SQL Database Dumps** workflow.

### Files updated
- `src/main/db/demo-db-${{ steps.versions.outputs.refapp_version }}.sql` — Database with demo data
- `src/main/db/empty-db-${{ steps.versions.outputs.refapp_version }}.sql` — Empty database (schema only)
branch: update-db-dumps-${{ steps.versions.outputs.refapp_version }}
base: openmrs-emr3
add-paths: |
src/main/db/*.sql
112 changes: 112 additions & 0 deletions scripts/generate-db-dumps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#!/bin/bash
# Generates an SQL database dump from a Docker-based OpenMRS instance.
#
# Usage: ./generate-db-dumps.sh <distro-dir> <refapp-version> <output-dir> <mode>
# distro-dir: Path to the SDK-generated distribution (e.g., target/distro)
# refapp-version: Reference Application version (e.g., 3.6.0-SNAPSHOT)
# output-dir: Where to save the SQL dump (e.g., src/main/db)
# mode: "demo" for demo data, "empty" for schema only

set -euo pipefail

DISTRO_DIR="${1:?Usage: $0 <distro-dir> <refapp-version> <output-dir> <mode>}"
REFAPP_VERSION="${2:?Missing refapp version}"
OUTPUT_DIR="${3:?Missing output directory}"
MODE="${4:?Missing mode (demo or empty)}"

TIMEOUT="${TIMEOUT:-600}"
DB_ROOT_PASSWORD="${DB_ROOT_PASSWORD:-openmrs}"
COMPOSE_FILE="$DISTRO_DIR/docker-compose.yml"
OVERRIDE_FILE="$DISTRO_DIR/docker-compose.override.yml"

if [ ! -f "$COMPOSE_FILE" ]; then
echo "❌ Docker Compose file not found: $COMPOSE_FILE"
exit 1
fi

# Build compose command args
COMPOSE_ARGS=(-f "$COMPOSE_FILE")

cleanup() {
echo "🧹 Cleaning up Docker containers..."
docker compose "${COMPOSE_ARGS[@]}" down -v 2>/dev/null || true
rm -f "$OVERRIDE_FILE"
}
trap cleanup EXIT

# Fix the auto-generated Dockerfile base image tag if needed
if [ -f "$DISTRO_DIR/web/Dockerfile" ] && grep -q 'nightly-amazoncorretto-11' "$DISTRO_DIR/web/Dockerfile"; then
sed -i.bak 's|openmrs/openmrs-core:nightly-amazoncorretto-11|openmrs/openmrs-core:2.8.x|g' \
"$DISTRO_DIR/web/Dockerfile" && rm -f "$DISTRO_DIR/web/Dockerfile.bak"
fi

# Configure demo data via compose override
rm -f "$OVERRIDE_FILE"
if [ "$MODE" = "demo" ]; then
cat > "$OVERRIDE_FILE" <<'EOF'
services:
web:
environment:
OMRS_CONFIG_ADD_DEMO_DATA: "true"
EOF
COMPOSE_ARGS+=(-f "$OVERRIDE_FILE")
echo "📦 Mode: demo (with demo data)"
else
echo "📦 Mode: empty (schema only, no demo data)"
fi

echo "🚀 Starting OpenMRS in Docker from $DISTRO_DIR..."
docker compose "${COMPOSE_ARGS[@]}" up -d --build web

echo "⏳ Waiting for OpenMRS to initialize (timeout: ${TIMEOUT}s)..."
START_TIME=$(date +%s)

while true; do
if curl -sf http://localhost:8080/openmrs > /dev/null 2>&1; then
echo "✅ OpenMRS is responding."
echo "⏳ Waiting 60s for background initialization tasks to complete..."
sleep 60
break
fi

NOW=$(date +%s)
ELAPSED=$((NOW - START_TIME))
if [ "$ELAPSED" -gt "$TIMEOUT" ]; then
echo "❌ Timeout reached after ${TIMEOUT}s waiting for OpenMRS."
echo "--- Docker logs (web) ---"
docker compose "${COMPOSE_ARGS[@]}" logs --tail=50 web
exit 1
fi

sleep 10
done

# Find the database container
DB_CONTAINER=$(docker compose "${COMPOSE_ARGS[@]}" ps -q db)
if [ -z "$DB_CONTAINER" ]; then
echo "❌ Could not find database container (service: db)."
docker compose "${COMPOSE_ARGS[@]}" ps
exit 1
fi

# Generate SQL dump
OUTPUT_FILE="$OUTPUT_DIR/${MODE}-db-${REFAPP_VERSION}.sql"
echo "📤 Dumping database to: $OUTPUT_FILE"
mkdir -p "$OUTPUT_DIR"

docker exec "$DB_CONTAINER" mysqldump \
--single-transaction \
--routines \
--triggers \
-u root -p"$DB_ROOT_PASSWORD" \
openmrs \
> "$OUTPUT_FILE"

FILE_SIZE=$(wc -c < "$OUTPUT_FILE" | tr -d ' ')
echo "✅ SQL dump generated: $OUTPUT_FILE ($FILE_SIZE bytes)"

if [ "$FILE_SIZE" -lt 1000 ]; then
echo "⚠️ Warning: SQL dump is suspiciously small. Check for errors."
head -20 "$OUTPUT_FILE"
exit 1
fi