Deleting existing orphaned collections when no channel exists in the config.

This commit is contained in:
Roland 2025-12-26 10:42:02 -07:00
parent 6a9ffcaadc
commit 150ad6ee78
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
6 changed files with 136 additions and 3 deletions

View file

@ -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,

View file

@ -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))

View file

@ -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.

View file

@ -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):

View file

@ -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()

View file

@ -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/<id>/tag endpoint for tagging and un-tagging."""