Skip to content

Add the missing Columns in Elasticsearch table#112

Open
vanitha1822 wants to merge 1 commit intomainfrom
nd/vs/es_column_update
Open

Add the missing Columns in Elasticsearch table#112
vanitha1822 wants to merge 1 commit intomainfrom
nd/vs/es_column_update

Conversation

@vanitha1822
Copy link
Copy Markdown
Member

@vanitha1822 vanitha1822 commented Mar 24, 2026

📋 Description

JIRA ID:

NA


✅ Type of Change

  • 🐞 Bug fix (non-breaking change which resolves an issue)

Summary by CodeRabbit

  • Performance

    • Optimized database indexing for improved query performance and system efficiency
    • Enhanced infrastructure for better tracking of system operations and data management
  • Chores

    • Database schema refinements to improve overall system reliability and stability

@vanitha1822 vanitha1822 requested a review from paras-dba March 24, 2026 06:35
@vanitha1822 vanitha1822 self-assigned this Mar 24, 2026
@sonarqubecloud
Copy link
Copy Markdown

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 24, 2026

📝 Walkthrough

Walkthrough

A new database migration adds columns (triggered_by, created_date, last_updated) to the t_elasticsearch_sync_job table in the db_identity schema, modifies existing column properties, removes obsolete indexes, and creates new ones using conditional SQL statements.

Changes

Cohort / File(s) Summary
Database Schema Migration
src/main/resources/db/migration/dbidentity/V12__Elasticsearch_Columns.sql
Conditionally adds three new columns to t_elasticsearch_sync_job; unconditionally modifies existing column defaults and nullability; removes legacy indexes (idx_job_status, idx_started_at, idx_status_started_at); adds new indexes (idx_status, idx_created_date, idx_job_type) with existence checks.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐰 A migration hops along with care,
Adding columns here and there,
Elasticsearch jobs now align,
New indexes help them shine,
Schema's dance, so defined!

