From b408d11ea1ff4409a3e74c962114c84d147dbb79 Mon Sep 17 00:00:00 2001 From: Roland Date: Fri, 26 Dec 2025 10:00:32 -0700 Subject: [PATCH] Deleting orphaned collections when no channel is in the channels config. --- modules/videos/lib.py | 13 ++++++++- modules/videos/test/test_common.py | 43 ++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/modules/videos/lib.py b/modules/videos/lib.py index 425b1e52..be5b3bcf 100644 --- a/modules/videos/lib.py +++ b/modules/videos/lib.py @@ -362,12 +362,23 @@ class ChannelsConfig(ConfigFile): ) with get_db_session(commit=True) as session: - # Delete any Channels that were deleted from the config. + # Delete any Channels that were deleted from the config, along with their Collections. + orphaned_collection_ids = [] for channel in session.query(Channel): if channel.id not in updated_channel_ids: logger.warning(f'Deleting {channel} because it is not in the config.') + orphaned_collection_ids.append(channel.collection_id) channel.delete_with_videos() + # Delete orphaned Collections (kind='channel') that no longer have a Channel. + # This prevents Collections from appearing in the UI with null channel_id. + from wrolpi.collections import Collection + for collection_id in orphaned_collection_ids: + collection = session.query(Collection).filter_by(id=collection_id).one_or_none() + if collection: + logger.warning(f'Deleting orphaned Collection {collection.name} (id={collection.id})') + session.delete(collection) + # Assign Videos to Channels using bulk SQL from modules.videos import claim_videos_for_channels claim_videos_for_channels(list(updated_channel_ids)) diff --git a/modules/videos/test/test_common.py b/modules/videos/test/test_common.py index 42b55fa8..7108a2c0 100644 --- a/modules/videos/test/test_common.py +++ b/modules/videos/test/test_common.py @@ -8,6 +8,7 @@ import pytest from PIL import Image from modules.videos.models import Channel, Video +from wrolpi.collections import Collection from wrolpi.common import get_absolute_media_path, get_wrolpi_config from wrolpi.downloader import Download, DownloadFrequency from wrolpi.files import lib as files_lib @@ -299,6 +300,48 @@ async def test_import_channel_delete_missing_channels(await_switches, test_sessi assert channel2_dir not in test_channels_config.read_text() +@pytest.mark.asyncio +async def test_import_channel_deletes_orphaned_collections(await_switches, test_session, channel_factory, + test_channels_config): + """When a Channel is deleted from config, its associated Collection should also be deleted. + + This prevents orphaned Collections (kind='channel') from appearing in the UI with null channel_id. + """ + # Create two channels (each creates a Collection with kind='channel') + channel1 = channel_factory(source_id='foo') + channel2 = channel_factory(source_id='bar') + test_session.commit() + + # Verify both Collections exist + assert test_session.query(Collection).filter_by(kind='channel').count() == 2 + + # Capture directory values before deletion + channel1_dir = str(channel1.directory) + channel2_dir = str(channel2.directory) + collection2_id = channel2.collection_id + + # Write Channels to the config file + save_channels_config() + await await_switches() + + # Remove channel2 from the config file + config = get_channels_config() + config_dict = config.dict() + config_dict['channels'] = [i for i in config.channels if i['directory'] != channel2_dir] + config.update(config_dict) + await await_switches() + + # Import config - this should delete channel2 AND its Collection + import_channels_config() + + # Verify only one Channel remains + assert test_session.query(Channel).count() == 1 + + # Verify the orphaned Collection was also deleted (this is the bug we're fixing) + assert test_session.query(Collection).filter_by(kind='channel').count() == 1 + assert test_session.query(Collection).filter_by(id=collection2_id).count() == 0 + + @pytest.mark.asyncio async def test_import_channel_download_comments(await_switches, test_session, channel_factory, test_channels_config):