数据库迁移优化,增强外键约束控制,改进日志输出

This commit is contained in:
2026-01-27 10:27:25 +08:00
parent 0fe45e3847
commit 729d335a69
6 changed files with 59 additions and 25 deletions

View File

@@ -18,8 +18,10 @@ import (
"BingPaper/internal/repo"
"BingPaper/internal/storage"
"BingPaper/internal/util"
"github.com/disintegration/imaging"
"go.uber.org/zap"
"gorm.io/gorm/clause"
)
type BingResponse struct {
@@ -109,10 +111,22 @@ func (f *Fetcher) processImage(ctx context.Context, bingImg BingImage) error {
Quiz: bingImg.Quiz,
}
if err := repo.DB.Create(&dbImg).Error; err != nil {
if err := repo.DB.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "date"}},
DoNothing: true,
}).Create(&dbImg).Error; err != nil {
return err
}
// 再次检查 dbImg.ID 是否被填充,如果没有被填充(说明由于冲突未插入),则需要查询出已有的 ID
if dbImg.ID == 0 {
var existing model.Image
if err := repo.DB.Where("date = ?", dateStr).First(&existing).Error; err != nil {
return err
}
dbImg = existing
}
// 保存各种分辨率
variants := []struct {
name string
@@ -194,27 +208,38 @@ func (f *Fetcher) saveVariant(ctx context.Context, img *model.Image, variant, fo
Size: int64(len(data)),
}
return repo.DB.Create(&vRecord).Error
return repo.DB.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "image_id"}, {Name: "variant"}, {Name: "format"}},
DoNothing: true,
}).Create(&vRecord).Error
}
func (f *Fetcher) saveDailyFiles(srcImg image.Image, originalData []byte) {
util.Logger.Info("Saving daily files")
localRoot := config.GetConfig().Storage.Local.Root
if config.GetConfig().Storage.Type != "local" {
// 如果不是本地存储,保存在临时目录或指定缓存目录
// 如果不是本地存储,保存在静态资源目录
localRoot = "static"
}
os.MkdirAll(filepath.Join(localRoot, "static"), 0755)
if err := os.MkdirAll(localRoot, 0755); err != nil {
util.Logger.Error("Failed to create directory", zap.String("path", localRoot), zap.Error(err))
return
}
// daily.jpeg (quality 95)
jpegPath := filepath.Join(localRoot, "static", "daily.jpeg")
fJpeg, _ := os.Create(jpegPath)
if fJpeg != nil {
jpegPath := filepath.Join(localRoot, "daily.jpeg")
fJpeg, err := os.Create(jpegPath)
if err != nil {
util.Logger.Error("Failed to create daily.jpeg", zap.Error(err))
} else {
jpeg.Encode(fJpeg, srcImg, &jpeg.Options{Quality: 95})
fJpeg.Close()
}
// original.jpeg (quality 100)
originalPath := filepath.Join(localRoot, "static", "original.jpeg")
os.WriteFile(originalPath, originalData, 0644)
originalPath := filepath.Join(localRoot, "original.jpeg")
if err := os.WriteFile(originalPath, originalData, 0644); err != nil {
util.Logger.Error("Failed to write original.jpeg", zap.Error(err))
}
}

View File

@@ -35,10 +35,14 @@ func CleanupOldImages(ctx context.Context) error {
util.Logger.Warn("Failed to delete storage object", zap.String("key", v.StorageKey), zap.Error(err))
}
}
// 删除 DB 记录 (级联删除由代码处理,或者 GORM 会处理已加载的关联吗?)
// 简单起见,手动删除关联
repo.DB.Where("image_id = ?", img.ID).Delete(&model.ImageVariant{})
repo.DB.Delete(&img)
// 删除关联记录(逻辑外键控制)
if err := repo.DB.Where("image_id = ?", img.ID).Delete(&model.ImageVariant{}).Error; err != nil {
util.Logger.Error("Failed to delete variants", zap.Uint("image_id", img.ID), zap.Error(err))
}
// 删除主表记录
if err := repo.DB.Delete(&img).Error; err != nil {
util.Logger.Error("Failed to delete image", zap.Uint("id", img.ID), zap.Error(err))
}
}
util.Logger.Info("Cleanup task completed", zap.Int("deleted_count", len(images)))