Skip to content
Open
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
39 changes: 39 additions & 0 deletions src/main/resources/db/migration/dbiemr/V38__AMM_2035.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
USE db_iemr;

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

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.

`visit_date` date DEFAULT NULL,
`home_visit_date` date NOT NULL,
`anc_visit_id` bigint NOT NULL,
`created_at` datetime DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`select_all` tinyint(1) DEFAULT 0,
`swelling` tinyint(1) DEFAULT 0,
`high_bp` tinyint(1) DEFAULT 0,
`convulsions` tinyint(1) DEFAULT 0,
`anemia` tinyint(1) DEFAULT 0,
`reduced_fetal_movement` tinyint(1) DEFAULT 0,
`age_risk` tinyint(1) DEFAULT 0,
`child_gap` tinyint(1) DEFAULT 0,
`short_height` tinyint(1) DEFAULT 0,
`pre_preg_weight` tinyint(1) DEFAULT 0,
`bleeding` tinyint(1) DEFAULT 0,
`miscarriage_history` tinyint(1) DEFAULT 0,
`four_plus_delivery` tinyint(1) DEFAULT 0,
`first_delivery` tinyint(1) DEFAULT 0,
`twin_pregnancy` tinyint(1) DEFAULT 0,
`c_section_history` tinyint(1) DEFAULT 0,
`pre_existing_disease` tinyint(1) DEFAULT 0,
`fever_malaria` tinyint(1) DEFAULT 0,
`jaundice` tinyint(1) DEFAULT 0,
`sickle_cell` tinyint(1) DEFAULT 0,
`prolonged_labor` tinyint(1) DEFAULT 0,
`malpresentation` tinyint(1) DEFAULT 0,
`user_id` int DEFAULT NULL,
`created_by` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`updated_by` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB
DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_unicode_ci;