实现增量扫描
All checks were successful
Go CI / test-and-build (push) Successful in 13s
Web CI / lint-test-build (push) Successful in 31s

This commit is contained in:
2026-04-11 14:38:23 +08:00
parent 3f82e29c6c
commit 79d47c81e7
10 changed files with 351 additions and 91 deletions

View File

@@ -76,14 +76,34 @@ func (r *LibraryRepository) Delete(id int) error {
}
defer tx.Rollback()
// Delete the library — CASCADE removes media_files and songs automatically
_, err = tx.Exec("DELETE FROM libraries WHERE id = ?", id)
if err != nil {
return err
}
// Clean up orphan albums (no songs reference them)
_, err = tx.Exec(`
if err := cleanupOrphansTx(tx); err != nil {
return err
}
return tx.Commit()
}
func (r *LibraryRepository) CleanupOrphans() error {
tx, err := r.db.Begin()
if err != nil {
return err
}
defer tx.Rollback()
if err := cleanupOrphansTx(tx); err != nil {
return err
}
return tx.Commit()
}
func cleanupOrphansTx(tx *sql.Tx) error {
_, err := tx.Exec(`
DELETE FROM albums WHERE id NOT IN (
SELECT DISTINCT album_id FROM songs WHERE album_id IS NOT NULL
)
@@ -92,7 +112,6 @@ func (r *LibraryRepository) Delete(id int) error {
return err
}
// Clean up orphan artists (no songs and no albums reference them)
_, err = tx.Exec(`
DELETE FROM artists WHERE id NOT IN (
SELECT DISTINCT artist_id FROM songs WHERE artist_id IS NOT NULL
@@ -104,5 +123,5 @@ func (r *LibraryRepository) Delete(id int) error {
return err
}
return tx.Commit()
return nil
}