🚥 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 title 'Add the missing Columns in Elasticsearch table' accurately reflects the main change: adding three new columns (triggered_by, created_date, last_updated) to the t_elasticsearch_sync_job table via database 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 nd/vs/es_column_update

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/dbidentity/V12__Elasticsearch_Columns.sql`:
- Around line 46-52: The ALTER statement incorrectly narrows current_offset to
INT and drops NOT NULL on counters; update the ALTER for table
db_identity.t_elasticsearch_sync_job so that current_offset is BIGINT NOT NULL
DEFAULT 0 and the counter columns total_records, processed_records,
success_count, and failure_count are defined as BIGINT NOT NULL DEFAULT 0 (leave
status as VARCHAR(50) NOT NULL DEFAULT 'PENDING') to restore the original types
and NOT NULL constraints.
- Around line 19-30: The migration currently adds created_date as NOT NULL with
DEFAULT CURRENT_TIMESTAMP which stamps existing rows with the migration time;
instead, first add created_date as a nullable column (use the same
`@preparedStatement` / PREPARE stmt pattern to execute an ALTER TABLE ... ADD
COLUMN created_date TIMESTAMP NULL AFTER triggered_by), then run an UPDATE to
backfill created_date from started_at (e.g., UPDATE <table> SET created_date =
started_at WHERE created_date IS NULL), and finally ALTER TABLE to make
created_date NOT NULL with DEFAULT CURRENT_TIMESTAMP and then create
idx_created_date; keep using the same `@preparedStatement/PREPARE` stmt/EXECUTE
stmt/DEALLOCATE PREPARE pattern around each DDL/UPDATE.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9a0aab65-1f7d-43c0-addc-b84975ced6c1

📥 Commits

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

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

Comment on lines +19 to +30
-- Add created_date
SET @columnname = 'created_date';
SET @preparedStatement = (SELECT IF(
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND COLUMN_NAME = @columnname) > 0,
'SELECT 1',
CONCAT('ALTER TABLE ', @dbname, '.', @tablename,
' ADD COLUMN created_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER triggered_by;')
));
PREPARE stmt FROM @preparedStatement;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
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:

# First, let's see the full context of both migration files
cd src/main/resources/db/migration/dbidentity && ls -la V*.sql | head -20

Repository: PSMRI/AMRIT-DB

Length of output: 1026


🏁 Script executed:

# Check the V7 migration to understand the existing schema
cat src/main/resources/db/migration/dbidentity/V7__ELASTICSEARCH.sql

Repository: PSMRI/AMRIT-DB

Length of output: 952


🏁 Script executed:

# Check the V12 migration file in full
cat src/main/resources/db/migration/dbidentity/V12__Elasticsearch_Columns.sql

Repository: PSMRI/AMRIT-DB

Length of output: 4695


🌐 Web query:

MySQL ADD COLUMN NOT NULL DEFAULT CURRENT_TIMESTAMP existing rows behavior

💡 Result:

When adding a column to an existing MySQL table using ALTER TABLE ADD COLUMN with NOT NULL and DEFAULT CURRENT_TIMESTAMP, the existing rows are populated with the default value, which is the current timestamp at the time the ALTER statement executes.

Citations:


Backfill created_date from started_at before making it non-null.

Line 26 will stamp every pre-existing job record with the migration execution time, not the row's original start time. When MySQL executes ADD COLUMN created_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, all existing rows are populated with the current timestamp at that moment—not the historical started_at value. This causes two problems: (1) historical chronology is lost, and (2) the idx_created_date index will be built on incorrect timestamps.

Add created_date as nullable first, backfill from started_at, then alter it to NOT NULL:

Suggested fix
-         ' ADD COLUMN created_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER triggered_by;')
+         ' ADD COLUMN created_date TIMESTAMP NULL AFTER triggered_by;')
 ));
 PREPARE stmt FROM `@preparedStatement`;
 EXECUTE stmt;
 DEALLOCATE PREPARE stmt;
+
+UPDATE db_identity.t_elasticsearch_sync_job
+SET created_date = COALESCE(started_at, CURRENT_TIMESTAMP)
+WHERE created_date IS NULL;
+
+ALTER TABLE db_identity.t_elasticsearch_sync_job
+MODIFY COLUMN created_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
-- Add created_date
SET @columnname = 'created_date';
SET @preparedStatement = (SELECT IF(
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND COLUMN_NAME = @columnname) > 0,
'SELECT 1',
CONCAT('ALTER TABLE ', @dbname, '.', @tablename,
' ADD COLUMN created_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER triggered_by;')
));
PREPARE stmt FROM @preparedStatement;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- Add created_date
SET `@columnname` = 'created_date';
SET `@preparedStatement` = (SELECT IF(
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = `@dbname` AND TABLE_NAME = `@tablename` AND COLUMN_NAME = `@columnname`) > 0,
'SELECT 1',
CONCAT('ALTER TABLE ', `@dbname`, '.', `@tablename`,
' ADD COLUMN created_date TIMESTAMP NULL AFTER triggered_by;')
));
PREPARE stmt FROM `@preparedStatement`;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
UPDATE db_identity.t_elasticsearch_sync_job
SET created_date = COALESCE(started_at, CURRENT_TIMESTAMP)
WHERE created_date IS NULL;
ALTER TABLE db_identity.t_elasticsearch_sync_job
MODIFY COLUMN created_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/main/resources/db/migration/dbidentity/V12__Elasticsearch_Columns.sql`
around lines 19 - 30, The migration currently adds created_date as NOT NULL with
DEFAULT CURRENT_TIMESTAMP which stamps existing rows with the migration time;
instead, first add created_date as a nullable column (use the same
`@preparedStatement` / PREPARE stmt pattern to execute an ALTER TABLE ... ADD
COLUMN created_date TIMESTAMP NULL AFTER triggered_by), then run an UPDATE to
backfill created_date from started_at (e.g., UPDATE <table> SET created_date =
started_at WHERE created_date IS NULL), and finally ALTER TABLE to make
created_date NOT NULL with DEFAULT CURRENT_TIMESTAMP and then create
idx_created_date; keep using the same `@preparedStatement/PREPARE` stmt/EXECUTE
stmt/DEALLOCATE PREPARE pattern around each DDL/UPDATE.

