mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-19 22:46:20 -05:00
Deleting orphaned collections when no channel is in the channels config.
This commit is contained in:
parent
0efe6db5a8
commit
b408d11ea1
2 changed files with 55 additions and 1 deletions
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue