From 150ad6ee78c85cea3bd9f6af3eaba169a5bf9887 Mon Sep 17 00:00:00 2001 From: Roland Date: Fri, 26 Dec 2025 10:42:02 -0700 Subject: [PATCH] Deleting existing orphaned collections when no channel exists in the config. --- modules/videos/channel/test/test_models.py | 9 ++++- modules/videos/lib.py | 17 ++++++++++ modules/videos/models.py | 11 +++++-- modules/videos/test/test_common.py | 36 ++++++++++++++++++++ wrolpi/collections/lib.py | 28 ++++++++++++++++ wrolpi/collections/test/test_api.py | 38 ++++++++++++++++++++++ 6 files changed, 136 insertions(+), 3 deletions(-) diff --git a/modules/videos/channel/test/test_models.py b/modules/videos/channel/test/test_models.py index 3b8cf7bc..7f2dbdfa 100644 --- a/modules/videos/channel/test/test_models.py +++ b/modules/videos/channel/test/test_models.py @@ -21,10 +21,13 @@ def test_delete_channel_no_url(test_session, channel_factory): @pytest.mark.asyncio async def test_delete_channel_with_videos(test_session, async_client, channel_factory, video_factory): - """Videos are disowned when their Channel is deleted.""" + """Videos are disowned when their Channel is deleted. The Collection should also be deleted.""" + from wrolpi.collections import Collection + channel = channel_factory() video = video_factory(channel_id=channel.id) video_path, video_id = video.video_path, video.id + collection_id = channel.collection_id test_session.commit() # Delete the Channel. @@ -37,6 +40,10 @@ async def test_delete_channel_with_videos(test_session, async_client, channel_fa assert video.video_path.is_file(), 'Video should not be deleted' assert not video.channel_id and not video.channel, 'Video should not have a Channel' + # Collection should also be deleted (this is the bug we're fixing) + assert test_session.query(Collection).filter_by(id=collection_id).count() == 0, \ + 'Collection should be deleted when Channel is deleted' + @pytest.mark.asyncio async def test_nested_channel_directories(test_session, async_client, test_directory, channel_factory, diff --git a/modules/videos/lib.py b/modules/videos/lib.py index be5b3bcf..4129b12e 100644 --- a/modules/videos/lib.py +++ b/modules/videos/lib.py @@ -379,6 +379,23 @@ class ChannelsConfig(ConfigFile): logger.warning(f'Deleting orphaned Collection {collection.name} (id={collection.id})') session.delete(collection) + # Also clean up any pre-existing orphaned Collections (kind='channel') + # that have no associated Channel. This handles orphans created before + # this cleanup logic was added. + with get_db_session(commit=True) as session: + from wrolpi.collections import Collection + # Find all Collections with kind='channel' that no Channel references + orphaned_collections = session.query(Collection).outerjoin( + Channel, Channel.collection_id == Collection.id + ).filter( + Collection.kind == 'channel', + Channel.id.is_(None) + ).all() + + for collection in orphaned_collections: + logger.warning(f'Deleting pre-existing 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/models.py b/modules/videos/models.py index 95b36af7..1f3283f7 100644 --- a/modules/videos/models.py +++ b/modules/videos/models.py @@ -625,7 +625,7 @@ class Channel(ModelHelper, Base): raise FileNotFoundError(f'Cannot create Channel info json because directory is not defined: {self}') def delete_with_videos(self): - """Delete all Video records (but not video files) related to this Channel. Then delete the Channel.""" + """Delete all Video records (but not video files) related to this Channel. Then delete the Channel and its Collection.""" session = Session.object_session(self) # Disown the videos. @@ -637,9 +637,16 @@ class Channel(ModelHelper, Base): download: Download download.delete() - # Must commit before final deletion. + # Get the Collection before deleting the Channel + collection = self.collection + + # Delete the Channel session.delete(self) + # Delete the Collection after Channel to avoid FK constraint issues + if collection: + session.delete(collection) + def update(self, data: dict): """ Update the attributes of this Channel. Will also update the Channel's Download, if it has one. diff --git a/modules/videos/test/test_common.py b/modules/videos/test/test_common.py index 7108a2c0..d3d5ee6e 100644 --- a/modules/videos/test/test_common.py +++ b/modules/videos/test/test_common.py @@ -342,6 +342,42 @@ async def test_import_channel_deletes_orphaned_collections(await_switches, test_ assert test_session.query(Collection).filter_by(id=collection2_id).count() == 0 +@pytest.mark.asyncio +async def test_import_channel_cleans_up_preexisting_orphaned_collections(await_switches, test_session, channel_factory, + test_channels_config): + """Pre-existing orphaned Collections (kind='channel') with no Channel should be deleted during import. + + This handles the case where a Collection was orphaned before the cleanup fix was deployed. + """ + # Create a channel (which creates a Collection) + channel1 = channel_factory(source_id='foo') + test_session.commit() + + # Create an orphaned Collection directly (simulating pre-existing orphan) + orphan_collection = Collection( + name='Orphaned Channel', + kind='channel', + directory=None, + ) + test_session.add(orphan_collection) + test_session.commit() + orphan_id = orphan_collection.id + + # Verify we have 2 Collections with kind='channel' + assert test_session.query(Collection).filter_by(kind='channel').count() == 2 + + # Write the valid channel to config + save_channels_config() + await await_switches() + + # Import config - should clean up the pre-existing orphan + import_channels_config() + + # Verify only one Collection remains (the one with a Channel) + assert test_session.query(Collection).filter_by(kind='channel').count() == 1 + assert test_session.query(Collection).filter_by(id=orphan_id).count() == 0 + + @pytest.mark.asyncio async def test_import_channel_download_comments(await_switches, test_session, channel_factory, test_channels_config): diff --git a/wrolpi/collections/lib.py b/wrolpi/collections/lib.py index 27f2824b..c80379b0 100644 --- a/wrolpi/collections/lib.py +++ b/wrolpi/collections/lib.py @@ -563,6 +563,12 @@ def delete_collection( - Deletes the Collection record - Triggers domain config save + For channel collections: + - Orphans child Videos (sets channel_id to NULL) + - Deletes the Channel record + - Deletes the Collection record + - Triggers channel config save + Args: session: Database session collection_id: The collection ID to delete @@ -597,6 +603,28 @@ def delete_collection( # Trigger domain config save save_domains_config.activate_switch() + # Orphan child Videos and delete Channel if this is a channel collection + elif collection.kind == 'channel': + # Local imports to avoid circular import: collections -> videos -> collections + from modules.videos.models import Channel, Video + from modules.videos.lib import save_channels_config + + # Find the Channel that references this Collection + channel = session.query(Channel).filter_by(collection_id=collection_id).one_or_none() + if channel: + # Orphan all Videos (set channel_id = None) + videos = session.query(Video).filter_by(channel_id=channel.id).all() + for video in videos: + video.channel_id = None + session.flush() + + # Delete the Channel (before Collection to satisfy FK) + session.delete(channel) + session.flush() + + # Trigger channel config save + save_channels_config.activate_switch() + # Delete the collection session.delete(collection) session.flush() diff --git a/wrolpi/collections/test/test_api.py b/wrolpi/collections/test/test_api.py index e2164a77..23f824e7 100644 --- a/wrolpi/collections/test/test_api.py +++ b/wrolpi/collections/test/test_api.py @@ -413,6 +413,44 @@ class TestCollectionDeletion: # Verify collection is deleted assert test_session.query(Collection).filter_by(id=collection_id).count() == 0 + @pytest.mark.asyncio + async def test_delete_channel_collection_with_videos( + self, async_client, test_session, test_directory, channel_factory, video_factory + ): + """Test that deleting a channel collection with videos orphans the videos.""" + from modules.videos.models import Channel, Video + + # Create a channel with videos + channel = channel_factory(name='test', directory=test_directory / 'videos' / 'test') + video = video_factory(channel_id=channel.id) + test_session.commit() + + collection_id = channel.collection.id + channel_id = channel.id + video_id = video.id + video_path = video.video_path + + # Delete the collection + request, response = await async_client.delete(f'/api/collections/{collection_id}') + + # Check response + assert response.status_code == HTTPStatus.NO_CONTENT + + # Expire all cached objects to see changes made by the API's separate session + test_session.expire_all() + + # Verify collection is deleted + assert test_session.query(Collection).filter_by(id=collection_id).count() == 0 + + # Channel should also be deleted (CASCADE from Collection) + assert test_session.query(Channel).filter_by(id=channel_id).count() == 0 + + # Video should still exist but be orphaned (channel_id = NULL) + video = test_session.query(Video).filter_by(id=video_id).one() + assert video.channel_id is None, 'Video should be orphaned when Channel is deleted' + assert video.video_path == video_path, 'Video path should not change' + assert video.video_path.is_file(), 'Video file should still exist' + class TestCollectionTagging: """Test the POST /api/collections//tag endpoint for tagging and un-tagging."""