Renaming a Tag commits its Download renames with the request (#879)

* Renaming a Tag commits its Download renames with the request

update_tag renamed Downloads in a background task using the request's
session, after RequestDB.write() had committed.  Its flush was never
committed, so the rename was lost, and the open write transaction held
SQLite's write lock: every later write failed with "database is locked"
until the API restarted.  Only the Channel moves stay in the background.

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

* Save the Downloads config after a Tag rename commits; scan Downloads only on a rename

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-25 18:30:17 -06:00 • committed by Roland
parent fffb2ed981
commit d16bac29e8
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
2 changed files with 47 additions and 21 deletions

View file

@ -182,22 +182,24 @@ class Tag(ModelHelper, Base):
if Session.object_session(self):
self.flush()
from modules.videos.errors import ChannelDirectoryConflict
async def _():
# Rename old name referenced in any Downloads.
# Rename old name referenced in any Downloads, in the caller's transaction. (A background task's writes
# would not be committed with it.) The caller saves the Downloads config after it commits.
if old_name and old_name != name:
from wrolpi.downloader import Download
to_flush = list()
for download in session.query(Download):
downloads = session.query(Download).options(
load_only(Download.id, Download.tag_names, Download.settings))
for download in downloads:
tag_names, settings = with_tag_name_renamed(download.tag_names, download.settings, old_name, name)
if tag_names != download.tag_names or settings != download.settings:
download.tag_names, download.settings = tag_names, settings
to_flush.append(download)
if to_flush:
session.flush(to_flush)
save_downloads_config.activate_switch()
from modules.videos.errors import ChannelDirectoryConflict
async def _():
# Only move channels if tag is being renamed (not created)
if old_name:
# Get channels through Collections
@ -1137,8 +1139,10 @@ def get_overlapping_tags(session: Session, tag_names: Union[str, List[str]], lim
async def upsert_tag(session: Session, name: str, color: str, tag_id: int = None) -> Tag:
old_name = None
if tag_id:
tag = Tag.find_by_id(session, tag_id)
old_name = tag.name
await tag.update_tag(session, name, color)
else:
tag = Tag()
@ -1156,6 +1160,9 @@ async def upsert_tag(session: Session, name: str, color: str, tag_id: int = None
save_tags_config.activate_switch()
sync_tags_directory.activate_switch()
if old_name and old_name != tag.name:
# Downloads were renamed; save them only once they are committed.
save_downloads_config.activate_switch()
return tag

View file

@ -596,27 +596,46 @@ async def test_tag_rename_with_channel(test_session, test_directory, video_facto
@pytest.mark.asyncio
async def test_tag_rename_downloads(test_session, tag_factory, await_background_tasks):
async def test_tag_rename_downloads(async_client, test_session, tag_factory, await_background_tasks, await_switches,
test_download_manager_config):
"""Renaming a Tag renames it everywhere a Download applies it, including the settings passed on to the
Downloads (and Channels) it creates."""
Downloads (and Channels) it creates. The rename is committed with the request, saved to the config, and leaves
the database writable."""
from wrolpi.db import get_db_session
from wrolpi.downloader import Download
tag, other = await tag_factory('one'), await tag_factory('two')
download = Download(url='https://example.com/playlist', downloader='video_channel', tag_names=['one', 'two'],
settings=dict(tag_names=['two', 'one'], channel_tag_name=['one'], video_resolutions=['720p']))
untouched = Download(url='https://example.com/other', downloader='video_channel', tag_names=['two'],
settings=dict(tag_names=['two'], channel_tag_name=['two']))
test_session.add_all([download, untouched])
test_session.add_all([
Download(url='https://example.com/playlist', downloader='video_channel', tag_names=['one', 'two'],
settings=dict(tag_names=['two', 'one'], channel_tag_name=['one'], video_resolutions=['720p'])),
Download(url='https://example.com/other', downloader='video_channel', tag_names=['two'],
settings=dict(tag_names=['two'], channel_tag_name=['two'])),
])
test_session.commit()
await tag.update_tag(test_session, 'New Name', None)
content = dict(name='New Name', color=tag.color)
request, response = await async_client.post(f'/api/tag/{tag.id}', content=json.dumps(content))
assert response.status_code == HTTPStatus.OK
await await_background_tasks()
test_session.commit()
assert download.tag_names == ['New Name', 'two']
assert download.settings == dict(tag_names=['two', 'New Name'], channel_tag_name=['New Name'],
video_resolutions=['720p'])
assert untouched.tag_names == ['two']
assert untouched.settings == dict(tag_names=['two'], channel_tag_name=['two'])
# Read what was committed, not what a session holds.
with get_db_session() as session:
downloads = {i.url: (i.tag_names, i.settings) for i in session.query(Download)}
assert downloads == {
'https://example.com/playlist': (['New Name', 'two'], dict(tag_names=['two', 'New Name'],
channel_tag_name=['New Name'],
video_resolutions=['720p'])),
'https://example.com/other': (['two'], dict(tag_names=['two'], channel_tag_name=['two'])),
}
await await_switches()
config = yaml.load(test_download_manager_config.read_text(), Loader=yaml.Loader)
playlist, = [i for i in config['downloads'] if i['url'] == 'https://example.com/playlist']
assert playlist['tag_names'] == ['New Name', 'two']
assert playlist['settings']['channel_tag_name'] == ['New Name']
# Nothing holds the write lock.
request, response = await async_client.post('/api/tag', content=json.dumps(dict(name='three', color='#000000')))
assert response.status_code == HTTPStatus.CREATED
@pytest.mark.asyncio