Skip to content

Migration/amm 2035#70

Open
SauravBizbRolly wants to merge 18 commits intomainfrom
migration/AMM_2035
Open

Migration/amm 2035#70
SauravBizbRolly wants to merge 18 commits intomainfrom
migration/AMM_2035

Conversation

@SauravBizbRolly
Copy link
Copy Markdown
Contributor

@SauravBizbRolly SauravBizbRolly commented Jan 5, 2026

📋 Description

JIRA ID: AMM-2035


✅ Type of Change

  • 🐞 Bug fix (non-breaking change which resolves an issue)
  • New feature (non-breaking change which adds functionality)
  • 🔥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 🛠 Refactor (change that is neither a fix nor a new feature)
  • ⚙️ Config change (configuration file or build script updates)
  • 📚 Documentation (updates to docs or readme)
  • 🧪 Tests (adding new or updating existing tests)
  • 🎨 UI/UX (changes that affect the user interface)
  • 🚀 Performance (improves performance)
  • 🧹 Chore (miscellaneous changes that don't modify src or test files)

ℹ️ Additional Information

Please describe how the changes were tested, and include any relevant screenshots, logs, or other information that provides additional context.

Summary by CodeRabbit

Release Notes

  • New Features
    • Added capability to record and track ANC counselling care sessions, including visit scheduling and beneficiary information management. The system now stores comprehensive health indicators and risk assessments while maintaining detailed audit trails to support thorough documentation and improved management of maternal healthcare counselling activities across different care settings and provider organizations.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Jan 5, 2026

📝 Walkthrough

Walkthrough

A new SQL migration file introduces the anc_counselling_care table to store antenatal care counseling and care data. The table includes a primary key, beneficiary linkage, visit dates, ANC indicators as boolean fields, audit metadata, and standard timestamps.

Changes

Cohort / File(s) Summary
Database Migration
src/main/resources/db/migration/dbiemr/V67__AMM_2035.sql
New table anc_counselling_care created with 37 lines of DDL, defining schema for ANC counseling records including beneficiary linkage, visit tracking, risk indicators as tinyint flags, and audit fields.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A table springs forth in the database deep,
To track the counsel that mothers keep,
With visits and flags in orderly rows,
ANC care records in careful prose!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'Migration/amm 2035' refers to the JIRA ID and migration type but lacks specificity about the actual schema change — it doesn't clearly convey that a new ANC counselling care table is being introduced. Consider a more descriptive title like 'Add anc_counselling_care table migration' or 'Create ANC counselling care schema (AMM-2035)' to better reflect the primary change.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

Important

Merge conflicts detected (Beta)

  • Resolve merge conflict in branch migration/AMM_2035
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch migration/AMM_2035
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Jan 7, 2026

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Fix all issues with AI agents
In @src/main/resources/db/migration/dbiemr/V38__AMM_2035.sql:
- Line 5: Add indexes for the foreign key columns to improve JOIN/WHERE
performance: create indexes on beneficiary_id, anc_visit_id, and user_id for the
table being created in V38__AMM_2035.sql by either adding INDEX/KEY lines for
`beneficiary_id`, `anc_visit_id`, and `user_id` inside the CREATE TABLE
(immediately after the PRIMARY KEY definition and before the closing
parenthesis) or by adding separate CREATE INDEX statements after the table
creation that reference those exact column names.
- Line 5: Add foreign key constraints on the new table to enforce referential
integrity: alter the table DDL that defines columns user_id and anc_visit_id to
add FOREIGN KEY (user_id) REFERENCES m_user(UserID) and FOREIGN KEY
(anc_visit_id) REFERENCES t_anc_visit(id) (give them sensible names like
fk_<table>_user_id and fk_<table>_anc_visit_id), ensure the referenced columns
are indexed, and choose appropriate ON DELETE/ON UPDATE actions (e.g., RESTRICT
or CASCADE) consistent with other transactional tables; also note beneficiary_id
references the identity DB and should either get a deferred FK pointing to that
schema or be explicitly documented/validated if a cross-db FK is not feasible.
🧹 Nitpick comments (2)
src/main/resources/db/migration/dbiemr/V38__AMM_2035.sql (2)

11-32: Consider schema normalization for the boolean flag columns.

The table contains 22 boolean flag columns representing different counselling care activities and risk factors. While this denormalized design may be acceptable if these fields are typically queried together, consider whether a normalized design with a separate lookup table and junction table would improve maintainability, especially if:

  • New risk factors/activities need to be added frequently
  • Query patterns primarily filter or aggregate by specific flags

A normalized approach could use a structure like:

  • counselling_care_types (lookup table for risk factors/activities)
  • anc_counselling_care_items (junction table linking counselling records to specific care types)

This would reduce schema changes when adding new care types and allow for additional metadata per care type.


34-35: Consider a more standard varchar length for audit columns.

The created_by and updated_by columns use varchar(250), which appears arbitrary. Most audit columns tracking usernames or user identifiers use standard sizes like varchar(100) or varchar(255). Consider aligning with your codebase's standard for user identifier fields.

📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 38905c8 and c71b048.

📒 Files selected for processing (1)
  • src/main/resources/db/migration/dbiemr/V38__AMM_2035.sql
🔇 Additional comments (1)
src/main/resources/db/migration/dbiemr/V38__AMM_2035.sql (1)

6-7: Verify the intentional difference in nullability for date fields.

The visit_date column is nullable (DEFAULT NULL) while home_visit_date is NOT NULL. Please confirm this difference is intentional and reflects the business logic requirements (e.g., home visits always have a date, but general visits may not).


CREATE TABLE IF NOT EXISTS `anc_counselling_care` (
`id` bigint NOT NULL AUTO_INCREMENT,
`beneficiary_id` bigint NOT NULL,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Add indexes on foreign key columns for query performance.

The columns beneficiary_id, anc_visit_id, and user_id appear to be foreign key references that will be frequently used in JOIN operations and WHERE clauses. Without indexes, queries will perform full table scans, leading to poor performance as the table grows.

⚡ Proposed fix to add indexes

Add these lines before the closing parenthesis and after the PRIMARY KEY definition:

   PRIMARY KEY (`id`),
+  KEY `idx_beneficiary_id` (`beneficiary_id`),
+  KEY `idx_anc_visit_id` (`anc_visit_id`),
+  KEY `idx_user_id` (`user_id`)
 ) ENGINE=InnoDB

Also applies to: 8-8, 33-33

🤖 Prompt for AI Agents
In @src/main/resources/db/migration/dbiemr/V38__AMM_2035.sql at line 5, Add
indexes for the foreign key columns to improve JOIN/WHERE performance: create
indexes on beneficiary_id, anc_visit_id, and user_id for the table being created
in V38__AMM_2035.sql by either adding INDEX/KEY lines for `beneficiary_id`,
`anc_visit_id`, and `user_id` inside the CREATE TABLE (immediately after the
PRIMARY KEY definition and before the closing parenthesis) or by adding separate
CREATE INDEX statements after the table creation that reference those exact
column names.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

cat -n src/main/resources/db/migration/dbiemr/V38__AMM_2035.sql

Repository: PSMRI/AMRIT-DB

Length of output: 1868


🏁 Script executed:

# Find migration files related to beneficiary, anc_visit, and user tables
find src/main/resources/db/migration -name "*.sql" | head -20

Repository: PSMRI/AMRIT-DB

Length of output: 1432


🏁 Script executed:

# Search for table definitions that might be referenced
rg "CREATE TABLE.*beneficiary|CREATE TABLE.*anc_visit|CREATE TABLE.*user" --type sql -A 5

Repository: PSMRI/AMRIT-DB

Length of output: 39220


🏁 Script executed:

# Find the anc_visit table definition and check its structure
rg "CREATE TABLE.*anc_visit" -A 20 --type sql | head -40

Repository: PSMRI/AMRIT-DB

Length of output: 2066


🏁 Script executed:

# Check if m_user table has a primary key and what column
rg "CREATE TABLE IF NOT EXISTS \`m_user\`" -A 15 --type sql

Repository: PSMRI/AMRIT-DB

Length of output: 1560


🏁 Script executed:

# Search for existing FOREIGN KEY constraints in the schema
rg "FOREIGN KEY|REFERENCES" --type sql | head -20

Repository: PSMRI/AMRIT-DB

Length of output: 3049


🏁 Script executed:

# Check if constraints are added to anc_counselling_care in later migrations
rg "anc_counselling_care" --type sql -A 5 -B 2

Repository: PSMRI/AMRIT-DB

Length of output: 762


🏁 Script executed:

# Find beneficiary table definition
rg "CREATE TABLE.*beneficiary" --type sql | grep -i "db_iemr\|dbidentity"

Repository: PSMRI/AMRIT-DB

Length of output: 2530


🏁 Script executed:

# Check V38 and later migrations for any ALTER TABLE on anc_counselling_care
cd src/main/resources/db/migration/dbiemr && ls -la V38* V39* V40* 2>/dev/null

Repository: PSMRI/AMRIT-DB

Length of output: 124


🏁 Script executed:

# Search for other tables in db_iemr that reference m_user to see if FK is used
rg "REFERENCES.*m_user|REFERENCES.*t_anc_visit" --type sql

Repository: PSMRI/AMRIT-DB

Length of output: 8498


🏁 Script executed:

# Check if there are other transactional tables in V38 or nearby migrations that show a pattern
rg "CREATE TABLE.*t_" src/main/resources/db/migration/dbiemr/V38__AMM_2035.sql -A 30

Repository: PSMRI/AMRIT-DB

Length of output: 40


🏁 Script executed:

# Count total migrations to understand lifecycle
ls -1 src/main/resources/db/migration/dbiemr/ | wc -l

Repository: PSMRI/AMRIT-DB

Length of output: 60


Add foreign key constraints to enforce referential integrity.

The columns user_id and anc_visit_id should reference m_user(UserID) and t_anc_visit(id) respectively. This schema consistently uses FK constraints across other transactional tables; missing them here creates inconsistency and increases risk of orphaned records. The beneficiary_id column also warrants consideration, though it references the identity database.

🤖 Prompt for AI Agents
In @src/main/resources/db/migration/dbiemr/V38__AMM_2035.sql at line 5, Add
foreign key constraints on the new table to enforce referential integrity: alter
the table DDL that defines columns user_id and anc_visit_id to add FOREIGN KEY
(user_id) REFERENCES m_user(UserID) and FOREIGN KEY (anc_visit_id) REFERENCES
t_anc_visit(id) (give them sensible names like fk_<table>_user_id and
fk_<table>_anc_visit_id), ensure the referenced columns are indexed, and choose
appropriate ON DELETE/ON UPDATE actions (e.g., RESTRICT or CASCADE) consistent
with other transactional tables; also note beneficiary_id references the
identity DB and should either get a deferred FK pointing to that schema or be
explicitly documented/validated if a cross-db FK is not feasible.

Copy link
Copy Markdown
Contributor

@paras-dba paras-dba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add indexes according to your requirement

@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
src/main/resources/db/migration/dbiemr/V67__AMM_2035.sql (2)

11-32: Boolean flags should be non-null to avoid tri-state ambiguity.

Lines 11-32 define boolean-like fields as nullable tinyint(1). This allows NULL, which complicates reporting and downstream logic. Prefer NOT NULL DEFAULT 0 consistently.

Suggested pattern (apply to all flag columns in this block)
-  `select_all` tinyint(1) DEFAULT '0',
-  `swelling` tinyint(1) DEFAULT '0',
-  `high_bp` tinyint(1) DEFAULT '0',
+  `select_all` tinyint(1) NOT NULL DEFAULT 0,
+  `swelling` tinyint(1) NOT NULL DEFAULT 0,
+  `high_bp` tinyint(1) NOT NULL DEFAULT 0,
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/main/resources/db/migration/dbiemr/V67__AMM_2035.sql` around lines 11 -
32, The boolean-like flag columns (e.g., select_all, swelling, high_bp,
convulsions, anemia, reduced_fetal_movement, age_risk, child_gap, short_height,
pre_preg_weight, bleeding, miscarriage_history, four_plus_delivery,
first_delivery, twin_pregnancy, c_section_history, pre_existing_disease,
fever_malaria, jaundice, sickle_cell, prolonged_labor, malpresentation) must be
defined as NOT NULL with a DEFAULT 0 to avoid tri-state NULL ambiguity; update
the CREATE/ALTER statements in V67__AMM_2035.sql to change each of these
tinyint(1) columns to tinyint(1) NOT NULL DEFAULT 0 (apply the pattern to every
flag in the block) and if this migration runs against existing data ensure any
pre-existing NULLs are coalesced to 0 before applying the NOT NULL constraint.

3-3: Align migration pattern with codebase convention or establish codebase-wide refactoring.

The concern about IF NOT EXISTS masking schema drift is valid for versioned migrations. However, this pattern is already used systematically throughout the project (V1__DB_IEMR.sql, V1__DB_REPORTING.sql, etc.). Changing only V67 creates inconsistency. Either adopt this pattern consistently across the codebase, or establish a codebase-wide refactoring plan to remove IF NOT EXISTS from all versioned migrations for deterministic behavior.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/main/resources/db/migration/dbiemr/V67__AMM_2035.sql` at line 3, The
migration V67__AMM_2035.sql uses "CREATE TABLE IF NOT EXISTS
`anc_counselling_care`" which deviates from the deterministic migration practice
you intend to enforce; either remove the IF NOT EXISTS from this statement to
match the existing versioned migration convention (as seen in V1__DB_IEMR.sql
and V1__DB_REPORTING.sql) or plan a coordinated codebase-wide refactor that
removes IF NOT EXISTS from all versioned migrations—update the
`anc_counselling_care` CREATE TABLE line to follow whichever consistent approach
you choose and document the decision in the migration policy.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/main/resources/db/migration/dbiemr/V67__AMM_2035.sql`:
- Line 8: Add a foreign key constraint linking anc_visit_id to t_anc_visit(id)
to enforce referential integrity: alter the table definition in this migration
so the column anc_visit_id bigint NOT NULL includes a foreign key referencing
t_anc_visit(id) (and name the constraint e.g. fk_<table>_anc_visit_id or
similar), ensure ON DELETE/UPDATE behavior is set appropriately (e.g. ON DELETE
CASCADE or NO ACTION per domain rules), and verify the referenced t_anc_visit.id
is the same bigint type before applying the migration.

---

Nitpick comments:
In `@src/main/resources/db/migration/dbiemr/V67__AMM_2035.sql`:
- Around line 11-32: The boolean-like flag columns (e.g., select_all, swelling,
high_bp, convulsions, anemia, reduced_fetal_movement, age_risk, child_gap,
short_height, pre_preg_weight, bleeding, miscarriage_history,
four_plus_delivery, first_delivery, twin_pregnancy, c_section_history,
pre_existing_disease, fever_malaria, jaundice, sickle_cell, prolonged_labor,
malpresentation) must be defined as NOT NULL with a DEFAULT 0 to avoid tri-state
NULL ambiguity; update the CREATE/ALTER statements in V67__AMM_2035.sql to
change each of these tinyint(1) columns to tinyint(1) NOT NULL DEFAULT 0 (apply
the pattern to every flag in the block) and if this migration runs against
existing data ensure any pre-existing NULLs are coalesced to 0 before applying
the NOT NULL constraint.
- Line 3: The migration V67__AMM_2035.sql uses "CREATE TABLE IF NOT EXISTS
`anc_counselling_care`" which deviates from the deterministic migration practice
you intend to enforce; either remove the IF NOT EXISTS from this statement to
match the existing versioned migration convention (as seen in V1__DB_IEMR.sql
and V1__DB_REPORTING.sql) or plan a coordinated codebase-wide refactor that
removes IF NOT EXISTS from all versioned migrations—update the
`anc_counselling_care` CREATE TABLE line to follow whichever consistent approach
you choose and document the decision in the migration policy.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d1572c66-08f9-48c6-a1c9-f999d612d91c

📥 Commits

Reviewing files that changed from the base of the PR and between c71b048 and 17757d6.

📒 Files selected for processing (1)
  • src/main/resources/db/migration/dbiemr/V67__AMM_2035.sql

`beneficiary_id` bigint NOT NULL,
`visit_date` date DEFAULT NULL,
`home_visit_date` date NOT NULL,
`anc_visit_id` bigint NOT NULL,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

fd -t f "V67__AMM_2035.sql" src/main/resources/db/migration

Repository: PSMRI/AMRIT-DB

Length of output: 114


🏁 Script executed:

cat -n src/main/resources/db/migration/dbiemr/V67__AMM_2035.sql

Repository: PSMRI/AMRIT-DB

Length of output: 1879


🏁 Script executed:

cat -n src/main/resources/db/migration/dbiemr/V1__DB_IEMR.sql | grep -A 20 "CREATE TABLE.*t_anc_visit"

Repository: PSMRI/AMRIT-DB

Length of output: 1058


🏁 Script executed:

fd -t f "V[0-9]*.sql" src/main/resources/db/migration/dbiemr | sort -V | tail -20

Repository: PSMRI/AMRIT-DB

Length of output: 40


🏁 Script executed:

grep -r "fk_anc_counselling_care" src/main/resources/db/migration/dbiemr/

Repository: PSMRI/AMRIT-DB

Length of output: 40


🏁 Script executed:

grep -r "CONSTRAINT.*anc_visit" src/main/resources/db/migration/dbiemr/

Repository: PSMRI/AMRIT-DB

Length of output: 40


🏁 Script executed:

ls -1 src/main/resources/db/migration/dbiemr/ | grep "^V" | sort -V | tail -5

Repository: PSMRI/AMRIT-DB

Length of output: 197


🏁 Script executed:

grep -l "anc_counselling_care" src/main/resources/db/migration/dbiemr/*.sql

Repository: PSMRI/AMRIT-DB

Length of output: 114


🏁 Script executed:

ls -1 src/main/resources/db/migration/dbiemr/ | wc -l

Repository: PSMRI/AMRIT-DB

Length of output: 60


🏁 Script executed:

grep -n "FOREIGN KEY" src/main/resources/db/migration/dbiemr/V67__AMM_2035.sql

Repository: PSMRI/AMRIT-DB

Length of output: 40


🏁 Script executed:

grep -n "FOREIGN KEY" src/main/resources/db/migration/dbiemr/V1__DB_IEMR.sql | head -5

Repository: PSMRI/AMRIT-DB

Length of output: 914


Add a foreign key constraint for anc_visit_id to enforce referential integrity.

The anc_visit_id column at line 8 is declared as NOT NULL but lacks a foreign key constraint. The t_anc_visit table exists in V1__DB_IEMR.sql with a compatible id column type (bigint). Without the constraint, orphaned records and referential integrity violations are possible.

Suggested change
    `updated_by` varchar(250) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
+   KEY `idx_anc_counselling_care_anc_visit_id` (`anc_visit_id`),
+   CONSTRAINT `fk_anc_counselling_care_anc_visit`
+     FOREIGN KEY (`anc_visit_id`) REFERENCES `t_anc_visit` (`id`),
    PRIMARY KEY (`id`)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/main/resources/db/migration/dbiemr/V67__AMM_2035.sql` at line 8, Add a
foreign key constraint linking anc_visit_id to t_anc_visit(id) to enforce
referential integrity: alter the table definition in this migration so the
column anc_visit_id bigint NOT NULL includes a foreign key referencing
t_anc_visit(id) (and name the constraint e.g. fk_<table>_anc_visit_id or
similar), ensure ON DELETE/UPDATE behavior is set appropriately (e.g. ON DELETE
CASCADE or NO ACTION per domain rules), and verify the referenced t_anc_visit.id
is the same bigint type before applying the migration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants