Collections: changes wait for their config's import; domains with archives survive the import (#784)

* collections: refuse playlist changes until playlists.yaml is imported

While WROLPi starts, the API accepts requests before every config is imported.  A playlist created then could not be
saved, and the import which followed deleted it as "no longer in config" (and rebuilt every playlist's items from
the config, undoing any change).  Playlist changes now wait for the import (503, "still starting").

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* collections: refuse domain changes until domains.yaml is imported

A domain created, changed, tagged or deleted through the API before domains.yaml was imported (e.g. while WROLPi
starts) was undone by the import.  These now wait for the import (503, "still starting").

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* archive: importing domains.yaml keeps domains which have archives

Archives create their domain (e.g. one uploaded, or found by a refresh, while WROLPi starts, before domains.yaml is
imported).  The import deleted every domain missing from the config, and the database deleted that domain's Archives
with it.  A domain with Archives is now kept (and saved to the config with the next change).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* videos: refuse Channel changes until channels.yaml is imported

A Channel created through the API before channels.yaml was imported (e.g. while WROLPi starts) could not be saved,
and the import which followed deleted it, and its Downloads, as "not in the config".  Creating, changing, tagging and
deleting a Channel (through the Channel or Collection API) now wait for the import (503, "still starting").

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
lrnselfreliance 2026-10-07 14:33:51 -06:00 • committed by GitHub
parent b742a4a25f
commit 1ded249fc7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 229 additions and 7 deletions

View file

@ -153,12 +153,19 @@ class DomainsConfig(ConfigFile):
collections = Collection.batch_from_config(session, valid_data_list)
imported_domains = {c.name for c in collections}
# Delete domain collections that are no longer in config
# Delete domain collections that are no longer in config. A domain with Archives is kept: Archives
# create their domain (e.g. one downloaded before this import), and deleting it would delete them.
with_archives = {i for i, in session.query(Archive.collection_id).distinct()}
all_domain_collections = session.query(Collection).filter_by(kind='domain').all()
for collection in all_domain_collections:
if collection.name not in imported_domains:
logger.info(f'Deleting domain collection {repr(collection.name)} (no longer in config)')
session.delete(collection)
if collection.name in imported_domains:
continue
if collection.id in with_archives:
logger.warning(f'Keeping domain collection {repr(collection.name)} which is not in config,'
f' because it has archives')
continue
logger.info(f'Deleting domain collection {repr(collection.name)} (no longer in config)')
session.delete(collection)
logger.info(f'Successfully imported {len(imported_domains)} domain collections from {file_str}')
self.successful_import = True

View file

@ -17,12 +17,19 @@ from wrolpi.tags import Tag
from .. import schema
from ..common import check_for_channel_conflicts
from ..errors import UnknownChannel
from ..lib import save_channels_config
from ..lib import save_channels_config, get_channels_config
from ..models import Channel, Video
logger = logger.getChild(__name__)
def check_channels_config_imported():
"""Raise ConfigNotImported until channels.yaml is imported; the import would undo a change made before it."""
# A test may have no channels config.
if config := get_channels_config():
config.check_imported()
async def get_minimal_channels(session: Session) -> List[dict]:
"""
Get the minimum amount of information necessary about all channels.
@ -112,6 +119,7 @@ def get_channel(session: Session, *, channel_id: int = None, source_id: str = No
async def update_channel(session: Session, *, data: schema.ChannelPutRequest, channel_id: int) -> Channel:
"""Update a Channel's DB record"""
check_channels_config_imported()
channel = Channel.find_by_id(session, channel_id)
# Verify that the URL/Name/directory aren't taken
@ -149,6 +157,7 @@ def create_channel(session: Session, data: schema.ChannelPostRequest, return_dic
"""
Create a new Channel. Check for conflicts with existing Channels.
"""
check_channels_config_imported()
try:
# Verify that the URL/Name/directory aren't taken
check_for_channel_conflicts(
@ -189,6 +198,7 @@ def create_channel(session: Session, data: schema.ChannelPostRequest, return_dic
def delete_channel(session: Session, *, channel_id: int):
check_channels_config_imported()
channel = Channel.find_by_id(session, channel_id)
channel_dict = channel.dict()
@ -292,6 +302,7 @@ async def tag_channel(session: Session, tag_name: str | None, directory: pathlib
"""Add a Tag to a Channel, or remove a Tag from a Channel if no `tag_name` is provided.
Move the Channel to the new directory, if provided."""
check_channels_config_imported()
if directory and flags.file_worker_busy.is_set():
raise FileWorkerConflict('Refusing to move channel while FileWorker is busy')

View file

@ -6,6 +6,9 @@ Tests cover:
2. Upgrade - config file exists with data
3. Edge cases - empty config, missing config with DB data
"""
import json
from http import HTTPStatus
import pytest
import yaml
@ -322,3 +325,44 @@ class TestChannelsConfigEdgeCases:
# Verify file_format restored
test_session.refresh(channel)
assert channel.collection.file_format == '%(upload_date)s_%(title)s.%(ext)s'
@pytest.mark.asyncio
async def test_channel_changes_wait_for_config_import(test_session, test_directory, async_client, channel_factory,
test_channels_config):
"""Until channels.yaml is imported (e.g. while WROLPi starts), Channels cannot change; the import would undo the
change (a new Channel would be deleted, with its Downloads)."""
channel = channel_factory(name='Kept')
test_session.commit()
config = get_channels_config()
config.dump_config()
assert config.get_file().is_file()
config.successful_import = False
cid, collection_id = channel.id, channel.collection_id
requests = [
('post', '/api/videos/channels', dict(name='New', directory='videos/new', url='https://example.com/new')),
('put', f'/api/videos/channels/{cid}', dict(name='Changed', directory=str(channel.directory))),
('post', f'/api/videos/channels/{cid}/tag', dict(tag_name='Tagged')),
('delete', f'/api/videos/channels/{cid}', None),
('put', f'/api/collections/{collection_id}', dict(description='changed')),
('post', f'/api/collections/{collection_id}/tag', dict(tag_name='Tagged')),
('delete', f'/api/collections/{collection_id}', None),
]
for method, url, body in requests:
kwargs = dict(content=json.dumps(body)) if body else {}
request, response = await getattr(async_client, method)(url, **kwargs)
assert response.status_code == HTTPStatus.SERVICE_UNAVAILABLE, (method, url, response.json)
assert response.json['code'] == 'CONFIG_NOT_IMPORTED'
test_session.expire_all()
channel = test_session.query(Channel).one()
assert channel.name == 'Kept'
assert channel.tag_name is None
assert channel.collection.description is None
# Once imported, Channels can change again.
config.import_config()
request, response = await async_client.put(f'/api/collections/{collection_id}',
content=json.dumps(dict(description='x')))
assert response.status_code == HTTPStatus.OK, response.json