processBatchToTempTable processes a batch of keys and writes to temporary table
(keys []models.APIKey, oldService, newService encryption.Service)
| 514 | |
| 515 | // processBatchToTempTable processes a batch of keys and writes to temporary table |
| 516 | func (cmd *MigrateKeysCommand) processBatchToTempTable(keys []models.APIKey, oldService, newService encryption.Service) error { |
| 517 | // Prepare batch data for insertion |
| 518 | type TempMigration struct { |
| 519 | ID uint `gorm:"primaryKey"` |
| 520 | KeyValueNew string `gorm:"column:key_value_new"` |
| 521 | KeyHashNew string `gorm:"column:key_hash_new"` |
| 522 | } |
| 523 | |
| 524 | var tempRecords []TempMigration |
| 525 | |
| 526 | for _, key := range keys { |
| 527 | // 1. Decrypt using old service |
| 528 | decrypted, err := oldService.Decrypt(key.KeyValue) |
| 529 | if err != nil { |
| 530 | return fmt.Errorf("key ID %d decryption failed: %w", key.ID, err) |
| 531 | } |
| 532 | |
| 533 | // 2. Encrypt using new service |
| 534 | encrypted, err := newService.Encrypt(decrypted) |
| 535 | if err != nil { |
| 536 | return fmt.Errorf("key ID %d encryption failed: %w", key.ID, err) |
| 537 | } |
| 538 | |
| 539 | // 3. Generate new hash using new service |
| 540 | newHash := newService.Hash(decrypted) |
| 541 | |
| 542 | tempRecords = append(tempRecords, TempMigration{ |
| 543 | ID: key.ID, |
| 544 | KeyValueNew: encrypted, |
| 545 | KeyHashNew: newHash, |
| 546 | }) |
| 547 | } |
| 548 | |
| 549 | // Insert batch into temp table in a transaction |
| 550 | return cmd.db.Transaction(func(tx *gorm.DB) error { |
| 551 | if err := tx.Table("temp_migration").Create(&tempRecords).Error; err != nil { |
| 552 | return fmt.Errorf("failed to insert batch into temp_migration: %w", err) |
| 553 | } |
| 554 | return nil |
| 555 | }) |
| 556 | } |
| 557 | |
| 558 | // verifyTempColumns verifies temporary table data integrity |
| 559 | func (cmd *MigrateKeysCommand) verifyTempColumns() error { |
no test coverage detected