Skip to content

alter table of sammelan record#113

Merged
paras-dba merged 2 commits intomainfrom
migration/alter_table_of_sammelan_record
Mar 26, 2026
Merged

alter table of sammelan record#113
paras-dba merged 2 commits intomainfrom
migration/alter_table_of_sammelan_record

Conversation

@SauravBizbRolly
Copy link
Copy Markdown
Contributor

@SauravBizbRolly SauravBizbRolly commented Mar 26, 2026

📋 Description

JIRA ID:

Please provide a summary of the change and the motivation behind it. Include relevant context and details.


✅ 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

  • Chores
    • Applied a database migration to conditionally manage a unique index on the records table to enforce uniqueness by asha and meeting date.
    • Ensures safe add/drop of the constraint at deploy time without affecting existing data.
    • No changes to public APIs or exported interfaces.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 26, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b51be9c3-4463-4806-904f-8a9004755776

📥 Commits

Reviewing files that changed from the base of the PR and between 36d718c and a0cd2dd.

📒 Files selected for processing (1)
  • src/main/resources/db/migration/dbiemr/V69__migratio_of_ch_dbiemr.sql
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/main/resources/db/migration/dbiemr/V69__migratio_of_ch_dbiemr.sql

📝 Walkthrough

Walkthrough

Adds a SQL migration that conditionally drops and/or adds the uk_asha_month index on sammelan_record by querying INFORMATION_SCHEMA.STATISTICS and executing DDL via prepared statements (with DEALLOCATE PREPARE) against the db_iemr schema.

Changes

Cohort / File(s) Summary
Database Migration
src/main/resources/db/migration/dbiemr/V69__migratio_of_ch_dbiemr.sql
Introduces conditional logic: checks INFORMATION_SCHEMA.STATISTICS for uk_asha_month, conditionally executes ALTER TABLE ... DROP INDEX uk_asha_month or no-op, then conditionally executes ALTER TABLE ... ADD UNIQUE KEY uk_asha_month (asha_id, meeting_date) if absent. All DDL uses prepared statements and DEALLOCATE PREPARE.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 I hopped through schema, checks in my paw,
I peeked at stats and fixed what I saw,
Drop if it's there, add when it's not—ah!
Prepared and dealloc'd, tidy and small,
A rabbit's migration, hopped over all. 🥕

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title accurately reflects the main change: altering the sammelan_record table by managing the uk_asha_month index through conditional SQL migration.
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.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch migration/alter_table_of_sammelan_record

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.

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

🤖 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/V69__migratio_of_ch_dbiemr.sql`:
- Around line 3-4: The ALTER TABLE statement drops the index unconditionally
which will fail on re-run; update the ALTER TABLE sammelan_record DROP INDEX
uk_asha_month to use DROP INDEX IF EXISTS uk_asha_month so the migration is
idempotent and matches the repo pattern for safe DROP operations.
- Around line 6-7: Before altering the unique key on table sammelan_record, run
validation and cleanup: query for existing duplicate groups by (asha_id,
meeting_date) to detect rows violating the new constraint, resolve duplicates
(merge or delete according to business rules) or consolidate meeting_date
values, and decide how to handle NULL meeting_date (e.g., set a canonical date,
disallow NULLs, or treat NULLs uniquely) so the new UNIQUE KEY uk_asha_month
(`asha_id`, `meeting_date`) will not fail or create inconsistent historical
data; add pre-migration checks that abort the migration if duplicates remain and
document the chosen cleanup strategy in the migration comments.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9a07c09e-84fe-428f-b652-b7c9ee34268c

📥 Commits

Reviewing files that changed from the base of the PR and between 3c0e666 and 36d718c.

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

Comment on lines +6 to +7
ALTER TABLE sammelan_record
ADD UNIQUE KEY uk_asha_month (`asha_id`, `meeting_date`); No newline at end of file
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:

#!/bin/bash
# Description: Check for existing data that would violate the new unique constraint

# Connect to the database and check for duplicates
cat <<'EOF' > check_duplicates.sql
USE db_iemr;

-- Check for duplicate (asha_id, meeting_date) combinations
SELECT 
    asha_id, 
    meeting_date, 
    COUNT(*) as duplicate_count
FROM sammelan_record
WHERE meeting_date IS NOT NULL
GROUP BY asha_id, meeting_date
HAVING COUNT(*) > 1;

-- Check for records with NULL meeting_date per asha_id
SELECT 
    asha_id, 
    COUNT(*) as null_meeting_date_count
FROM sammelan_record
WHERE meeting_date IS NULL
GROUP BY asha_id
HAVING COUNT(*) > 1;
EOF

echo "Generated SQL script: check_duplicates.sql"
echo "Run this script against your database to identify data that would violate the new constraint."
cat check_duplicates.sql

Repository: PSMRI/AMRIT-DB

Length of output: 650


🏁 Script executed:

find . -name "V69__*" -o -name "V59__*" | head -20

Repository: PSMRI/AMRIT-DB

Length of output: 195


🏁 Script executed:

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

Repository: PSMRI/AMRIT-DB

Length of output: 261


🏁 Script executed:

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

Repository: PSMRI/AMRIT-DB

Length of output: 1250


Validate existing data before changing the unique constraint.

This migration changes the unique constraint from (asha_id) to (asha_id, meeting_date), which is a significant business logic change:

  1. Existing data consistency: While the migration itself will succeed (the old constraint is dropped before adding the new one), if the table contains multiple records with the same (asha_id, meeting_date) combination, those duplicates will remain in the database. The new constraint only prevents future duplicates, creating an inconsistent state where existing violations are frozen but new ones are forbidden.

  2. NULL handling: Since meeting_date is nullable, MySQL allows multiple NULL values in the unique constraint. This means multiple records with the same asha_id but NULL meeting_date will be allowed, which may not be the intended behavior.

  3. Breaking change: The original constraint enforced one record per ASHA worker total. The new constraint allows one record per ASHA worker per meeting date. This changes business logic and may affect application code expecting the old constraint.

Recommend validating and cleaning up any existing duplicate (asha_id, meeting_date) combinations before deploying this migration.

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

In `@src/main/resources/db/migration/dbiemr/V69__migratio_of_ch_dbiemr.sql` around
lines 6 - 7, Before altering the unique key on table sammelan_record, run
validation and cleanup: query for existing duplicate groups by (asha_id,
meeting_date) to detect rows violating the new constraint, resolve duplicates
(merge or delete according to business rules) or consolidate meeting_date
values, and decide how to handle NULL meeting_date (e.g., set a canonical date,
disallow NULLs, or treat NULLs uniquely) so the new UNIQUE KEY uk_asha_month
(`asha_id`, `meeting_date`) will not fail or create inconsistent historical
data; add pre-migration checks that abort the migration if duplicates remain and
document the chosen cleanup strategy in the migration comments.

@sonarqubecloud
Copy link
Copy Markdown

@paras-dba paras-dba merged commit ab297dd into main Mar 26, 2026
7 of 8 checks passed
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