Save channels.yaml with the database's version (#880)

save_channels_config wrote the Channels with `update()`, which keeps the version the config already had in
memory.  After a Channel changed, the database's version was ahead of the saved file's, so the next import
treated the file as older and saved the database over it: an edit made to channels.yaml in the meantime was
lost.  It now saves with `dump_config`, which writes the database's version, as every other database config
does.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
lrnselfreliance 2026-10-25 19:39:45 -06:00 • committed by GitHub
parent d16bac29e8
commit ae97727b98
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 29 additions and 14 deletions

View file

@ -1022,30 +1022,22 @@ def get_yt_dlp_sleep_opts() -> dict:
return {}
def get_channels_config_from_db(session: Session) -> dict:
"""Create a dictionary that contains all the Channels from the DB."""
from wrolpi.collections import Collection
channels = session.query(Channel).join(Collection).order_by(Collection.directory).all()
channels = sorted((i.config_view() for i in channels), key=lambda i: i['directory'] if i['directory'] else '')
return dict(channels=channels)
@register_switch_handler('save_channels_config')
def save_channels_config():
"""Get the Channel information from the DB, save it to the config."""
"""Save the Channels in the DB to the config, with the DB's version (see `ChannelsConfig.dump_config`)."""
with get_db_session() as session:
config = get_channels_config_from_db(session)
channels_config = get_channels_config()
has_channels = session.query(Channel.id).first() is not None
channels_config = get_channels_config()
if PYTEST and not channels_config:
logger.warning('Refusing to save channels config because test did not initialize a test config!')
return
if not config.get('channels') and channels_config.channels:
if not has_channels and channels_config.channels:
logger.warning('Refusing to save empty channels config because channels exist in the config file!')
return
channels_config.update(config)
channels_config.dump_config()
logger.info('save_channels_config completed')

View file

@ -284,3 +284,23 @@ async def test_import_does_not_save_during_its_write(test_session, saved_channel
assert saves == []
assert 'https://example.com/one' not in (get_download_manager_config().skip_urls or [])
@pytest.mark.asyncio
async def test_saved_change_is_in_step(test_session, saved_channels, await_switches):
"""A Channel changed in WROLPi is saved with the database's version, so an edit of the saved file is imported
(not saved over as an older file)."""
print("before", get_config_version(test_session, "channels.yaml"), get_channels_config().version)
two = channels(test_session)['two']
two.generate_posters = not two.generate_posters
test_session.commit()
print("after commit", get_config_version(test_session, "channels.yaml"), get_channels_config().version)
lib.save_channels_config()
await await_switches()
file_version = yaml.safe_load(saved_channels.read_text())['version']
assert file_version == get_config_version(test_session, 'channels.yaml')
edit_channel(saved_channels, 'two', url='https://example.com/edited')
import_channels_config()
assert channels(test_session)['two'].url == 'https://example.com/edited'

View file

@ -9,7 +9,8 @@ from PIL import Image
from modules.videos.models import Channel, Video
from wrolpi.collections import Collection
from wrolpi.conftest import config_was_imported, probe_write_lock_is_held, production_like_sessions
from wrolpi.conftest import config_versions_lost, config_was_imported, probe_write_lock_is_held, \
production_like_sessions
from wrolpi.common import get_absolute_media_path, get_wrolpi_config, get_relative_to_media_directory
from wrolpi.downloader import Download, DownloadFrequency
from wrolpi.vars import PROJECT_DIR
@ -292,10 +293,12 @@ async def test_import_channel_delete_missing_channels(await_switches, test_sessi
test_session.delete(channel1)
test_session.delete(channel2)
test_session.commit()
config_versions_lost(test_session)
await await_switches()
# Importing the config creates two Channels.
import_channels_config()
await await_switches()
assert len(test_session.query(Channel).all()) == 2
assert channel1_dir in test_channels_config.read_text()
assert channel2_dir in test_channels_config.read_text()