-- +goose Up ALTER TABLE library_sources ADD COLUMN excluded_dirs text[] NOT NULL DEFAULT '{}'; -- +goose StatementBegin CREATE FUNCTION library_path_excluded(rel_path text, excluded_dirs text[]) RETURNS boolean LANGUAGE sql IMMUTABLE PARALLEL SAFE AS $$ SELECT EXISTS ( SELECT 1 FROM unnest(excluded_dirs) AS d(dir) WHERE rel_path = dir OR starts_with(rel_path, dir || '/') ); $$; -- +goose StatementEnd -- Keep historical jobs and files; only tracks whose every known file is excluded are suppressed. -- +goose StatementBegin CREATE FUNCTION library_track_scan_excluded(track uuid) RETURNS boolean LANGUAGE sql STABLE AS $$ SELECT EXISTS (SELECT 1 FROM media_files WHERE track_id = track) AND NOT EXISTS ( SELECT 1 FROM media_files f JOIN library_sources s ON s.id = f.source_id WHERE f.track_id = track AND NOT library_path_excluded(f.rel_path, s.excluded_dirs) ); $$; -- +goose StatementEnd -- +goose Down DROP FUNCTION library_track_scan_excluded(uuid); DROP FUNCTION library_path_excluded(text, text[]); ALTER TABLE library_sources DROP COLUMN excluded_dirs;