mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-10-26 02:48:38 -04:00
Some checks are pending
Backend Tests / api-tests (core) (push) Waiting to run
Backend Tests / api-tests (files) (push) Waiting to run
Backend Tests / api-tests (media) (push) Waiting to run
Backend Tests / api-tests (rest) (push) Waiting to run
Backend Tests / api-contract-tests (push) Waiting to run
Backend Tests / api-migrations-and-start (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Controller Tests / controller-tests (push) Waiting to run
Cypress Tests / cypress-component-tests (push) Waiting to run
Cypress Tests / cypress-e2e-tests (push) Waiting to run
Jest Tests / jest-tests (push) Waiting to run
* Benchmark every database config import (#865) An opt-in test (WROLPI_CONFIG_BENCH=1) that builds a large library in bulk, writes each config with its own dump_config, then times every import in startup order: a steady pass (the database already matches) and a rebuild (config-owned rows deleted, everything restored from the configs). It prints seconds, SQL statements and writes per import, and asserts the rebuild restored every row so no timed import silently skipped its work. WROLPI_CONFIG_BENCH_SCALE multiplies the row counts. The baseline for the config import rework. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> * Database configs carry a version the database keeps too Step 1 of the config import rework. A database config (tags, downloads, channels, domains, playlists, repos, recently viewed) and the database now each carry an integer version, so an import can tell which side is newer without a clock (a Raspberry Pi's resets at boot): - config_version (new table) holds the database's version of each config. - An after_flush hook adds one to it for every config whose rows a flush changed, in the same transaction. Each config registers what it owns (register_ownership): models, optionally only some columns, optionally only some rows (a Collection's kind; a Download's Collection's kind). Raw SQL writes to owned tables count explicitly. - dump_config writes the database's version, read with the rows it saves; save() no longer adds one for these configs. - An import compares versions first: file == database or newer imports with deletes; file older (the database changed and was not saved before a restart) saves the database instead of undoing that change; no database row (a config this code never imported) imports without deletes. The import records the file's version last. - The Import button and backup imports apply the file whatever its version (import_forced). Importers only gate their existing deletes on the decision here; they are otherwise unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Config versions: keep unsaved rows after a no-delete import, count every owned write Review fixes: - An import without deletes (no database version yet) no longer records the file's version, which let the next startup delete the rows it had kept. The version stays unknown, and the database is saved into the file instead; a successful save records the version. A save after a partial import is refused, so the version stays unknown until an import applies the whole file. - An import removes the database's version while it runs, so its own writes do not count: one that fails partway no longer looks newer than the file and gets saved over it on the next startup. - Playlists imported without deletes keep their database items in order; the file's other items are added after their neighbor from the file. - A Download leaving a Channel or Repo counts for that Collection's config (collection_id loads its previous value). - Bulk writes count: untagging, retrying Downloads, deleting a duplicate's Download, removing many items from a playlist. Deleting Downloads by id counts channels.yaml/repos.yaml only for their Downloads. - Only a playlist's items count for playlists.yaml (author and subject Collections have items too). Collection kinds are looked up once per flush. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Config versions count only the columns each config saves A Repo's fetch (head_sha, last_fetch), a Channel's refresh (video_count, info_json), a Collection's counts, and a Download attempt (attempts, error) counted as config changes, so the next import of that config saved instead of importing: a hand edit to repos.yaml was overwritten after any fetch. Each model now registers only the columns its config saves. domains.yaml saves its domains' Downloads, which now count for it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
95 lines
4.3 KiB
Python
95 lines
4.3 KiB
Python
import pytest
|
|
|
|
from modules.repos import lib
|
|
from modules.repos.models import Repository
|
|
from wrolpi.collections.models import Collection
|
|
from wrolpi.conftest import config_versions_lost
|
|
from wrolpi.downloader import Download, DownloadFrequency
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_dump_and_import(test_session, test_directory, async_client, test_wrolpi_config, await_switches):
|
|
"""repos.yaml is the source of truth; a lost database is rebuilt from it."""
|
|
lib.create_repository(test_session, 'https://github.com/kiwix/kiwix-tools', tag_name='software',
|
|
description='Tools', frequency=DownloadFrequency.daily)
|
|
lib.create_repository(test_session, 'https://github.com/a/b', mode='snapshot', branch='dev')
|
|
await await_switches()
|
|
|
|
config = lib.get_repos_config()
|
|
assert config.get_file().is_file()
|
|
# Each Repo's random clone token is kept, so a restored Repo still owns its clone.
|
|
dumped = [dict(i) for i in config.repos]
|
|
assert all(len(i.pop('clone_token')) == 32 for i in dumped)
|
|
assert dumped == [
|
|
dict(name='b', url='https://github.com/a/b', directory='repos/b', tag_name=None, description=None,
|
|
mode='snapshot', branch='dev', submodules=False, frequency=DownloadFrequency.weekly),
|
|
dict(name='kiwix-tools', url='https://github.com/kiwix/kiwix-tools', directory='repos/software/kiwix-tools',
|
|
tag_name='software', description='Tools', mode='full', branch=None, submodules=False,
|
|
frequency=DownloadFrequency.daily),
|
|
]
|
|
|
|
# Lose the database.
|
|
for repo in test_session.query(Repository).all():
|
|
lib._delete_repository(test_session, repo)
|
|
test_session.commit()
|
|
config_versions_lost(test_session)
|
|
assert test_session.query(Collection).count() == 0
|
|
|
|
config.import_config()
|
|
# Imported without deletes; the database is saved, which records its version.
|
|
await await_switches()
|
|
test_session.expire_all()
|
|
repos = {i.name: i for i in test_session.query(Repository)}
|
|
assert repos['kiwix-tools'].directory == test_directory / 'repos/software/kiwix-tools'
|
|
assert repos['kiwix-tools'].tag_name == 'software'
|
|
assert repos['kiwix-tools'].collection.description == 'Tools'
|
|
assert repos['kiwix-tools'].download.frequency == DownloadFrequency.daily
|
|
assert (repos['b'].mode, repos['b'].branch) == ('snapshot', 'dev')
|
|
assert {i.downloader for i in test_session.query(Download)} == {'git'}
|
|
|
|
# A repo removed from the config is deleted.
|
|
data = config.read_config_file()
|
|
data['repos'] = data['repos'][1:]
|
|
data['version'] += 1
|
|
config.write_config_data(data, config.get_file())
|
|
config.import_config()
|
|
test_session.expire_all()
|
|
assert [i.name for i in test_session.query(Repository)] == ['kiwix-tools']
|
|
assert test_session.query(Download).count() == 1
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_import_links_existing_download(test_session, async_client, test_wrolpi_config):
|
|
"""A Repo imported after download_manager.yaml re-uses its Download."""
|
|
lib.create_repository(test_session, 'https://github.com/a/b')
|
|
test_session.query(Repository).delete()
|
|
test_session.query(Collection).delete()
|
|
test_session.commit()
|
|
download = test_session.query(Download).one()
|
|
assert download.collection_id is None
|
|
|
|
config = lib.get_repos_config()
|
|
config.write_config_data(dict(version=1, repos=[dict(name='b', url='https://github.com/a/b', frequency=3600)]),
|
|
config.get_file())
|
|
config.import_config()
|
|
test_session.expire_all()
|
|
|
|
repo = test_session.query(Repository).one()
|
|
assert test_session.query(Download).one().id == download.id
|
|
assert download.collection_id == repo.collection_id
|
|
assert download.frequency == 3600
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_delete_last_repo(test_session, async_client, test_wrolpi_config, await_switches):
|
|
"""Deleting the last Repo empties the config, so it does not return on the next import."""
|
|
repo = lib.create_repository(test_session, 'https://github.com/a/b')
|
|
await await_switches()
|
|
config = lib.get_repos_config()
|
|
assert len(config.read_config_file()['repos']) == 1
|
|
|
|
request, response = await async_client.delete(f'/api/repos/{repo.id}')
|
|
assert config.read_config_file()['repos'] == []
|
|
|
|
config.import_config()
|
|
assert test_session.query(Repository).count() == 0
|