处理删库重建库没有媒体数据bug

本地文件入库媒体库删除后重新入库导致0条目bug
This commit is contained in:
truewhile
2026-08-19 22:33:23 +08:00
parent 2f1f427b9e
commit 0d9ba2c33a
2 changed files with 11 additions and 1 deletions
+7
View File
@@ -51,6 +51,13 @@ func TestDeleteLibraryHardDeletesLibraryRoots(t *testing.T) {
if visibleLibraryCount != 0 { if visibleLibraryCount != 0 {
t.Fatalf("deleted library should not remain visible, count=%d", visibleLibraryCount) t.Fatalf("deleted library should not remain visible, count=%d", visibleLibraryCount)
} }
var mediaCount int64
if err := db.Unscoped().Model(&model.Media{}).Where("library_id = ?", lib.ID).Count(&mediaCount).Error; err != nil {
t.Fatal(err)
}
if mediaCount != 0 {
t.Fatalf("local library media rows should be hard deleted, count=%d", mediaCount)
}
} }
func TestDeleteCloudLibraryPurgesMountWithoutRecycleBin(t *testing.T) { func TestDeleteCloudLibraryPurgesMountWithoutRecycleBin(t *testing.T) {
+4 -1
View File
@@ -37,7 +37,10 @@ func (s *MediaService) DeleteLibrary(ctx context.Context, id string) error {
} }
} }
err = s.repo.DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error { err = s.repo.DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := tx.Where("library_id = ?", id).Delete(&model.Media{}).Error; err != nil { // 本地库媒体行统一硬删除(与云库分支一致):删库即彻底清空该库的
// media 行,避免重扫时旧行被 upsert 复活却仍挂在已删库下,导致重建
// 同名库后条目始终为 0。只作用于数据库表行,磁盘文件不受影响。
if err := tx.Unscoped().Where("library_id = ?", id).Delete(&model.Media{}).Error; err != nil {
return err return err
} }
if err := hardDeleteLibraryRoots(ctx, tx, id); err != nil { if err := hardDeleteLibraryRoots(ctx, tx, id); err != nil {