diff --git a/modules/videos/lib.py b/modules/videos/lib.py index bcc36bd6..07a995fc 100644 --- a/modules/videos/lib.py +++ b/modules/videos/lib.py @@ -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') diff --git a/modules/videos/test/test_channels_config_import.py b/modules/videos/test/test_channels_config_import.py index 735a697a..84e46350 100644 --- a/modules/videos/test/test_channels_config_import.py +++ b/modules/videos/test/test_channels_config_import.py @@ -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' diff --git a/modules/videos/test/test_common.py b/modules/videos/test/test_common.py index 67537497..a33a95f9 100644 --- a/modules/videos/test/test_common.py +++ b/modules/videos/test/test_common.py @@ -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()