mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-03 13:19:58 -05:00
Deleting existing orphaned collections when no channel exists in the config.
This commit is contained in:
parent
6a9ffcaadc
commit
150ad6ee78
6 changed files with 136 additions and 3 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue