# psmri/amrit-db architecture

> Architecture diagram of the GitHub repository psmri/amrit-db, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/psmri/amrit-db
- Repository: https://github.com/psmri/amrit-db

## Overview

##### Project type and purpose
- This is a backend‐only Spring Boot microservice whose sole responsibility is to manage and run Flyway database migrations across multiple schemas.
- It has no frontend/UI layers; it exposes a minimal REST API (e.g., a VersionController) and runs automatically at startup.

##### File structure
- Top‐level build and CI files: pom.xml, mvnw, GitHub workflows.
- src/main/java: Spring Boot application entry point, config packages, controller, service, data model.
- src/main/resources/db/migration: four schema subfolders containing versioned SQL scripts.
- src/main/environment: environment‐specific property templates.
- src/test: basic SpringBootTest.

##### Extract key points from the README
- Uses Java 17, Maven 3.6+, MySQL 8.
- Four mandatory databases: db_iemr, db_reporting, db_identity, db_1097_identity.
- Flyway handles schema versioning and migration at startup.
- CI pipelines include build on PR, packaging, SAST checks.

##### Main components
a. AMRIT-DB Spring Boot Application (single deployable JAR)
- Subcomponents:
- Application Core (AmritDbApplication, ServletInitializer)
- Configuration Layer (DatasourceConfig, FlywayConfig, JdbcTemplateConfig, FlywayMigrator)
- Service Layer (VersionService)
- Controller Layer (VersionController)
- Data Layer (FlywaySchemaVersion model, JdbcTemplate integration)
b. Database Server (MySQL instance)
- Four logical schemas: db_iemr, db_reporting, db_identity, db_1097_identity
c. Flyway Migration Scripts
- Organized under src/main/resources/db/migration/<schema>/V*.sql
d. External Systems/Tools
- GitHub Actions CI pipelines
- Developers’ local IDE and Maven build
- Swagger UI (runtime endpoint)

##### Relationships & data flows
- On startup, Spring Boot loads application.properties + environment overrides → initializes DataSource beans → triggers FlywayConfig → FlywayMigrator reads migration scripts for each schema → applies pending migrations to MySQL schemas.
- VersionController → VersionService → JdbcTemplate queries flyway_schema_history.
- CI pipelines run mvn clean install → package → run tests → SAST → (optionally) deploy artifact.

##### Architectural patterns & principles
- Layered (Controller → Service → Data).
- Configuration‐first approach (externalize properties per environment).
- Convention-based migration (Flyway folder structure and naming).
- Single‐responsibility microservice (DB schema management only).

##### Details
- Maven wrapper and pom.xml as inputs to the CI build.
- Swagger UI endpoint as optional runtime interface.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% CI/CD Pipeline
    subgraph "CI/CD Pipeline" 
        direction TB
        GH_Pull["build-on-pull-request.yml"]:::ci
        GH_Pkg["package.yml"]:::ci
        GH_SAST["sast.yml"]:::ci
        Pom["pom.xml"]:::ci
        MVNW["mvnw & mvnw.cmd"]:::ci
        MVNWrapper["maven-wrapper.properties"]:::ci
    end
    CI_Artifact["Build Artifact (JAR)"]:::code

    %% Application
    subgraph "AMRIT-DB Application" 
        direction TB
        subgraph Core
            A1["AmritDbApplication"]:::code
            A2["ServletInitializer"]:::code
        end
        subgraph Config
            C1["DatasourceConfig"]:::code
            C2["FlywayConfig"]:::code
            C3["JdbcTemplateConfig"]:::code
            C4["FlywayMigrator"]:::code
        end
        subgraph Controller
            Ctrl["VersionController"]:::code
        end
        subgraph Service
            Serv["VersionService"]:::code
        end
        subgraph Data
            DataModel["FlywaySchemaVersion"]:::code
        end
        Swagger["Swagger UI Endpoint"]:::code
    end

    %% Migration Scripts
    subgraph "Migration Scripts (db/migration)" 
        direction TB
        MS1["dbiemr/*.sql"]:::scripts
        MS2["dbreporting/*.sql"]:::scripts
        MS3["dbidentity/*.sql"]:::scripts
        MS4["db1097identity/*.sql"]:::scripts
    end

    %% Database
    subgraph "MySQL Database" 
        direction LR
        DB1[(db_iemr)]:::db
        DB2[(db_reporting)]:::db
        DB3[(db_identity)]:::db
        DB4[(db_1097_identity)]:::db
    end

    %% Relationships
    GH_Pull --> GH_Pkg
    GH_Pkg --> GH_SAST
    Pom --> GH_Pull
    MVNW --> GH_Pull
    MVNWrapper --> GH_Pull
    GH_SAST --> CI_Artifact
    CI_Artifact -->|"deploy"| A1

    MS1 -->|migrations| C4
    MS2 -->|migrations| C4
    MS3 -->|migrations| C4
    MS4 -->|migrations| C4

    C4 -->|apply migrations| DB1
    C4 -->|"apply migrations"| DB2
    C4 -->|"apply migrations"| DB3
    C4 -->|"apply migrations"| DB4

    Ctrl -->|"calls"| Serv
    Serv -->|"uses JdbcTemplate"| DataModel
    DataModel -->|"queries"| DB1
    DataModel -->|"queries"| DB2
    DataModel -->|"queries"| DB3
    DataModel -->|"queries"| DB4

    A1 -->|"initializes beans"| C1
    A1 -->|"initializes beans"| C2
    A1 -->|"initializes beans"| C3
    A1 -->|"initializes beans"| C4

    A1 -->|"exposes"| Ctrl
    A1 -->|"serves"| Swagger

    %% Click Events
    click A1 "https://github.com/psmri/amrit-db/blob/main/src/main/java/com/db/piramalswasthya/AmritDbApplication.java"
    click A2 "https://github.com/psmri/amrit-db/blob/main/src/main/java/com/db/piramalswasthya/ServletInitializer.java"
    click C1 "https://github.com/psmri/amrit-db/blob/main/src/main/java/com/db/piramalswasthya/config/DatasourceConfig.java"
    click C2 "https://github.com/psmri/amrit-db/blob/main/src/main/java/com/db/piramalswasthya/config/FlywayConfig.java"
    click C3 "https://github.com/psmri/amrit-db/blob/main/src/main/java/com/db/piramalswasthya/config/JdbcTemplateConfig.java"
    click C4 "https://github.com/psmri/amrit-db/blob/main/src/main/java/com/db/piramalswasthya/config/FlywayMigrator.java"
    click Ctrl "https://github.com/psmri/amrit-db/blob/main/src/main/java/com/db/piramalswasthya/controller/VersionController.java"
    click Serv "https://github.com/psmri/amrit-db/blob/main/src/main/java/com/db/piramalswasthya/service/VersionService.java"
    click DataModel "https://github.com/psmri/amrit-db/blob/main/src/main/java/com/db/piramalswasthya/data/FlywaySchemaVersion.java"
    click MS1 "https://github.com/psmri/amrit-db/tree/main/src/main/resources/db/migration/dbiemr"
    click MS2 "https://github.com/psmri/amrit-db/tree/main/src/main/resources/db/migration/dbreporting"
    click MS3 "https://github.com/psmri/amrit-db/tree/main/src/main/resources/db/migration/dbidentity"
    click MS4 "https://github.com/psmri/amrit-db/tree/main/src/main/resources/db/migration/db1097identity"
    click GH_Pull "https://github.com/psmri/amrit-db/blob/main/.github/workflows/build-on-pull-request.yml"
    click GH_Pkg "https://github.com/psmri/amrit-db/blob/main/.github/workflows/pacakage.yml"
    click GH_SAST "https://github.com/psmri/amrit-db/blob/main/.github/workflows/sast.yml"
    click Pom "https://github.com/psmri/amrit-db/blob/main/pom.xml"
    click MVNW "https://github.com/psmri/amrit-db/tree/main/mvnw"
    click MVNWrapper "https://github.com/psmri/amrit-db/blob/main/.mvn/wrapper/maven-wrapper.properties"

    %% Styles
    classDef code fill:#ADD8E6,stroke:#000,color:#000
    classDef db fill:#90EE90,stroke:#000,color:#000
    classDef scripts fill:#FFFFE0,stroke:#000,color:#000
    classDef ci fill:#FFA500,stroke:#000,color:#000
```
