fix(storage): apply driver switch on save and repoint upload records

Saving storage settings now activates the selected driver immediately
instead of staging it until migration. When the driver changes, existing
upload rows are repointed to the new storage_driver so /f/{id} reads use
the correct backend. ForDriver can also open non-active drivers from the
saved multi-backend config.
This commit is contained in:
ryan
2026-06-18 13:39:07 +08:00
parent 6fa7034172
commit 1f391e9ec2
9 changed files with 134 additions and 47 deletions
@@ -7,6 +7,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/Rain-kl/Wavelet/internal/db"
@@ -87,6 +88,9 @@ func updateSystemConfig(ctx context.Context, key string, req UpdateSystemConfigR
if err := tx.Model(&config).Updates(updates).Error; err != nil {
return err
}
if err := repointUploadStorageDriversOnDriverSwitch(ctx, tx, key, originalDriver, req.Value); err != nil {
return err
}
resolveStorageMigrationTasksOnDirectDriverUpdate(ctx, tx, key, originalDriver, req.Value)
return nil
}); err != nil {
@@ -97,6 +101,38 @@ func updateSystemConfig(ctx context.Context, key string, req UpdateSystemConfigR
return nil
}
func repointUploadStorageDriversOnDriverSwitch(
ctx context.Context,
tx *gorm.DB,
key string,
originalDriver storage.Driver,
newValue string,
) error {
if key != model.ConfigKeyStorageConfig || originalDriver == "" {
return nil
}
var newCfg storage.Config
if err := json.Unmarshal([]byte(newValue), &newCfg); err != nil {
return fmt.Errorf("parse storage config for driver repoint: %w", err)
}
if newCfg.Driver == "" || newCfg.Driver == originalDriver {
return nil
}
result := tx.Model(&model.Upload{}).
Where("storage_driver = ? AND status != ?", string(originalDriver), model.UploadStatusDeleted).
Update("storage_driver", string(newCfg.Driver))
if result.Error != nil {
return result.Error
}
if result.RowsAffected > 0 {
logger.InfoF(ctx, "[StorageConfig] switched driver %s -> %s, repointed %d upload records",
originalDriver, newCfg.Driver, result.RowsAffected)
}
return nil
}
func resolveStorageMigrationTasksOnDirectDriverUpdate(
ctx context.Context,
tx *gorm.DB,