Comment on lines +46 to +52
ALTER TABLE db_identity.t_elasticsearch_sync_job
MODIFY COLUMN status VARCHAR(50) NOT NULL DEFAULT 'PENDING',
MODIFY COLUMN total_records BIGINT DEFAULT 0,
MODIFY COLUMN processed_records BIGINT DEFAULT 0,
MODIFY COLUMN success_count BIGINT DEFAULT 0,
MODIFY COLUMN failure_count BIGINT DEFAULT 0,
MODIFY COLUMN current_offset INT DEFAULT 0,
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 | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

fd "V7__ELASTICSEARCH.sql" src/

Repository: PSMRI/AMRIT-DB

Length of output: 122


🏁 Script executed:

fd "V12__Elasticsearch_Columns.sql" src/

Repository: PSMRI/AMRIT-DB

Length of output: 131


🏁 Script executed:

cat -n src/main/resources/db/migration/dbidentity/V7__ELASTICSEARCH.sql

Repository: PSMRI/AMRIT-DB

Length of output: 1092


🏁 Script executed:

cat -n src/main/resources/db/migration/dbidentity/V12__Elasticsearch_Columns.sql

Repository: PSMRI/AMRIT-DB

Length of output: 5563


Fix the counter column types and constraints in this ALTER statement.

Line 52 narrows current_offset from BIGINT to INT; the original V7 schema explicitly defined it as BIGINT NOT NULL DEFAULT 0 to prevent offset overflow on large jobs. The same block also removes NOT NULL from all five counter columns (lines 48–51), which violates the original schema design. On large sync jobs, the INT offset can overflow, and missing NOT NULL constraints allow unexpected NULL values in counter fields.

🔧 Restore original types and NOT NULL constraints
 ALTER TABLE db_identity.t_elasticsearch_sync_job
 MODIFY COLUMN status VARCHAR(50) NOT NULL DEFAULT 'PENDING',
- MODIFY COLUMN total_records BIGINT DEFAULT 0,
- MODIFY COLUMN processed_records BIGINT DEFAULT 0,
- MODIFY COLUMN success_count BIGINT DEFAULT 0,
- MODIFY COLUMN failure_count BIGINT DEFAULT 0,
- MODIFY COLUMN current_offset INT DEFAULT 0,
+ MODIFY COLUMN total_records BIGINT NOT NULL DEFAULT 0,
+ MODIFY COLUMN processed_records BIGINT NOT NULL DEFAULT 0,
+ MODIFY COLUMN success_count BIGINT NOT NULL DEFAULT 0,
+ MODIFY COLUMN failure_count BIGINT NOT NULL DEFAULT 0,
+ MODIFY COLUMN current_offset BIGINT NOT NULL DEFAULT 0,
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
ALTER TABLE db_identity.t_elasticsearch_sync_job
MODIFY COLUMN status VARCHAR(50) NOT NULL DEFAULT 'PENDING',
MODIFY COLUMN total_records BIGINT DEFAULT 0,
MODIFY COLUMN processed_records BIGINT DEFAULT 0,
MODIFY COLUMN success_count BIGINT DEFAULT 0,
MODIFY COLUMN failure_count BIGINT DEFAULT 0,
MODIFY COLUMN current_offset INT DEFAULT 0,
ALTER TABLE db_identity.t_elasticsearch_sync_job
MODIFY COLUMN status VARCHAR(50) NOT NULL DEFAULT 'PENDING',
MODIFY COLUMN total_records BIGINT NOT NULL DEFAULT 0,
MODIFY COLUMN processed_records BIGINT NOT NULL DEFAULT 0,
MODIFY COLUMN success_count BIGINT NOT NULL DEFAULT 0,
MODIFY COLUMN failure_count BIGINT NOT NULL DEFAULT 0,
MODIFY COLUMN current_offset BIGINT 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/dbidentity/V12__Elasticsearch_Columns.sql`
around lines 46 - 52, The ALTER statement incorrectly narrows current_offset to
INT and drops NOT NULL on counters; update the ALTER for table
db_identity.t_elasticsearch_sync_job so that current_offset is BIGINT NOT NULL
DEFAULT 0 and the counter columns total_records, processed_records,
success_count, and failure_count are defined as BIGINT NOT NULL DEFAULT 0 (leave
status as VARCHAR(50) NOT NULL DEFAULT 'PENDING') to restore the original types
and NOT NULL constraints.

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.

1 participant