Fixing bug where configs could be overwritten with empty configs on first refresh. Fixing bug where configs were displayed as JSON in admin settings.

This commit is contained in:
Roland 2026-03-22 12:56:37 -06:00
parent 83ad253d6f
commit 755b133fa4
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
7 changed files with 91 additions and 5 deletions

View file

@ -425,6 +425,11 @@ export async function getConfig(fileName) {
return (await response.json())['config'];
}
export async function getConfigText(fileName) {
const response = await apiGet(`${API_URI}/config?file_name=${fileName}`);
return (await response.json())['config_text'];
}
export async function getConfigBackups(fileName) {
const response = await apiGet(`${API_URI}/config/backups?file_name=${fileName}`);
if (response.ok) {

View file

@ -3,7 +3,7 @@ import {Button, Header, Icon, Loader, Modal, Popup, Table} from "../Theme";
import React, {useContext, useEffect, useState} from "react";
import {TableBody, TableCell, TableHeader, TableHeaderCell, TableRow} from "semantic-ui-react";
import {TableRowPlaceholder} from "../Placeholder";
import {getConfig, getConfigBackups, postConfigBackupImport, postConfigBackupPreview} from "../../api";
import {getConfigText, getConfigBackups, postConfigBackupImport, postConfigBackupPreview} from "../../api";
import {ThemeContext} from "../../contexts/contexts";
function WarningIcon({ok}) {
@ -300,7 +300,7 @@ function ConfigContentModal({open, onClose, fileName}) {
let cancelled = false;
setLoading(true);
setContent(null);
getConfig(fileName).then(result => {
getConfigText(fileName).then(result => {
if (cancelled) return;
setContent(result);
setLoading(false);
@ -318,7 +318,7 @@ function ConfigContentModal({open, onClose, fileName}) {
{loading
? <Loader active inline='centered'/>
: content
? <pre style={{whiteSpace: 'pre-wrap', wordBreak: 'break-word'}}>{JSON.stringify(content, null, 2)}</pre>
? <pre style={{whiteSpace: 'pre-wrap', wordBreak: 'break-word'}}>{content}</pre>
: <p>Failed to load config contents.</p>
}
</Modal.Content>

View file

@ -175,7 +175,11 @@ class DomainsConfig(ConfigFile):
# Use to_config to export each collection
collections_data = [collection.to_config() for collection in collections]
self._config['collections'] = collections_data
if not collections_data and self._config.get('collections'):
logger.warning('Refusing to save empty domains config because domains exist in the config file!')
return
self._config['collections'] = collections_data
logger.info(f'Dumping {len(collections_data)} domain collections to config')
self.save(file, send_events, overwrite)

View file

@ -1,6 +1,7 @@
"""Tests for domain collection tagging functionality."""
import pytest
import yaml
from wrolpi.collections import Collection
@ -316,3 +317,43 @@ async def test_untag_domain_without_directory_saves_config(test_session, test_di
domain_entry = next((c for c in data.get('collections', []) if c['name'] == 'example.com'), None)
assert domain_entry is not None, "Domain collection not found in config"
assert domain_entry.get('tag_name') is None, "Tag should be removed from config"
@pytest.mark.asyncio
async def test_dump_domains_config_does_not_wipe_config(test_session, test_directory, async_client):
"""Dumping domains config when DB is empty must not overwrite an existing config file that has domains.
Regression test: a fresh DB triggering dump_config would write collections: []
to the config file, permanently wiping the domain definitions.
"""
from modules.archive.lib import DomainsConfig
from wrolpi.common import get_media_directory
# Create domain collection and dump config.
domain_dir = get_media_directory() / 'archives' / 'example.com'
domain_dir.mkdir(parents=True, exist_ok=True)
collection = Collection(name='example.com', kind='domain', directory=domain_dir)
test_session.add(collection)
test_session.commit()
config_file = test_directory / 'config' / 'domains.yaml'
config_file.parent.mkdir(parents=True, exist_ok=True)
domains_config = DomainsConfig()
domains_config.dump_config(config_file, overwrite=True)
data = yaml.safe_load(config_file.read_text())
assert len(data['collections']) == 1, 'Config should have a domain'
# Simulate a fresh DB by deleting all domain collections.
test_session.query(Collection).filter_by(kind='domain').delete()
test_session.commit()
# Re-initialize the config from the file so in-memory has the collections.
domains_config.initialize()
file_data = yaml.safe_load(config_file.read_text())
assert len(file_data['collections']) == 1, 'File should still have domains before dump'
# Dumping with an empty DB should NOT wipe the config file.
domains_config.dump_config(config_file)
data = yaml.safe_load(config_file.read_text())
assert len(data['collections']) == 1, 'Config was wiped by dumping with empty DB!'

View file

@ -799,6 +799,10 @@ def save_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:
logger.warning('Refusing to save empty channels config because channels exist in the config file!')
return
channels_config.update(config)
logger.info('save_channels_config completed')

View file

@ -450,6 +450,36 @@ async def test_import_channel_download_comments(await_switches, test_session, ch
assert channel.download_missing_data is False
@pytest.mark.asyncio
async def test_save_channels_config_does_not_wipe_config(await_switches, test_session, channel_factory,
test_channels_config):
"""Saving channels config when DB is empty must not overwrite an existing config file that has channels.
Regression test: a fresh DB (e.g. after repair) triggering save_channels_config would write channels: []
to the config file, permanently wiping the channel definitions.
"""
# Create a channel and save it to the config file.
channel = channel_factory()
test_session.commit()
save_channels_config()
await await_switches()
assert test_channels_config.is_file()
config_text = test_channels_config.read_text()
assert 'channels: []' not in config_text, 'Config should have a channel'
# Simulate a fresh DB by deleting all channels.
test_session.query(Channel).delete()
test_session.query(Collection).filter_by(kind='channel').delete()
test_session.commit()
assert test_session.query(Channel).count() == 0
# Saving with an empty DB should NOT wipe the config file.
save_channels_config()
await await_switches()
config_text = test_channels_config.read_text()
assert 'channels: []' not in config_text, 'Config was wiped by saving with empty DB!'
@pytest.mark.asyncio
async def test_ffprobe_json(async_client, video_file, corrupted_video_file):
content = await common.ffprobe_json(video_file)

View file

@ -17,7 +17,9 @@ config_bp = Blueprint('config', '/api/config')
def get_config(request: Request):
if file_name := request.args.get('file_name'):
config = get_config_by_file_name(file_name)
return json_response(dict(config=config))
config_file = config.get_file()
config_text = config_file.read_text() if config_file.is_file() else None
return json_response(dict(config=config, config_text=config_text))
else:
configs = get_all_configs()
configs = {k: v.config_status() for k, v in configs.items()}