MCPcopy Create free account
hub / github.com/tbphp/gpt-load / createBackupTableAndMigrate

Method createBackupTableAndMigrate

internal/commands/migrate.go:349–403  ·  view source on GitHub ↗

createBackupTableAndMigrate performs migration using temporary table

()

Source from the content-addressed store, hash-verified

347
348// createBackupTableAndMigrate performs migration using temporary table
349func (cmd *MigrateKeysCommand) createBackupTableAndMigrate() error {
350 logrus.Info("Starting key migration using temporary table...")
351
352 // 1. Create temporary table
353 if err := cmd.createTempTable(); err != nil {
354 return fmt.Errorf("failed to create temporary table: %w", err)
355 }
356
357 // 2. Create old and new encryption services
358 oldService, newService, err := cmd.createMigrationServices()
359 if err != nil {
360 return err
361 }
362
363 // 3. Get total count to migrate
364 var totalCount int64
365 if err := cmd.db.Model(&models.APIKey{}).Count(&totalCount).Error; err != nil {
366 return fmt.Errorf("failed to get key count: %w", err)
367 }
368
369 if totalCount == 0 {
370 logrus.Info("No keys to migrate")
371 return nil
372 }
373
374 logrus.Infof("Starting migration of %d keys...", totalCount)
375
376 // 4. Process migration in batches
377 processedCount := 0
378 lastID := uint(0)
379
380 for {
381 var keys []models.APIKey
382 // Use ID-based pagination for stable results
383 if err := cmd.db.Where("id > ?", lastID).Order("id").Limit(migrationBatchSize).Find(&keys).Error; err != nil {
384 return fmt.Errorf("failed to get key data: %w", err)
385 }
386
387 if len(keys) == 0 {
388 break
389 }
390
391 // Process current batch to temp table
392 if err := cmd.processBatchToTempTable(keys, oldService, newService); err != nil {
393 return fmt.Errorf("failed to process batch data: %w", err)
394 }
395
396 processedCount += len(keys)
397 lastID = keys[len(keys)-1].ID
398 logrus.Infof("Processed %d/%d keys", processedCount, totalCount)
399 }
400
401 logrus.Info("Data migration to temporary table completed")
402 return nil
403}
404
405// createTempTable creates a temporary table for migration
406func (cmd *MigrateKeysCommand) createTempTable() error {

Callers 1

ExecuteMethod · 0.95

Calls 3

createTempTableMethod · 0.95

Tested by

no test coverage detected