实现增量扫描
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user