fix(13-02): auto-resolve phantom playlist tracks after library scan
- Add phantom_file_path column to playlist_tracks (migration 7) - Store original file_path during RemoveLibrary phantom metadata population - After each successful scan, UPDATE phantom tracks whose phantom_file_path now matches an audio_files row, re-linking them and clearing phantom metadata - Update schema file, sqlc generated code, and database test for new column
This commit is contained in:
@@ -312,6 +312,16 @@ func runMigrations(
|
||||
}
|
||||
}
|
||||
|
||||
// Migration 7: add phantom_file_path to playlist_tracks
|
||||
// for automatic phantom resolution after library re-scans.
|
||||
if version < 7 {
|
||||
if err := migration7PhantomFilePath(
|
||||
ctx, db, logger,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1030,6 +1040,44 @@ func migration6MultiLibrary(
|
||||
return nil
|
||||
}
|
||||
|
||||
// migration7PhantomFilePath adds the phantom_file_path column to
|
||||
// playlist_tracks so that phantom entries can be automatically
|
||||
// re-linked to audio_files after a library scan.
|
||||
func migration7PhantomFilePath(
|
||||
ctx context.Context,
|
||||
db *sql.DB,
|
||||
logger *slog.Logger,
|
||||
) error {
|
||||
logger.Info(
|
||||
"applying migration 7: phantom_file_path column",
|
||||
)
|
||||
|
||||
// SAFETY: ALTER TABLE ADD COLUMN for new nullable column.
|
||||
if _, err := db.ExecContext(ctx,
|
||||
`ALTER TABLE playlist_tracks
|
||||
ADD COLUMN phantom_file_path TEXT`,
|
||||
); err != nil {
|
||||
if !isDuplicateColumnErr(err) {
|
||||
return fmt.Errorf(
|
||||
"migration 7: could not add column: %w",
|
||||
err,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := db.ExecContext(
|
||||
ctx, "PRAGMA user_version = 7",
|
||||
); err != nil {
|
||||
return fmt.Errorf(
|
||||
"could not set user_version to 7: %w", err,
|
||||
)
|
||||
}
|
||||
|
||||
logger.Info("migration 7 complete")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// readLibraryDirFromTOML reads the TOML config file and returns
|
||||
// the Library.DirectoryPath value, or "" if not configured.
|
||||
func readLibraryDirFromTOML(logger *slog.Logger) string {
|
||||
|
||||
@@ -91,6 +91,7 @@ func TestMigration6FreshDB(t *testing.T) {
|
||||
"phantom_duration_ms": false,
|
||||
"phantom_genre": false,
|
||||
"phantom_cover_art_path": false,
|
||||
"phantom_file_path": false,
|
||||
}
|
||||
|
||||
audioFileIDNullable := false
|
||||
@@ -171,7 +172,7 @@ func TestMigration6FreshDB(t *testing.T) {
|
||||
t.Error("track_metadata VIEW does not contain library_id")
|
||||
}
|
||||
|
||||
// Verify user_version >= 6.
|
||||
// Verify user_version >= 7.
|
||||
var version int
|
||||
|
||||
verRows, err := db.QueryContext("PRAGMA user_version")
|
||||
@@ -193,8 +194,8 @@ func TestMigration6FreshDB(t *testing.T) {
|
||||
|
||||
_ = verRows.Close()
|
||||
|
||||
if version < 6 {
|
||||
t.Errorf("user_version = %d, want >= 6", version)
|
||||
if version < 7 {
|
||||
t.Errorf("user_version = %d, want >= 7", version)
|
||||
}
|
||||
|
||||
// Verify libraries table has only the sentinel row on fresh DB.
|
||||
|
||||
@@ -9,6 +9,7 @@ CREATE TABLE IF NOT EXISTS playlist_tracks (
|
||||
phantom_duration_ms INTEGER,
|
||||
phantom_genre TEXT,
|
||||
phantom_cover_art_path TEXT,
|
||||
phantom_file_path TEXT,
|
||||
FOREIGN KEY(playlist_id) REFERENCES playlists(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(audio_file_id) REFERENCES audio_files(id) ON DELETE SET NULL
|
||||
);
|
||||
|
||||
@@ -90,6 +90,7 @@ type PlaylistTrack struct {
|
||||
PhantomDurationMs sql.NullInt64
|
||||
PhantomGenre sql.NullString
|
||||
PhantomCoverArtPath sql.NullString
|
||||
PhantomFilePath sql.NullString
|
||||
}
|
||||
|
||||
type Queue struct {
|
||||
|
||||
@@ -16,7 +16,7 @@ INSERT INTO playlist_tracks (
|
||||
phantom_title, phantom_artist, phantom_album,
|
||||
phantom_duration_ms, phantom_genre, phantom_cover_art_path
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
RETURNING id, playlist_id, audio_file_id, position, phantom_title, phantom_artist, phantom_album, phantom_duration_ms, phantom_genre, phantom_cover_art_path
|
||||
RETURNING id, playlist_id, audio_file_id, position, phantom_title, phantom_artist, phantom_album, phantom_duration_ms, phantom_genre, phantom_cover_art_path, phantom_file_path
|
||||
`
|
||||
|
||||
type AddPlaylistTrackParams struct {
|
||||
@@ -55,6 +55,7 @@ func (q *Queries) AddPlaylistTrack(ctx context.Context, arg AddPlaylistTrackPara
|
||||
&i.PhantomDurationMs,
|
||||
&i.PhantomGenre,
|
||||
&i.PhantomCoverArtPath,
|
||||
&i.PhantomFilePath,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user