feat(library): remove tracks from the library without touching the file

"Remove from library" deletes the audio_files row and records the path
as excluded, so the next scan does not import it again. Without the
exclusion the operation undoes itself on the next scan, which is worse
than not having it at all; the file on disk is never touched, which is
the promise the confirmation copy will make.

The soft scan compares the number of audio files on disk against the
number of rows, so both walks now skip excluded paths — otherwise the
two counts disagree forever and every launch queues a full scan of the
whole library. A full rescan clears the exclusions, which is the only
way back for a path removed by mistake until there is a UI for it.
This commit is contained in:
2026-08-13 13:04:59 -04:00
parent 91bab4e73e
commit acbe7c4676
20 changed files with 854 additions and 11 deletions
@@ -343,3 +343,7 @@ JOIN audio_files af ON af.recording_id = r.id
WHERE r.mbid IN (sqlc.slice('mbids'))
AND af.library_id = ?
ORDER BY af.file_path;
-- name: GetAudioFilesByPaths :many
SELECT id, library_id, file_path, group_key FROM audio_files
WHERE file_path IN (sqlc.slice('paths'));
@@ -0,0 +1,15 @@
-- name: ExcludePath :exec
INSERT INTO excluded_paths (library_id, file_path)
VALUES (?, ?)
ON CONFLICT(library_id, file_path) DO NOTHING;
-- name: GetExcludedPathsByLibrary :many
SELECT file_path FROM excluded_paths
WHERE library_id = ?;
-- name: CountExcludedPathsByLibrary :one
SELECT COUNT(*) FROM excluded_paths
WHERE library_id = ?;
-- name: ClearExcludedPaths :exec
DELETE FROM excluded_paths;
@@ -0,0 +1,25 @@
-- Paths the user has removed from the library, which the scanner must
-- not import again.
--
-- "Remove from library" deletes the audio_files row and leaves the file
-- on disk. Without this table the next scan finds the file, sees no
-- row for it, and imports it again — so the exclusion is not an
-- enhancement, it is what makes the operation mean anything.
--
-- A row is keyed by (library_id, file_path) rather than by audio_file
-- id, because the row it names has just been deleted. ON DELETE
-- CASCADE from libraries means removing a library takes its exclusions
-- with it; a full rescan clears the table outright, which is the only
-- way back for a path removed by mistake until there is a UI for it.
CREATE TABLE IF NOT EXISTS excluded_paths (
id INTEGER PRIMARY KEY,
library_id INTEGER NOT NULL,
file_path TEXT NOT NULL,
excluded_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE(library_id, file_path),
FOREIGN KEY(library_id) REFERENCES libraries(id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS idx_excluded_paths_library
ON excluded_paths(library_id);
@@ -648,6 +648,56 @@ func (q *Queries) GetAudioFilesByLibrary(ctx context.Context, libraryID int64) (
return items, nil
}
const getAudioFilesByPaths = `-- name: GetAudioFilesByPaths :many
SELECT id, library_id, file_path, group_key FROM audio_files
WHERE file_path IN (/*SLICE:paths*/?)
`
type GetAudioFilesByPathsRow struct {
ID int64
LibraryID int64
FilePath string
GroupKey string
}
func (q *Queries) GetAudioFilesByPaths(ctx context.Context, paths []string) ([]GetAudioFilesByPathsRow, error) {
query := getAudioFilesByPaths
var queryParams []interface{}
if len(paths) > 0 {
for _, v := range paths {
queryParams = append(queryParams, v)
}
query = strings.Replace(query, "/*SLICE:paths*/?", strings.Repeat(",?", len(paths))[1:], 1)
} else {
query = strings.Replace(query, "/*SLICE:paths*/?", "NULL", 1)
}
rows, err := q.db.QueryContext(ctx, query, queryParams...)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetAudioFilesByPathsRow
for rows.Next() {
var i GetAudioFilesByPathsRow
if err := rows.Scan(
&i.ID,
&i.LibraryID,
&i.FilePath,
&i.GroupKey,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getAudioFilesByReleaseGroup = `-- name: GetAudioFilesByReleaseGroup :many
SELECT
af.file_path,
@@ -0,0 +1,75 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
// source: excluded_paths.sql
package sqlcgen
import (
"context"
)
const clearExcludedPaths = `-- name: ClearExcludedPaths :exec
DELETE FROM excluded_paths
`
func (q *Queries) ClearExcludedPaths(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, clearExcludedPaths)
return err
}
const countExcludedPathsByLibrary = `-- name: CountExcludedPathsByLibrary :one
SELECT COUNT(*) FROM excluded_paths
WHERE library_id = ?
`
func (q *Queries) CountExcludedPathsByLibrary(ctx context.Context, libraryID int64) (int64, error) {
row := q.db.QueryRowContext(ctx, countExcludedPathsByLibrary, libraryID)
var count int64
err := row.Scan(&count)
return count, err
}
const excludePath = `-- name: ExcludePath :exec
INSERT INTO excluded_paths (library_id, file_path)
VALUES (?, ?)
ON CONFLICT(library_id, file_path) DO NOTHING
`
type ExcludePathParams struct {
LibraryID int64
FilePath string
}
func (q *Queries) ExcludePath(ctx context.Context, arg ExcludePathParams) error {
_, err := q.db.ExecContext(ctx, excludePath, arg.LibraryID, arg.FilePath)
return err
}
const getExcludedPathsByLibrary = `-- name: GetExcludedPathsByLibrary :many
SELECT file_path FROM excluded_paths
WHERE library_id = ?
`
func (q *Queries) GetExcludedPathsByLibrary(ctx context.Context, libraryID int64) ([]string, error) {
rows, err := q.db.QueryContext(ctx, getExcludedPathsByLibrary, libraryID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []string
for rows.Next() {
var file_path string
if err := rows.Scan(&file_path); err != nil {
return nil, err
}
items = append(items, file_path)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
+7
View File
@@ -139,6 +139,13 @@ type DownloadRequest struct {
UpdatedAt time.Time
}
type ExcludedPath struct {
ID int64
LibraryID int64
FilePath string
ExcludedAt time.Time
}
type ExploreChampionFt struct {
Title string
ArtistName string