-
Notifications
You must be signed in to change notification settings - Fork 16
Add the missing Columns in Elasticsearch table #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,124 @@ | ||||||||||||||||||||||||||||||
| USE db_identity; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| SET @dbname = 'db_identity'; | ||||||||||||||||||||||||||||||
| SET @tablename = 't_elasticsearch_sync_job'; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| -- Add triggered_by | ||||||||||||||||||||||||||||||
| SET @columnname = 'triggered_by'; | ||||||||||||||||||||||||||||||
| 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', | ||||||||||||||||||||||||||||||
|
Check failure on line 11 in src/main/resources/db/migration/dbidentity/V12__Elasticsearch_Columns.sql
|
||||||||||||||||||||||||||||||
| CONCAT('ALTER TABLE ', @dbname, '.', @tablename, | ||||||||||||||||||||||||||||||
|
Check failure on line 12 in src/main/resources/db/migration/dbidentity/V12__Elasticsearch_Columns.sql
|
||||||||||||||||||||||||||||||
| ' ADD COLUMN triggered_by VARCHAR(100) DEFAULT NULL AFTER error_message;') | ||||||||||||||||||||||||||||||
| )); | ||||||||||||||||||||||||||||||
| 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 NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER triggered_by;') | ||||||||||||||||||||||||||||||
| )); | ||||||||||||||||||||||||||||||
| PREPARE stmt FROM @preparedStatement; | ||||||||||||||||||||||||||||||
| EXECUTE stmt; | ||||||||||||||||||||||||||||||
| DEALLOCATE PREPARE stmt; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| -- Add last_updated | ||||||||||||||||||||||||||||||
| SET @columnname = 'last_updated'; | ||||||||||||||||||||||||||||||
| 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 last_updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP AFTER created_date;') | ||||||||||||||||||||||||||||||
| )); | ||||||||||||||||||||||||||||||
| PREPARE stmt FROM @preparedStatement; | ||||||||||||||||||||||||||||||
| EXECUTE stmt; | ||||||||||||||||||||||||||||||
| DEALLOCATE PREPARE stmt; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| -- Modify columns (always execute) | ||||||||||||||||||||||||||||||
| 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, | ||||||||||||||||||||||||||||||
|
Comment on lines
+46
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π§© 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.sqlRepository: PSMRI/AMRIT-DB Length of output: 1092 π Script executed: cat -n src/main/resources/db/migration/dbidentity/V12__Elasticsearch_Columns.sqlRepository: PSMRI/AMRIT-DB Length of output: 5563 Fix the counter column types and constraints in this ALTER statement. Line 52 narrows π§ 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
Suggested change
π€ Prompt for AI Agents |
||||||||||||||||||||||||||||||
| MODIFY COLUMN started_at TIMESTAMP NULL DEFAULT NULL, | ||||||||||||||||||||||||||||||
| MODIFY COLUMN completed_at TIMESTAMP NULL DEFAULT NULL, | ||||||||||||||||||||||||||||||
| MODIFY COLUMN estimated_time_remaining BIGINT NULL AFTER last_updated, | ||||||||||||||||||||||||||||||
| MODIFY COLUMN processing_speed DOUBLE NULL AFTER estimated_time_remaining; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| -- Drop indexes if exist | ||||||||||||||||||||||||||||||
| SET @indexname = 'idx_job_status'; | ||||||||||||||||||||||||||||||
| SET @preparedStatement = (SELECT IF( | ||||||||||||||||||||||||||||||
| (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS | ||||||||||||||||||||||||||||||
| WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND INDEX_NAME = @indexname) > 0, | ||||||||||||||||||||||||||||||
| CONCAT('DROP INDEX ', @indexname, ' ON ', @dbname, '.', @tablename), | ||||||||||||||||||||||||||||||
|
Check failure on line 63 in src/main/resources/db/migration/dbidentity/V12__Elasticsearch_Columns.sql
|
||||||||||||||||||||||||||||||
| 'SELECT 1' | ||||||||||||||||||||||||||||||
| )); | ||||||||||||||||||||||||||||||
| PREPARE stmt FROM @preparedStatement; | ||||||||||||||||||||||||||||||
| EXECUTE stmt; | ||||||||||||||||||||||||||||||
| DEALLOCATE PREPARE stmt; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| SET @indexname = 'idx_started_at'; | ||||||||||||||||||||||||||||||
| SET @preparedStatement = (SELECT IF( | ||||||||||||||||||||||||||||||
| (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS | ||||||||||||||||||||||||||||||
| WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND INDEX_NAME = @indexname) > 0, | ||||||||||||||||||||||||||||||
| CONCAT('DROP INDEX ', @indexname, ' ON ', @dbname, '.', @tablename), | ||||||||||||||||||||||||||||||
| 'SELECT 1' | ||||||||||||||||||||||||||||||
| )); | ||||||||||||||||||||||||||||||
| PREPARE stmt FROM @preparedStatement; | ||||||||||||||||||||||||||||||
| EXECUTE stmt; | ||||||||||||||||||||||||||||||
| DEALLOCATE PREPARE stmt; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| SET @indexname = 'idx_status_started_at'; | ||||||||||||||||||||||||||||||
| SET @preparedStatement = (SELECT IF( | ||||||||||||||||||||||||||||||
| (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS | ||||||||||||||||||||||||||||||
| WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND INDEX_NAME = @indexname) > 0, | ||||||||||||||||||||||||||||||
| CONCAT('DROP INDEX ', @indexname, ' ON ', @dbname, '.', @tablename), | ||||||||||||||||||||||||||||||
| 'SELECT 1' | ||||||||||||||||||||||||||||||
| )); | ||||||||||||||||||||||||||||||
| PREPARE stmt FROM @preparedStatement; | ||||||||||||||||||||||||||||||
| EXECUTE stmt; | ||||||||||||||||||||||||||||||
| DEALLOCATE PREPARE stmt; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| -- Add new indexes (only if not exist) | ||||||||||||||||||||||||||||||
| SET @indexname = 'idx_status'; | ||||||||||||||||||||||||||||||
| SET @preparedStatement = (SELECT IF( | ||||||||||||||||||||||||||||||
| (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS | ||||||||||||||||||||||||||||||
| WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND INDEX_NAME = @indexname) > 0, | ||||||||||||||||||||||||||||||
| 'SELECT 1', | ||||||||||||||||||||||||||||||
| CONCAT('CREATE INDEX idx_status ON ', @dbname, '.', @tablename, ' (status);') | ||||||||||||||||||||||||||||||
| )); | ||||||||||||||||||||||||||||||
| PREPARE stmt FROM @preparedStatement; | ||||||||||||||||||||||||||||||
| EXECUTE stmt; | ||||||||||||||||||||||||||||||
| DEALLOCATE PREPARE stmt; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| SET @indexname = 'idx_created_date'; | ||||||||||||||||||||||||||||||
| SET @preparedStatement = (SELECT IF( | ||||||||||||||||||||||||||||||
| (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS | ||||||||||||||||||||||||||||||
| WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND INDEX_NAME = @indexname) > 0, | ||||||||||||||||||||||||||||||
| 'SELECT 1', | ||||||||||||||||||||||||||||||
| CONCAT('CREATE INDEX idx_created_date ON ', @dbname, '.', @tablename, ' (created_date);') | ||||||||||||||||||||||||||||||
| )); | ||||||||||||||||||||||||||||||
| PREPARE stmt FROM @preparedStatement; | ||||||||||||||||||||||||||||||
| EXECUTE stmt; | ||||||||||||||||||||||||||||||
| DEALLOCATE PREPARE stmt; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| SET @indexname = 'idx_job_type'; | ||||||||||||||||||||||||||||||
| SET @preparedStatement = (SELECT IF( | ||||||||||||||||||||||||||||||
| (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS | ||||||||||||||||||||||||||||||
| WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND INDEX_NAME = @indexname) > 0, | ||||||||||||||||||||||||||||||
| 'SELECT 1', | ||||||||||||||||||||||||||||||
| CONCAT('CREATE INDEX idx_job_type ON ', @dbname, '.', @tablename, ' (job_type);') | ||||||||||||||||||||||||||||||
| )); | ||||||||||||||||||||||||||||||
| PREPARE stmt FROM @preparedStatement; | ||||||||||||||||||||||||||||||
| EXECUTE stmt; | ||||||||||||||||||||||||||||||
| DEALLOCATE PREPARE stmt; | ||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π§© Analysis chain
π Script executed:
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.sqlRepository: 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.sqlRepository: 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_datefromstarted_atbefore 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 historicalstarted_atvalue. This causes two problems: (1) historical chronology is lost, and (2) theidx_created_dateindex will be built on incorrect timestamps.Add
created_dateas nullable first, backfill fromstarted_at, then alter it toNOT NULL:Suggested fix
π Committable suggestion
π€ Prompt for AI Agents