diff --git a/src/opensak/db/repair.py b/src/opensak/db/repair.py new file mode 100644 index 00000000..ac8c042b --- /dev/null +++ b/src/opensak/db/repair.py @@ -0,0 +1,336 @@ +""" +src/opensak/db/repair.py — check the data of a database and fix what can be +fixed safely, like GSAK's Repair/Defrag (#1051, phase 1). + +#1048 checks the *file* (quick_check, integrity_check, foreign_key_check); +this module checks the *data*. ``scan()`` only reads and reports every check +with its number of problems and a few example GC codes. ``repair()`` fixes +the checks the user picked, in one transaction, and leaves everything else +as it is. Some checks are report only: there is no safe automatic fix, so +the user has to look at the caches. + +Checks in this phase: + +* rows that point to a missing parent row (``foreign_key_check``): logs, + waypoints, attributes, trackables and user notes of a cache that is gone. + Fix: delete them. Foreign keys are enforced and cache_delete.py removes + the child rows first, so these come from older databases, migrations that + run with foreign keys off, or a crash. +* a custom waypoint's link to its parent cache (``caches.parent_gc_code``) + that points to a cache that is not in the database. Fix: clear the link. +* a waypoint's ``parent_gc_code`` that isn't the code of its own cache. + Fix: set it, as the importer and macros do. +* the corrected-coordinates flag without corrected coordinates. Filters + count such a cache as corrected, the map and distances do not. Fix: clear + the flag. Coordinates "corrected" to the listed ones are fine: they are a + real user choice (the final is at the listed coordinates). +* rows of the companion database (#1034) whose cache is gone. Fix: + ``companion.cleanup()``. +* report only: coordinates out of range, difficulty or terrain outside + 1–5 or not in 0.5 steps, an empty GC code or name. + +No duplicate check: the unique constraints on attributes, ``logs.log_id`` +and waypoints exist in every database (models since v0.2.0, migrations 2 and +21), and the importers replace a cache's waypoints and match logs by id, so +the NULL gaps of those constraints are not filled twice either. + +Fixing a correction flag would fire the last_user_update trigger (#1140); +a repair is not a user edit, so the dates are put back. + +The functions open their own sqlite3 connection, like maintenance.py. The +scan also works while the database is open; for repair() the caller +releases the database's connections first. +""" + +from __future__ import annotations + +import logging +import sqlite3 +from dataclasses import dataclass, field +from pathlib import Path +from typing import Callable, Optional + +logger = logging.getLogger(__name__) + +# GC codes listed per check. +MAX_EXAMPLES = 5 +# Rows per DELETE … WHERE rowid IN (…), below SQLite's bound-parameter limit. +_CHUNK = 500 + +# Check ids. Their names are in database_repair_dialog.py. +ORPHANS = "orphans" +PARENT_LINK = "parent_link" +WAYPOINT_PARENT = "waypoint_parent" +CORRECTED_FLAG = "corrected_flag" +COMPANION_ORPHANS = "companion_orphans" +COORDS = "coords" +CORRECTED_COORDS = "corrected_coords" +RATINGS = "ratings" +IDENTITY = "identity" + + +@dataclass(frozen=True) +class Finding: + """One check's result. *table* is set for the orphan checks (one finding + per table). *fixable* False: report only.""" + + check: str + count: int + fixable: bool + examples: tuple[str, ...] = () + table: Optional[str] = None + + @property + def key(self) -> str: + """Identifies the finding to repair(): the check, or check:table.""" + return f"{self.check}:{self.table}" if self.table else self.check + + +@dataclass(frozen=True) +class ScanResult: + path: Path + findings: tuple[Finding, ...] = () + # quick_check messages. When set, the file itself is damaged: the data + # checks did not run, and only a backup can help. + damaged: tuple[str, ...] = () + + @property + def problems(self) -> tuple[Finding, ...]: + return tuple(f for f in self.findings if f.count) + + @property + def fixable(self) -> tuple[Finding, ...]: + return tuple(f for f in self.problems if f.fixable) + + +@dataclass +class RepairResult: + """What repair() changed: finding key → rows fixed.""" + + path: Path + fixed: dict[str, int] = field(default_factory=dict) + + @property + def total(self) -> int: + return sum(self.fixed.values()) + + +# ── Report-only checks: (id, WHERE on caches) ───────────────────────────────── + +_COORDS_WHERE = ( + "latitude IS NULL OR longitude IS NULL " + "OR latitude NOT BETWEEN -90 AND 90 OR longitude NOT BETWEEN -180 AND 180" +) + + +def _rating_bad(col: str) -> str: + # NULL is fine: custom waypoints have no rating. + return (f"({col} IS NOT NULL AND ({col} NOT BETWEEN 1 AND 5 " + f"OR {col} * 2 <> CAST({col} * 2 AS INTEGER)))") + + +_REPORT_CHECKS: tuple[tuple[str, str], ...] = ( + (COORDS, _COORDS_WHERE), + (RATINGS, f"{_rating_bad('difficulty')} OR {_rating_bad('terrain')}"), + (IDENTITY, "gc_code IS NULL OR trim(gc_code) = '' OR name IS NULL OR trim(name) = ''"), +) + +_CORRECTED_COORDS_WHERE = ( + "n.is_corrected AND n.corrected_lat IS NOT NULL AND n.corrected_lon IS NOT NULL " + "AND (n.corrected_lat NOT BETWEEN -90 AND 90 OR n.corrected_lon NOT BETWEEN -180 AND 180)" +) + +# ── Fixable checks on the main database ─────────────────────────────────────── + +_PARENT_LINK_WHERE = ( + "parent_gc_code IS NOT NULL AND trim(parent_gc_code) <> '' " + "AND upper(trim(parent_gc_code)) NOT IN (SELECT gc_code FROM caches)" +) +_WAYPOINT_PARENT_FROM = ( + "FROM waypoints w JOIN caches c ON c.id = w.cache_id " + "WHERE w.parent_gc_code IS NOT c.gc_code" +) +def _corrected_flag_where(alias: str = "") -> str: + a = f"{alias}." if alias else "" + return f"{a}is_corrected AND ({a}corrected_lat IS NULL OR {a}corrected_lon IS NULL)" + + +# ── Scan ────────────────────────────────────────────────────────────────────── + +def scan(db_path: Path, with_companion: bool = False, + step: Optional[Callable[[str], None]] = None) -> ScanResult: + """ + Run every check on the database *db_path* (and on its companion when + *with_companion*). Changes nothing. *step* is called with each check id + before it runs. Raises sqlite3.Error if the file can't be read. + """ + db_path = Path(db_path) + notify = step or (lambda _id: None) + conn = _connect(db_path) + try: + notify("quick_check") + rows = [str(r[0]) for r in conn.execute("PRAGMA quick_check").fetchall()] + if rows != ["ok"]: + return ScanResult(db_path, damaged=tuple(rows)) + findings: list[Finding] = [] + + notify(ORPHANS) + for table, count in sorted(_orphan_rowids_by_table(conn, count_only=True).items()): + findings.append(Finding(ORPHANS, count, True, table=table)) + + notify(PARENT_LINK) + findings.append(_cache_finding(conn, PARENT_LINK, _PARENT_LINK_WHERE, True)) + + notify(WAYPOINT_PARENT) + count = conn.execute(f"SELECT count(*) {_WAYPOINT_PARENT_FROM}").fetchone()[0] + examples = _codes(conn, f"SELECT DISTINCT c.gc_code {_WAYPOINT_PARENT_FROM} " + "ORDER BY c.gc_code") if count else () + findings.append(Finding(WAYPOINT_PARENT, count, True, examples)) + + notify(CORRECTED_FLAG) + findings.append(_note_finding(conn, CORRECTED_FLAG, _corrected_flag_where("n"), True)) + + if with_companion: + notify(COMPANION_ORPHANS) + findings.append(Finding(COMPANION_ORPHANS, + sum(_companion_orphans(db_path).values()), True)) + + for check, where in _REPORT_CHECKS: + notify(check) + findings.append(_cache_finding(conn, check, where, False)) + notify(CORRECTED_COORDS) + findings.append(_note_finding(conn, CORRECTED_COORDS, _CORRECTED_COORDS_WHERE, False)) + finally: + conn.close() + return ScanResult(db_path, tuple(findings)) + + +def _cache_finding(conn: sqlite3.Connection, check: str, where: str, fixable: bool) -> Finding: + count = conn.execute(f"SELECT count(*) FROM caches WHERE {where}").fetchone()[0] + examples = _codes(conn, f"SELECT gc_code FROM caches WHERE {where} ORDER BY gc_code") \ + if count else () + return Finding(check, count, fixable, examples) + + +def _note_finding(conn: sqlite3.Connection, check: str, where: str, fixable: bool) -> Finding: + base = f"FROM user_notes n JOIN caches c ON c.id = n.cache_id WHERE {where}" + count = conn.execute(f"SELECT count(*) {base}").fetchone()[0] + examples = _codes(conn, f"SELECT c.gc_code {base} ORDER BY c.gc_code") if count else () + return Finding(check, count, fixable, examples) + + +def _codes(conn: sqlite3.Connection, sql: str) -> tuple[str, ...]: + rows = conn.execute(f"{sql} LIMIT {MAX_EXAMPLES}").fetchall() + # An empty GC code is one of the problems; show it as "''". + return tuple(str(r[0]) if r[0] not in (None, "") else "''" for r in rows) + + +def _orphan_rowids_by_table(conn: sqlite3.Connection, count_only: bool = False) -> dict: + """foreign_key_check grouped by table: table → row count (*count_only*) + or table → list of rowids.""" + found: dict = {} + for table, rowid, _parent, _fkid in conn.execute("PRAGMA foreign_key_check").fetchall(): + if count_only: + found[table] = found.get(table, 0) + 1 + elif rowid is not None: + found.setdefault(table, []).append(rowid) + if not count_only: + # A row with two broken keys is listed twice. + found = {t: sorted(set(ids)) for t, ids in found.items()} + return found + + +def _companion_orphans(db_path: Path) -> dict[str, int]: + from opensak.db import companion + + if not companion.exists(db_path): + return {} + result = companion.check(db_path) + return dict(result.orphans) if result is not None else {} + + +# ── Repair ──────────────────────────────────────────────────────────────────── + +def repair(db_path: Path, keys: set[str], + step: Optional[Callable[[str], None]] = None) -> RepairResult: + """ + Fix the findings *keys* (Finding.key of fixable findings) in *db_path*. + The main database's fixes run in one transaction; the companion is + cleaned up after it is committed. Keys of report-only checks are + ignored. The caller has released the database's connections. Raises + sqlite3.Error; then nothing in the main database has changed. + """ + db_path = Path(db_path) + notify = step or (lambda _id: None) + result = RepairResult(db_path) + conn = _connect(db_path) + try: + conn.execute("PRAGMA foreign_keys=ON") + with conn: + orphan_tables = {k.split(":", 1)[1] for k in keys if k.startswith(f"{ORPHANS}:")} + if orphan_tables: + notify(ORPHANS) + for table, rowids in _orphan_rowids_by_table(conn).items(): + if table in orphan_tables: + result.fixed[f"{ORPHANS}:{table}"] = _delete_rowids(conn, table, rowids) + if PARENT_LINK in keys: + notify(PARENT_LINK) + result.fixed[PARENT_LINK] = conn.execute( + f"UPDATE caches SET parent_gc_code = NULL WHERE {_PARENT_LINK_WHERE}" + ).rowcount + if WAYPOINT_PARENT in keys: + notify(WAYPOINT_PARENT) + result.fixed[WAYPOINT_PARENT] = conn.execute( + "UPDATE waypoints SET parent_gc_code = (SELECT gc_code FROM caches " + "WHERE caches.id = waypoints.cache_id) " + "WHERE cache_id IN (SELECT id FROM caches) AND parent_gc_code IS NOT " + "(SELECT gc_code FROM caches WHERE caches.id = waypoints.cache_id)" + ).rowcount + if CORRECTED_FLAG in keys: + notify(CORRECTED_FLAG) + result.fixed[CORRECTED_FLAG] = _clear_corrected_flags(conn) + finally: + conn.close() + if COMPANION_ORPHANS in keys: + from opensak.db import companion + + notify(COMPANION_ORPHANS) + if companion.exists(db_path): + result.fixed[COMPANION_ORPHANS] = sum(companion.cleanup(db_path).orphans.values()) + result.fixed = {k: v for k, v in result.fixed.items() if v > 0} + if result.total: + logger.info("repair of %s: %s", db_path, result.fixed) + return result + + +def _delete_rowids(conn: sqlite3.Connection, table: str, rowids: list[int]) -> int: + removed = 0 + quoted = '"' + table.replace('"', '""') + '"' + for start in range(0, len(rowids), _CHUNK): + chunk = rowids[start:start + _CHUNK] + marks = ", ".join("?" * len(chunk)) + removed += conn.execute(f"DELETE FROM {quoted} WHERE rowid IN ({marks})", chunk).rowcount + return removed + + +def _clear_corrected_flags(conn: sqlite3.Connection) -> int: + """Clear the flag, keeping the caches' last_user_update: the trigger + would set it to now, but a repair is not a user edit.""" + kept = conn.execute( + "SELECT c.id, c.last_user_update FROM caches c JOIN user_notes n ON n.cache_id = c.id " + f"WHERE {_corrected_flag_where('n')}" + ).fetchall() + count = conn.execute( + f"UPDATE user_notes SET is_corrected = 0, corrected_source = NULL " + f"WHERE {_corrected_flag_where()}" + ).rowcount + conn.executemany("UPDATE caches SET last_user_update = ? WHERE id = ?", + [(stamp, cache_id) for cache_id, stamp in kept]) + return count + + +def _connect(db_path: Path) -> sqlite3.Connection: + """A connection to the existing file *db_path* — never creates one.""" + if not db_path.is_file(): + raise sqlite3.OperationalError(f"unable to open database file: {db_path}") + return sqlite3.connect(str(db_path), timeout=30) diff --git a/src/opensak/gui/dialogs/database_maintenance_dialog.py b/src/opensak/gui/dialogs/database_maintenance_dialog.py index 7246dd3f..3c30d517 100644 --- a/src/opensak/gui/dialogs/database_maintenance_dialog.py +++ b/src/opensak/gui/dialogs/database_maintenance_dialog.py @@ -13,6 +13,8 @@ * **Checkpoint** — write the WAL back and empty the -wal file. * **Compact…** — VACUUM, only when it pays off, after a disk space check and optionally a backup set. +* **Repair…** — check the data and fix what can be fixed safely, in its own + dialog (database_repair_dialog.py, #1051). While the ``companion-db`` feature flag is on, a "Companion data" section lists the companion's tables per owner (kind, rows, estimated size) and @@ -392,8 +394,11 @@ def _setup_ui(self) -> None: self._btn_compact = QPushButton(tr("maint_btn_compact")) self._btn_compact.setToolTip(tr("maint_tip_compact")) self._btn_compact.clicked.connect(self._compact) + self._btn_repair = QPushButton(tr("maint_btn_repair")) + self._btn_repair.setToolTip(tr("maint_tip_repair")) + self._btn_repair.clicked.connect(self._open_repair) for w in (self._btn_check, self._thorough, self._btn_optimize, - self._btn_checkpoint, self._btn_compact): + self._btn_checkpoint, self._btn_compact, self._btn_repair): actions.addWidget(w) actions.addStretch() layout.addLayout(actions) @@ -420,7 +425,7 @@ def _setup_ui(self) -> None: self._action_buttons: list[QWidget] = [ self._btn_check, self._thorough, self._btn_optimize, - self._btn_checkpoint, self._btn_compact, + self._btn_checkpoint, self._btn_compact, self._btn_repair, ] def _build_companion_group(self) -> QGroupBox: @@ -657,6 +662,16 @@ def _on_compact_failed(self, error: str) -> None: self._append([tr("maint_backup_failed", error=error)]) self.refresh() + def _open_repair(self) -> None: + from opensak.gui.dialogs.database_repair_dialog import DatabaseRepairDialog + + dlg = DatabaseRepairDialog(self._db, self) + dlg.exec() + dlg.wait_idle() + dlg.deleteLater() + # A repair can compact the file and remove rows. + self.refresh() + # ── Companion data ──────────────────────────────────────────────────────── def _clear_table(self) -> None: diff --git a/src/opensak/gui/dialogs/database_repair_dialog.py b/src/opensak/gui/dialogs/database_repair_dialog.py new file mode 100644 index 00000000..401f9dbd --- /dev/null +++ b/src/opensak/gui/dialogs/database_repair_dialog.py @@ -0,0 +1,350 @@ +""" +src/opensak/gui/dialogs/database_repair_dialog.py — Repair… in the +Maintenance dialog (#1051, phase 1): check the data of a database and fix +what can be fixed safely, like GSAK's Repair/Defrag. + +1. Scan (read only, in the background): every check of db/repair.py with + its number of problems and a few example GC codes. +2. The user ticks what to fix. Report-only checks can't be ticked. "Make a + backup first" is ticked. +3. Repair: backup set, the fixes in one transaction, Optimize, and Compact + when more than a quarter of the file is free (as GSAK does). Then a + summary, and the scan runs again. + +A damaged file (quick_check fails) can't be repaired by SQLite; the dialog +points to File → Restore from backup… instead. + +Works on the active database (its connections are released first, as for +Compact) and on any other database in the list. The work is in run_scan() +and run_repair(), which return plain results, so they can be tested +without threads. +""" + +from __future__ import annotations + +import logging +import time +from dataclasses import dataclass +from pathlib import Path +from typing import Any, Callable, Optional + +from PySide6.QtCore import Qt +from PySide6.QtWidgets import ( + QCheckBox, QDialog, QDialogButtonBox, QHBoxLayout, QHeaderView, QLabel, + QPlainTextEdit, QProgressBar, QPushButton, QTreeWidget, QTreeWidgetItem, + QVBoxLayout, QWidget, +) + +from opensak.db import companion, maintenance, repair +from opensak.gui.dialogs.database_maintenance_dialog import ( + CompactOutcome, MaintenanceWorker, Step, compact_lines, +) +from opensak.gui.dialogs.widgets import clamp_dialog_height_to_screen +from opensak.lang import tr +from opensak.utils import flags + +logger = logging.getLogger(__name__) + +# Compact after a repair when at least this share of the file is free (GSAK). +COMPACT_FRACTION = 0.25 +# The finding key of a row in the tree. +KEY_ROLE = Qt.ItemDataRole.UserRole +# Damage messages listed; the rest is in the log. +_MAX_DAMAGE_LINES = 20 + +# Each check's name, as a translation key. +_CHECK_TEXT = { + repair.ORPHANS: "repair_chk_orphans", + repair.PARENT_LINK: "repair_chk_parent_link", + repair.WAYPOINT_PARENT: "repair_chk_waypoint_parent", + repair.CORRECTED_FLAG: "repair_chk_corrected_flag", + repair.COMPANION_ORPHANS: "repair_chk_companion_orphans", + repair.COORDS: "repair_chk_coords", + repair.CORRECTED_COORDS: "repair_chk_corrected_coords", + repair.RATINGS: "repair_chk_ratings", + repair.IDENTITY: "repair_chk_identity", +} +# The fix each fixable check makes, as a translation key. +_FIX_TEXT = { + repair.ORPHANS: "repair_fix_delete", + repair.PARENT_LINK: "repair_fix_clear_link", + repair.WAYPOINT_PARENT: "repair_fix_set_parent", + repair.CORRECTED_FLAG: "repair_fix_clear_flag", + repair.COMPANION_ORPHANS: "repair_fix_delete", +} + + +@dataclass +class RepairOutcome: + result: repair.RepairResult + backup_path: Optional[Path] = None + compact: Optional[maintenance.VacuumResult] = None + + +# ── The work (worker thread) ────────────────────────────────────────────────── + +def _with_companion(db_path: Path) -> bool: + return bool(flags.companion_db) and companion.exists(db_path) + + +def run_scan(db_path: Path, step: Step) -> repair.ScanResult: + step(tr("repair_step_scan")) + return repair.scan(db_path, with_companion=_with_companion(db_path)) + + +def run_repair(db: Any, keys: set[str], backup: bool, step: Step) -> RepairOutcome: + """Back up *db* (when *backup*), fix *keys*, then Optimize and Compact + when it pays off. The caller has released the database's connections. + A failed backup raises BackupError, and then nothing is changed.""" + backup_path = None + if backup: + from opensak.backup.backupset import KIND_MANUAL, write_backup_set + + step(tr("maint_step_backup")) + backup_path = write_backup_set([db], KIND_MANUAL).backup_set.path + step(tr("repair_step_fix")) + outcome = RepairOutcome(repair.repair(db.path, keys), backup_path) + step(tr("maint_step_optimize", file=tr("maint_db_group"))) + maintenance.optimize_file(db.path) + stats = maintenance.file_info(db.path).stats + if maintenance.is_worthwhile(stats, maintenance.MANUAL_MIN_RECLAIM_BYTES, COMPACT_FRACTION): + step(tr("maint_step_compact", file=tr("maint_db_group"))) + # vacuum() checks the free disk space itself. + outcome.compact = maintenance.vacuum_file( + db.path, min_bytes=maintenance.MANUAL_MIN_RECLAIM_BYTES, + min_fraction=COMPACT_FRACTION) + if outcome.compact.ran: + maintenance.optimize_file(db.path) + return outcome + + +# ── Text ────────────────────────────────────────────────────────────────────── + +def check_label(finding: repair.Finding) -> str: + key = _CHECK_TEXT[finding.check] + return tr(key, table=finding.table) if finding.table else tr(key) + + +def _label_for_key(key: str) -> str: + check, _, table = key.partition(":") + return check_label(repair.Finding(check, 0, True, table=table or None)) + + +def repair_lines(outcome: RepairOutcome) -> list[str]: + lines = [] + if outcome.backup_path is not None: + lines.append(tr("maint_backup_done", path=str(outcome.backup_path))) + if not outcome.result.fixed: + lines.append(tr("repair_nothing_fixed")) + for key, count in outcome.result.fixed.items(): + lines.append(tr("repair_fixed", count=f"{count:,}", check=_label_for_key(key))) + if outcome.compact is not None: + lines += compact_lines(CompactOutcome(results=[outcome.compact])) + return lines + + +# ── Dialog ──────────────────────────────────────────────────────────────────── + +class DatabaseRepairDialog(QDialog): + """Repair of the database *db* (an entry of the database list).""" + + def __init__(self, db: Any, parent: Optional[QWidget] = None) -> None: + super().__init__(parent) + self._db = db + self.scan_result: Optional[repair.ScanResult] = None + self._worker: Optional[MaintenanceWorker] = None + self.setWindowTitle(tr("repair_title", name=db.name)) + self.setMinimumSize(720, 460) + self._setup_ui() + clamp_dialog_height_to_screen(self, parent) + self._scan() + + def _setup_ui(self) -> None: + layout = QVBoxLayout(self) + intro = QLabel(tr("repair_intro")) + intro.setWordWrap(True) + layout.addWidget(intro) + + self._summary = QLabel("") + self._summary.setWordWrap(True) + layout.addWidget(self._summary) + + self._tree = QTreeWidget() + self._tree.setRootIsDecorated(False) + self._tree.setHeaderLabels([ + tr("repair_col_check"), tr("repair_col_count"), tr("repair_col_fix"), + tr("repair_col_examples"), + ]) + header = self._tree.header() + if header is not None: + header.setSectionResizeMode(0, QHeaderView.ResizeMode.Stretch) + for col in (1, 2, 3): + header.setSectionResizeMode(col, QHeaderView.ResizeMode.ResizeToContents) + self._tree.itemChanged.connect(lambda *_: self._update_buttons()) + layout.addWidget(self._tree, 2) + + row = QHBoxLayout() + self._backup = QCheckBox(tr("maint_compact_backup")) + self._backup.setChecked(True) + self._btn_rescan = QPushButton(tr("repair_btn_rescan")) + self._btn_rescan.clicked.connect(self._scan) + self._btn_repair = QPushButton(tr("repair_btn_repair")) + self._btn_repair.clicked.connect(self._repair) + row.addWidget(self._backup) + row.addStretch() + row.addWidget(self._btn_rescan) + row.addWidget(self._btn_repair) + layout.addLayout(row) + + busy_row = QHBoxLayout() + self._busy = QProgressBar() + self._busy.setRange(0, 0) + self._busy.setMaximumWidth(160) + self._busy.setVisible(False) + self._step = QLabel("") + busy_row.addWidget(self._busy) + busy_row.addWidget(self._step, 1) + layout.addLayout(busy_row) + + layout.addWidget(QLabel(f"{tr('dist_result_group')}")) + self._results = QPlainTextEdit() + self._results.setReadOnly(True) + self._results.setMinimumHeight(80) + layout.addWidget(self._results, 1) + + buttons = QDialogButtonBox(QDialogButtonBox.StandardButton.Close) + buttons.rejected.connect(self.reject) + layout.addWidget(buttons) + + # ── Scan ────────────────────────────────────────────────────────────────── + + def _scan(self) -> None: + self.scan_result = None + self._run(lambda step: run_scan(self._db.path, step), self._show_scan) + + def _show_scan(self, result: repair.ScanResult) -> None: + self._tree.blockSignals(True) + self._tree.clear() + for finding in result.problems: + item = QTreeWidgetItem([ + check_label(finding), f"{finding.count:,}", + tr(_FIX_TEXT[finding.check]) if finding.fixable else tr("repair_report_only"), + ", ".join(finding.examples) + (" …" if finding.count > len(finding.examples) + and finding.examples else ""), + ]) + item.setData(0, KEY_ROLE, finding.key) + item.setTextAlignment(1, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter) + if finding.fixable: + item.setFlags(item.flags() | Qt.ItemFlag.ItemIsUserCheckable) + item.setCheckState(0, Qt.CheckState.Checked) + else: + item.setFlags(item.flags() & ~Qt.ItemFlag.ItemIsUserCheckable) + self._tree.addTopLevelItem(item) + self._tree.blockSignals(False) + + if result.damaged: + self._summary.setText(f"{tr('repair_damaged')} {tr('maint_check_main_failed')}") + self._append(list(result.damaged[:_MAX_DAMAGE_LINES])) + elif not result.problems: + self._summary.setText(tr("repair_scan_clean")) + else: + self._summary.setText(tr("repair_scan_summary", problems=len(result.problems), + fixable=len(result.fixable))) + self.scan_result = result + self._set_busy(False) + + def checked_keys(self) -> set[str]: + keys = set() + for i in range(self._tree.topLevelItemCount()): + item = self._tree.topLevelItem(i) + if (item is not None and item.flags() & Qt.ItemFlag.ItemIsUserCheckable + and item.checkState(0) == Qt.CheckState.Checked): + keys.add(str(item.data(0, KEY_ROLE))) + return keys + + # ── Repair ──────────────────────────────────────────────────────────────── + + def _repair(self) -> None: + keys = self.checked_keys() + if not keys or self.is_running(): + return + backup = self._backup.isChecked() + from opensak.db.database import release_connections + + release_connections(self._db.path) + self._run(lambda step: run_repair(self._db, keys, backup, step), self._on_repaired, + failed=self._on_repair_failed) + + def _on_repaired(self, outcome: RepairOutcome) -> None: + if outcome.backup_path is not None: + try: + from opensak.backup.exit_state import record_backup_state + record_backup_state([self._db]) + except Exception: + logger.warning("repair: could not record the backup state", exc_info=True) + self._set_busy(False) + self._append(repair_lines(outcome)) + self._rescan_when_done() + + def _on_repair_failed(self, error: str) -> None: + self._set_busy(False) + self._append([tr("repair_failed", error=error)]) + self._rescan_when_done() + + def _rescan_when_done(self) -> None: + # Called from the worker's last signal: its thread is about to end, + # and _run() starts nothing while it still runs. + self.wait_idle() + self._scan() + + # ── Running jobs ────────────────────────────────────────────────────────── + + def is_running(self) -> bool: + return self._worker is not None and self._worker.isRunning() + + def _run(self, job: Callable[[Step], object], done: Callable[[Any], None], + failed: Optional[Callable[[str], None]] = None) -> None: + if self.is_running(): + return + self._set_busy(True) + worker = MaintenanceWorker(job, self) + worker.step.connect(self._step.setText) + worker.succeeded.connect(done) + worker.failed.connect(failed or self._on_failed) + self._worker = worker + worker.start() + + def _on_failed(self, error: str) -> None: + self._set_busy(False) + self._append([tr("maint_failed", file=self._db.name, error=error)]) + self._summary.setText("") + self.scan_result = repair.ScanResult(Path(self._db.path)) + + def _set_busy(self, busy: bool) -> None: + self._busy.setVisible(busy) + if not busy: + self._step.setText("") + self._tree.setEnabled(not busy) + self._backup.setEnabled(not busy) + self._btn_rescan.setEnabled(not busy) + self._btn_repair.setEnabled(not busy and bool(self.checked_keys())) + + def _update_buttons(self) -> None: + self._btn_repair.setEnabled(not self.is_running() and bool(self.checked_keys())) + + def _append(self, lines: list[str]) -> None: + if not lines: + return + stamp = time.strftime("%H:%M:%S") + self._results.appendPlainText("\n".join(f"{stamp} {line}" for line in lines)) + + def reject(self) -> None: + # A running repair or VACUUM can't be interrupted safely. + if self.is_running(): + return + super().reject() + + def wait_idle(self, msecs: int = 60000) -> None: + """Block until the running step is done (for tests and closing).""" + if self._worker is not None: + self._worker.wait(msecs) diff --git a/src/opensak/lang/cs.py b/src/opensak/lang/cs.py index d2ca9622..ab54ce84 100644 --- a/src/opensak/lang/cs.py +++ b/src/opensak/lang/cs.py @@ -1107,6 +1107,38 @@ "maint_compact_total": "Celkem: {before} → {after}.", "maint_backup_failed": "Nic nebylo zkomprimováno:\n{error}", "maint_backup_done": "Záloha vytvořena: {path}", + "maint_btn_repair": "Opravit…", + "maint_tip_repair": "Zkontroluje data (řádky bez keše, poškozené odkazy, neplatné hodnoty) a opraví, co lze bezpečně opravit.", + "repair_title": "Oprava — {name}", + "repair_intro": "Zkontroluje data databáze. Zaškrtnuté problémy budou opraveny. Problémy označené \"Pouze hlásit\" nemají bezpečnou automatickou opravu: zkontrolujte tyto keše sami.", + "repair_col_check": "Kontrola", + "repair_col_count": "Problémy", + "repair_col_fix": "Oprava", + "repair_col_examples": "Příklady", + "repair_chk_orphans": "Řádky v {table}, jejichž keš již neexistuje", + "repair_chk_parent_link": "Vlastní waypointy propojené s keší, která není v databázi", + "repair_chk_waypoint_parent": "Waypointy s nesprávným nadřazeným GC kódem", + "repair_chk_corrected_flag": "Keše označené jako opravené bez opravených souřadnic", + "repair_chk_companion_orphans": "Řádky v doprovodné databázi, jejichž keš již neexistuje", + "repair_chk_coords": "Souřadnice chybí nebo jsou mimo rozsah", + "repair_chk_corrected_coords": "Opravené souřadnice mimo rozsah", + "repair_chk_ratings": "Obtížnost nebo terén není mezi 1 a 5 v krocích po 0,5", + "repair_chk_identity": "Prázdný GC kód nebo název", + "repair_fix_delete": "Smazat", + "repair_fix_clear_link": "Odstranit odkaz", + "repair_fix_set_parent": "Nastavit kód keše", + "repair_fix_clear_flag": "Zrušit označení", + "repair_report_only": "Pouze hlásit", + "repair_btn_repair": "Opravit", + "repair_btn_rescan": "Zkontrolovat znovu", + "repair_step_scan": "Kontrola dat…", + "repair_step_fix": "Oprava zaškrtnutých problémů…", + "repair_scan_clean": "Nebyly nalezeny žádné problémy.", + "repair_scan_summary": "Problémy našlo kontrol: {problems}, z toho lze zde opravit: {fixable}.", + "repair_damaged": "Samotný soubor databáze je poškozen a SQLite jej nedokáže opravit.", + "repair_fixed": "Opraveno {count}: {check}", + "repair_nothing_fixed": "Nic nebylo změněno.", + "repair_failed": "Oprava selhala, nic nebylo změněno:\n{error}", "maint_cleanup_done": "{file}: odstraněny zbylé řádky ({orphans}) a záznamy registru ({registry}).", "maint_cleanup_nothing": "{file}: není co uklízet.", "maint_clear_confirm": "Smazat všechny řádky ({rows}) tabulky {table}?\nVlastník: {owner}\n\nTabulka zůstane. Řádky lze vrátit jen ze zálohy.", diff --git a/src/opensak/lang/da.py b/src/opensak/lang/da.py index 2d02da6c..c6362ff5 100644 --- a/src/opensak/lang/da.py +++ b/src/opensak/lang/da.py @@ -1107,6 +1107,38 @@ "maint_compact_total": "I alt: {before} → {after}.", "maint_backup_failed": "Intet blev komprimeret:\n{error}", "maint_backup_done": "Sikkerhedskopi lavet: {path}", + "maint_btn_repair": "Reparér…", + "maint_tip_repair": "Kontrollerer data (rækker uden cache, brudte links, ugyldige værdier) og retter det, der sikkert kan rettes.", + "repair_title": "Reparér — {name}", + "repair_intro": "Kontrollerer databasens data. De afkrydsede problemer bliver rettet. Problemer markeret \"Kun rapport\" har ingen sikker automatisk rettelse: kontrollér selv de caches.", + "repair_col_check": "Kontrol", + "repair_col_count": "Problemer", + "repair_col_fix": "Rettelse", + "repair_col_examples": "Eksempler", + "repair_chk_orphans": "Rækker i {table}, hvis cache ikke længere findes", + "repair_chk_parent_link": "Brugerdefinerede waypoints knyttet til en cache, der ikke er i databasen", + "repair_chk_waypoint_parent": "Waypoints med forkert overordnet GC-kode", + "repair_chk_corrected_flag": "Caches markeret som korrigeret uden korrigerede koordinater", + "repair_chk_companion_orphans": "Rækker i ledsagerdatabasen, hvis cache ikke længere findes", + "repair_chk_coords": "Koordinater mangler eller er uden for gyldigt område", + "repair_chk_corrected_coords": "Korrigerede koordinater uden for gyldigt område", + "repair_chk_ratings": "Sværhedsgrad eller terræn ikke mellem 1 og 5 i trin på 0,5", + "repair_chk_identity": "Tom GC-kode eller navn", + "repair_fix_delete": "Slet", + "repair_fix_clear_link": "Fjern linket", + "repair_fix_set_parent": "Sæt cachens kode", + "repair_fix_clear_flag": "Fjern markeringen", + "repair_report_only": "Kun rapport", + "repair_btn_repair": "Reparér", + "repair_btn_rescan": "Kontrollér igen", + "repair_step_scan": "Kontrollerer data…", + "repair_step_fix": "Retter de afkrydsede problemer…", + "repair_scan_clean": "Ingen problemer fundet.", + "repair_scan_summary": "{problems} kontrol(ler) fandt problemer, {fixable} af dem kan rettes her.", + "repair_damaged": "Selve databasefilen er beskadiget, og SQLite kan ikke reparere den.", + "repair_fixed": "Rettet {count}: {check}", + "repair_nothing_fixed": "Intet blev ændret.", + "repair_failed": "Reparationen mislykkedes, intet blev ændret:\n{error}", "maint_cleanup_done": "{file}: fjernede {orphans} overskydende række(r) og {registry} registreringer.", "maint_cleanup_nothing": "{file}: intet at rydde op.", "maint_clear_confirm": "Slet alle {rows} rækker i tabellen {table}?\nEjer: {owner}\n\nTabellen bliver. Rækkerne kan kun hentes tilbage fra en sikkerhedskopi.", diff --git a/src/opensak/lang/de.py b/src/opensak/lang/de.py index b48419a6..3b747f0c 100644 --- a/src/opensak/lang/de.py +++ b/src/opensak/lang/de.py @@ -1108,6 +1108,38 @@ "maint_compact_total": "Gesamt: {before} → {after}.", "maint_backup_failed": "Es wurde nichts komprimiert:\n{error}", "maint_backup_done": "Sicherung erstellt: {path}", + "maint_btn_repair": "Reparieren…", + "maint_tip_repair": "Prüft die Daten (Zeilen ohne Cache, defekte Verknüpfungen, ungültige Werte) und behebt, was sicher behoben werden kann.", + "repair_title": "Reparieren — {name}", + "repair_intro": "Prüft die Daten der Datenbank. Die angekreuzten Probleme werden behoben. Für Probleme mit \"Nur melden\" gibt es keine sichere automatische Lösung: Prüfen Sie diese Caches selbst.", + "repair_col_check": "Prüfung", + "repair_col_count": "Probleme", + "repair_col_fix": "Behebung", + "repair_col_examples": "Beispiele", + "repair_chk_orphans": "Zeilen in {table}, deren Cache nicht mehr existiert", + "repair_chk_parent_link": "Eigene Wegpunkte, die mit einem Cache verknüpft sind, der nicht in der Datenbank ist", + "repair_chk_waypoint_parent": "Wegpunkte mit falschem übergeordnetem GC-Code", + "repair_chk_corrected_flag": "Caches, die als korrigiert markiert sind, aber keine korrigierten Koordinaten haben", + "repair_chk_companion_orphans": "Zeilen in der Begleitdatenbank, deren Cache nicht mehr existiert", + "repair_chk_coords": "Koordinaten fehlen oder liegen außerhalb des gültigen Bereichs", + "repair_chk_corrected_coords": "Korrigierte Koordinaten außerhalb des gültigen Bereichs", + "repair_chk_ratings": "Schwierigkeit oder Gelände nicht zwischen 1 und 5 in Schritten von 0,5", + "repair_chk_identity": "Leerer GC-Code oder Name", + "repair_fix_delete": "Löschen", + "repair_fix_clear_link": "Verknüpfung entfernen", + "repair_fix_set_parent": "Code des Caches setzen", + "repair_fix_clear_flag": "Markierung entfernen", + "repair_report_only": "Nur melden", + "repair_btn_repair": "Reparieren", + "repair_btn_rescan": "Erneut prüfen", + "repair_step_scan": "Daten werden geprüft…", + "repair_step_fix": "Angekreuzte Probleme werden behoben…", + "repair_scan_clean": "Keine Probleme gefunden.", + "repair_scan_summary": "{problems} Prüfung(en) haben Probleme gefunden, {fixable} davon können hier behoben werden.", + "repair_damaged": "Die Datenbankdatei selbst ist beschädigt, und SQLite kann sie nicht reparieren.", + "repair_fixed": "Behoben {count}: {check}", + "repair_nothing_fixed": "Es wurde nichts geändert.", + "repair_failed": "Reparatur fehlgeschlagen, es wurde nichts geändert:\n{error}", "maint_cleanup_done": "{file}: {orphans} übrig gebliebene Zeile(n) und {registry} Registereinträge entfernt.", "maint_cleanup_nothing": "{file}: nichts aufzuräumen.", "maint_clear_confirm": "Alle {rows} Zeilen der Tabelle {table} löschen?\nBesitzer: {owner}\n\nDie Tabelle bleibt. Die Zeilen lassen sich nur aus einer Sicherung zurückholen.", diff --git a/src/opensak/lang/de_CH.py b/src/opensak/lang/de_CH.py index 414a0805..7e82d18c 100644 --- a/src/opensak/lang/de_CH.py +++ b/src/opensak/lang/de_CH.py @@ -1109,6 +1109,38 @@ "maint_compact_total": "Gesamt: {before} → {after}.", "maint_backup_failed": "Es wurde nichts komprimiert:\n{error}", "maint_backup_done": "Sicherung erstellt: {path}", + "maint_btn_repair": "Reparieren…", + "maint_tip_repair": "Prüft die Daten (Zeilen ohne Cache, defekte Verknüpfungen, ungültige Werte) und behebt, was sicher behoben werden kann.", + "repair_title": "Reparieren — {name}", + "repair_intro": "Prüft die Daten der Datenbank. Die angekreuzten Probleme werden behoben. Für Probleme mit \"Nur melden\" gibt es keine sichere automatische Lösung: Prüfen Sie diese Caches selbst.", + "repair_col_check": "Prüfung", + "repair_col_count": "Probleme", + "repair_col_fix": "Behebung", + "repair_col_examples": "Beispiele", + "repair_chk_orphans": "Zeilen in {table}, deren Cache nicht mehr existiert", + "repair_chk_parent_link": "Eigene Wegpunkte, die mit einem Cache verknüpft sind, der nicht in der Datenbank ist", + "repair_chk_waypoint_parent": "Wegpunkte mit falschem übergeordnetem GC-Code", + "repair_chk_corrected_flag": "Caches, die als korrigiert markiert sind, aber keine korrigierten Koordinaten haben", + "repair_chk_companion_orphans": "Zeilen in der Begleitdatenbank, deren Cache nicht mehr existiert", + "repair_chk_coords": "Koordinaten fehlen oder liegen ausserhalb des gültigen Bereichs", + "repair_chk_corrected_coords": "Korrigierte Koordinaten ausserhalb des gültigen Bereichs", + "repair_chk_ratings": "Schwierigkeit oder Gelände nicht zwischen 1 und 5 in Schritten von 0,5", + "repair_chk_identity": "Leerer GC-Code oder Name", + "repair_fix_delete": "Löschen", + "repair_fix_clear_link": "Verknüpfung entfernen", + "repair_fix_set_parent": "Code des Caches setzen", + "repair_fix_clear_flag": "Markierung entfernen", + "repair_report_only": "Nur melden", + "repair_btn_repair": "Reparieren", + "repair_btn_rescan": "Erneut prüfen", + "repair_step_scan": "Daten werden geprüft…", + "repair_step_fix": "Angekreuzte Probleme werden behoben…", + "repair_scan_clean": "Keine Probleme gefunden.", + "repair_scan_summary": "{problems} Prüfung(en) haben Probleme gefunden, {fixable} davon können hier behoben werden.", + "repair_damaged": "Die Datenbankdatei selbst ist beschädigt, und SQLite kann sie nicht reparieren.", + "repair_fixed": "Behoben {count}: {check}", + "repair_nothing_fixed": "Es wurde nichts geändert.", + "repair_failed": "Reparatur fehlgeschlagen, es wurde nichts geändert:\n{error}", "maint_cleanup_done": "{file}: {orphans} übrig gebliebene Zeile(n) und {registry} Registereinträge entfernt.", "maint_cleanup_nothing": "{file}: nichts aufzuräumen.", "maint_clear_confirm": "Alle {rows} Zeilen der Tabelle {table} löschen?\nBesitzer: {owner}\n\nDie Tabelle bleibt. Die Zeilen lassen sich nur aus einer Sicherung zurückholen.", diff --git a/src/opensak/lang/en.py b/src/opensak/lang/en.py index 815eee48..27553250 100644 --- a/src/opensak/lang/en.py +++ b/src/opensak/lang/en.py @@ -1106,6 +1106,38 @@ "maint_compact_total": "Total: {before} → {after}.", "maint_backup_failed": "Nothing was compacted:\n{error}", "maint_backup_done": "Backup made: {path}", + "maint_btn_repair": "Repair…", + "maint_tip_repair": "Checks the data (rows without a cache, broken links, invalid values) and fixes what can be fixed safely.", + "repair_title": "Repair — {name}", + "repair_intro": "Checks the data of the database. The ticked problems are fixed. Problems marked \"Report only\" have no safe automatic fix: check those caches yourself.", + "repair_col_check": "Check", + "repair_col_count": "Problems", + "repair_col_fix": "Fix", + "repair_col_examples": "Examples", + "repair_chk_orphans": "Rows in {table} whose cache no longer exists", + "repair_chk_parent_link": "Custom waypoints linked to a cache that isn't in the database", + "repair_chk_waypoint_parent": "Waypoints with the wrong parent GC code", + "repair_chk_corrected_flag": "Caches marked as corrected without corrected coordinates", + "repair_chk_companion_orphans": "Rows in the companion database whose cache no longer exists", + "repair_chk_coords": "Coordinates missing or out of range", + "repair_chk_corrected_coords": "Corrected coordinates out of range", + "repair_chk_ratings": "Difficulty or terrain not between 1 and 5 in steps of 0.5", + "repair_chk_identity": "Empty GC code or name", + "repair_fix_delete": "Delete", + "repair_fix_clear_link": "Clear the link", + "repair_fix_set_parent": "Set the cache's code", + "repair_fix_clear_flag": "Clear the mark", + "repair_report_only": "Report only", + "repair_btn_repair": "Repair", + "repair_btn_rescan": "Scan again", + "repair_step_scan": "Checking the data…", + "repair_step_fix": "Fixing the ticked problems…", + "repair_scan_clean": "No problems found.", + "repair_scan_summary": "{problems} check(s) found problems, {fixable} of them can be fixed here.", + "repair_damaged": "The database file itself is damaged, and SQLite can't repair it.", + "repair_fixed": "Fixed {count}: {check}", + "repair_nothing_fixed": "Nothing was changed.", + "repair_failed": "Repair failed, nothing was changed:\n{error}", "maint_cleanup_done": "{file}: removed {orphans} leftover row(s) and {registry} registry entries.", "maint_cleanup_nothing": "{file}: nothing to clean up.", "maint_clear_confirm": "Delete all {rows} rows of the table {table}?\nOwner: {owner}\n\nThe table stays. The rows can only be brought back from a backup.", diff --git a/src/opensak/lang/es.py b/src/opensak/lang/es.py index 29d4a26c..743e6140 100644 --- a/src/opensak/lang/es.py +++ b/src/opensak/lang/es.py @@ -1108,6 +1108,38 @@ "maint_compact_total": "Total: {before} → {after}.", "maint_backup_failed": "No se compactó nada:\n{error}", "maint_backup_done": "Copia de seguridad hecha: {path}", + "maint_btn_repair": "Reparar…", + "maint_tip_repair": "Comprueba los datos (filas sin caché, enlaces rotos, valores no válidos) y corrige lo que se puede corregir de forma segura.", + "repair_title": "Reparar — {name}", + "repair_intro": "Comprueba los datos de la base de datos. Se corrigen los problemas marcados. Los problemas con \"Solo informar\" no tienen una corrección automática segura: revise esos cachés usted mismo.", + "repair_col_check": "Comprobación", + "repair_col_count": "Problemas", + "repair_col_fix": "Corrección", + "repair_col_examples": "Ejemplos", + "repair_chk_orphans": "Filas en {table} cuyo caché ya no existe", + "repair_chk_parent_link": "Waypoints personalizados vinculados a un caché que no está en la base de datos", + "repair_chk_waypoint_parent": "Waypoints con el código GC principal incorrecto", + "repair_chk_corrected_flag": "Cachés marcados como corregidos sin coordenadas corregidas", + "repair_chk_companion_orphans": "Filas en la base de datos complementaria cuyo caché ya no existe", + "repair_chk_coords": "Coordenadas que faltan o fuera de rango", + "repair_chk_corrected_coords": "Coordenadas corregidas fuera de rango", + "repair_chk_ratings": "Dificultad o terreno no entre 1 y 5 en pasos de 0,5", + "repair_chk_identity": "Código GC o nombre vacío", + "repair_fix_delete": "Eliminar", + "repair_fix_clear_link": "Quitar el vínculo", + "repair_fix_set_parent": "Poner el código del caché", + "repair_fix_clear_flag": "Quitar la marca", + "repair_report_only": "Solo informar", + "repair_btn_repair": "Reparar", + "repair_btn_rescan": "Comprobar de nuevo", + "repair_step_scan": "Comprobando los datos…", + "repair_step_fix": "Corrigiendo los problemas marcados…", + "repair_scan_clean": "No se encontraron problemas.", + "repair_scan_summary": "{problems} comprobación(es) encontraron problemas, {fixable} se pueden corregir aquí.", + "repair_damaged": "El propio archivo de la base de datos está dañado y SQLite no puede repararlo.", + "repair_fixed": "Corregido {count}: {check}", + "repair_nothing_fixed": "No se cambió nada.", + "repair_failed": "La reparación falló, no se cambió nada:\n{error}", "maint_cleanup_done": "{file}: eliminadas {orphans} fila(s) sobrantes y {registry} entradas del registro.", "maint_cleanup_nothing": "{file}: nada que limpiar.", "maint_clear_confirm": "¿Borrar las {rows} filas de la tabla {table}?\nPropietario: {owner}\n\nLa tabla se queda. Las filas solo se pueden recuperar desde una copia de seguridad.", diff --git a/src/opensak/lang/fr.py b/src/opensak/lang/fr.py index 94dd43ad..59fe588b 100644 --- a/src/opensak/lang/fr.py +++ b/src/opensak/lang/fr.py @@ -1108,6 +1108,38 @@ "maint_compact_total": "Total : {before} → {after}.", "maint_backup_failed": "Rien n'a été compacté :\n{error}", "maint_backup_done": "Sauvegarde faite : {path}", + "maint_btn_repair": "Réparer…", + "maint_tip_repair": "Vérifie les données (lignes sans cache, liens rompus, valeurs non valides) et corrige ce qui peut l'être sans risque.", + "repair_title": "Réparer — {name}", + "repair_intro": "Vérifie les données de la base. Les problèmes cochés sont corrigés. Les problèmes marqués « Signaler seulement » n'ont pas de correction automatique sûre : vérifiez ces caches vous-même.", + "repair_col_check": "Vérification", + "repair_col_count": "Problèmes", + "repair_col_fix": "Correction", + "repair_col_examples": "Exemples", + "repair_chk_orphans": "Lignes de {table} dont la cache n'existe plus", + "repair_chk_parent_link": "Waypoints personnalisés liés à une cache absente de la base", + "repair_chk_waypoint_parent": "Waypoints avec un mauvais code GC parent", + "repair_chk_corrected_flag": "Caches marquées comme corrigées sans coordonnées corrigées", + "repair_chk_companion_orphans": "Lignes de la base compagnon dont la cache n'existe plus", + "repair_chk_coords": "Coordonnées manquantes ou hors limites", + "repair_chk_corrected_coords": "Coordonnées corrigées hors limites", + "repair_chk_ratings": "Difficulté ou terrain hors de 1 à 5 ou pas par pas de 0,5", + "repair_chk_identity": "Code GC ou nom vide", + "repair_fix_delete": "Supprimer", + "repair_fix_clear_link": "Retirer le lien", + "repair_fix_set_parent": "Mettre le code de la cache", + "repair_fix_clear_flag": "Retirer la marque", + "repair_report_only": "Signaler seulement", + "repair_btn_repair": "Réparer", + "repair_btn_rescan": "Vérifier à nouveau", + "repair_step_scan": "Vérification des données…", + "repair_step_fix": "Correction des problèmes cochés…", + "repair_scan_clean": "Aucun problème trouvé.", + "repair_scan_summary": "{problems} vérification(s) ont trouvé des problèmes, {fixable} peuvent être corrigées ici.", + "repair_damaged": "Le fichier de la base lui-même est endommagé et SQLite ne peut pas le réparer.", + "repair_fixed": "Corrigé {count} : {check}", + "repair_nothing_fixed": "Rien n'a été modifié.", + "repair_failed": "La réparation a échoué, rien n'a été modifié :\n{error}", "maint_cleanup_done": "{file} : {orphans} ligne(s) restante(s) et {registry} entrées du registre supprimées.", "maint_cleanup_nothing": "{file} : rien à nettoyer.", "maint_clear_confirm": "Supprimer les {rows} lignes de la table {table} ?\nPropriétaire : {owner}\n\nLa table reste. Les lignes ne peuvent être récupérées que depuis une sauvegarde.", diff --git a/src/opensak/lang/nl.py b/src/opensak/lang/nl.py index 6f629247..7c0dc0c2 100644 --- a/src/opensak/lang/nl.py +++ b/src/opensak/lang/nl.py @@ -1110,6 +1110,38 @@ "maint_compact_total": "Totaal: {before} → {after}.", "maint_backup_failed": "Er is niets gecomprimeerd:\n{error}", "maint_backup_done": "Back-up gemaakt: {path}", + "maint_btn_repair": "Repareren…", + "maint_tip_repair": "Controleert de gegevens (rijen zonder cache, verbroken koppelingen, ongeldige waarden) en herstelt wat veilig hersteld kan worden.", + "repair_title": "Repareren — {name}", + "repair_intro": "Controleert de gegevens van de database. De aangevinkte problemen worden hersteld. Problemen met \"Alleen melden\" hebben geen veilige automatische oplossing: controleer die caches zelf.", + "repair_col_check": "Controle", + "repair_col_count": "Problemen", + "repair_col_fix": "Oplossing", + "repair_col_examples": "Voorbeelden", + "repair_chk_orphans": "Rijen in {table} waarvan de cache niet meer bestaat", + "repair_chk_parent_link": "Eigen waypoints gekoppeld aan een cache die niet in de database staat", + "repair_chk_waypoint_parent": "Waypoints met de verkeerde bovenliggende GC-code", + "repair_chk_corrected_flag": "Caches gemarkeerd als gecorrigeerd zonder gecorrigeerde coördinaten", + "repair_chk_companion_orphans": "Rijen in de begeleidende database waarvan de cache niet meer bestaat", + "repair_chk_coords": "Coördinaten ontbreken of buiten bereik", + "repair_chk_corrected_coords": "Gecorrigeerde coördinaten buiten bereik", + "repair_chk_ratings": "Moeilijkheid of terrein niet tussen 1 en 5 in stappen van 0,5", + "repair_chk_identity": "Lege GC-code of naam", + "repair_fix_delete": "Verwijderen", + "repair_fix_clear_link": "Koppeling verwijderen", + "repair_fix_set_parent": "Code van de cache instellen", + "repair_fix_clear_flag": "Markering verwijderen", + "repair_report_only": "Alleen melden", + "repair_btn_repair": "Repareren", + "repair_btn_rescan": "Opnieuw controleren", + "repair_step_scan": "Gegevens controleren…", + "repair_step_fix": "Aangevinkte problemen herstellen…", + "repair_scan_clean": "Geen problemen gevonden.", + "repair_scan_summary": "{problems} controle(s) vonden problemen, {fixable} daarvan kunnen hier hersteld worden.", + "repair_damaged": "Het databasebestand zelf is beschadigd en SQLite kan het niet repareren.", + "repair_fixed": "Hersteld {count}: {check}", + "repair_nothing_fixed": "Er is niets gewijzigd.", + "repair_failed": "Repareren mislukt, er is niets gewijzigd:\n{error}", "maint_cleanup_done": "{file}: {orphans} overgebleven rij(en) en {registry} registervermeldingen verwijderd.", "maint_cleanup_nothing": "{file}: niets op te ruimen.", "maint_clear_confirm": "Alle {rows} rijen van de tabel {table} verwijderen?\nEigenaar: {owner}\n\nDe tabel blijft. De rijen zijn alleen terug te halen uit een back-up.", diff --git a/src/opensak/lang/pl.py b/src/opensak/lang/pl.py index d797e5a5..dba80515 100644 --- a/src/opensak/lang/pl.py +++ b/src/opensak/lang/pl.py @@ -1108,6 +1108,38 @@ "maint_compact_total": "Razem: {before} → {after}.", "maint_backup_failed": "Nic nie zostało skompaktowane:\n{error}", "maint_backup_done": "Utworzono kopię zapasową: {path}", + "maint_btn_repair": "Napraw…", + "maint_tip_repair": "Sprawdza dane (wiersze bez skrzynki, zerwane powiązania, nieprawidłowe wartości) i naprawia to, co można bezpiecznie naprawić.", + "repair_title": "Naprawa — {name}", + "repair_intro": "Sprawdza dane bazy. Zaznaczone problemy zostaną naprawione. Problemy oznaczone \"Tylko zgłoś\" nie mają bezpiecznej automatycznej naprawy: sprawdź te skrzynki samodzielnie.", + "repair_col_check": "Sprawdzenie", + "repair_col_count": "Problemy", + "repair_col_fix": "Naprawa", + "repair_col_examples": "Przykłady", + "repair_chk_orphans": "Wiersze w {table}, których skrzynka już nie istnieje", + "repair_chk_parent_link": "Własne waypointy powiązane ze skrzynką, której nie ma w bazie", + "repair_chk_waypoint_parent": "Waypointy z błędnym nadrzędnym kodem GC", + "repair_chk_corrected_flag": "Skrzynki oznaczone jako skorygowane bez skorygowanych współrzędnych", + "repair_chk_companion_orphans": "Wiersze w bazie towarzyszącej, których skrzynka już nie istnieje", + "repair_chk_coords": "Brak współrzędnych lub poza zakresem", + "repair_chk_corrected_coords": "Skorygowane współrzędne poza zakresem", + "repair_chk_ratings": "Trudność lub teren nie od 1 do 5 w krokach co 0,5", + "repair_chk_identity": "Pusty kod GC lub nazwa", + "repair_fix_delete": "Usuń", + "repair_fix_clear_link": "Usuń powiązanie", + "repair_fix_set_parent": "Ustaw kod skrzynki", + "repair_fix_clear_flag": "Usuń oznaczenie", + "repair_report_only": "Tylko zgłoś", + "repair_btn_repair": "Napraw", + "repair_btn_rescan": "Sprawdź ponownie", + "repair_step_scan": "Sprawdzanie danych…", + "repair_step_fix": "Naprawianie zaznaczonych problemów…", + "repair_scan_clean": "Nie znaleziono problemów.", + "repair_scan_summary": "Sprawdzenia z problemami: {problems}, w tym do naprawy tutaj: {fixable}.", + "repair_damaged": "Sam plik bazy jest uszkodzony i SQLite nie może go naprawić.", + "repair_fixed": "Naprawiono {count}: {check}", + "repair_nothing_fixed": "Nic nie zostało zmienione.", + "repair_failed": "Naprawa nie powiodła się, nic nie zostało zmienione:\n{error}", "maint_cleanup_done": "{file}: usunięto pozostałe wiersze ({orphans}) i wpisy rejestru ({registry}).", "maint_cleanup_nothing": "{file}: nie ma czego czyścić.", "maint_clear_confirm": "Usunąć wszystkie wiersze ({rows}) tabeli {table}?\nWłaściciel: {owner}\n\nTabela zostaje. Wiersze można odzyskać tylko z kopii zapasowej.", diff --git a/src/opensak/lang/pt.py b/src/opensak/lang/pt.py index 83337881..b1ef5ff5 100644 --- a/src/opensak/lang/pt.py +++ b/src/opensak/lang/pt.py @@ -1107,6 +1107,38 @@ "maint_compact_total": "Total: {before} → {after}.", "maint_backup_failed": "Nada foi compactado:\n{error}", "maint_backup_done": "Cópia de segurança feita: {path}", + "maint_btn_repair": "Reparar…", + "maint_tip_repair": "Verifica os dados (linhas sem cache, ligações quebradas, valores inválidos) e corrige o que pode ser corrigido com segurança.", + "repair_title": "Reparar — {name}", + "repair_intro": "Verifica os dados da base de dados. Os problemas marcados são corrigidos. Os problemas com \"Só reportar\" não têm uma correção automática segura: verifique essas caches.", + "repair_col_check": "Verificação", + "repair_col_count": "Problemas", + "repair_col_fix": "Correção", + "repair_col_examples": "Exemplos", + "repair_chk_orphans": "Linhas em {table} cuja cache já não existe", + "repair_chk_parent_link": "Waypoints personalizados ligados a uma cache que não está na base de dados", + "repair_chk_waypoint_parent": "Waypoints com o código GC principal errado", + "repair_chk_corrected_flag": "Caches marcadas como corrigidas sem coordenadas corrigidas", + "repair_chk_companion_orphans": "Linhas na base de dados complementar cuja cache já não existe", + "repair_chk_coords": "Coordenadas em falta ou fora do intervalo", + "repair_chk_corrected_coords": "Coordenadas corrigidas fora do intervalo", + "repair_chk_ratings": "Dificuldade ou terreno fora de 1 a 5 em passos de 0,5", + "repair_chk_identity": "Código GC ou nome vazio", + "repair_fix_delete": "Eliminar", + "repair_fix_clear_link": "Remover a ligação", + "repair_fix_set_parent": "Definir o código da cache", + "repair_fix_clear_flag": "Remover a marca", + "repair_report_only": "Só reportar", + "repair_btn_repair": "Reparar", + "repair_btn_rescan": "Verificar de novo", + "repair_step_scan": "A verificar os dados…", + "repair_step_fix": "A corrigir os problemas marcados…", + "repair_scan_clean": "Nenhum problema encontrado.", + "repair_scan_summary": "{problems} verificação(ões) encontraram problemas, {fixable} podem ser corrigidas aqui.", + "repair_damaged": "O próprio ficheiro da base de dados está danificado e o SQLite não o consegue reparar.", + "repair_fixed": "Corrigido {count}: {check}", + "repair_nothing_fixed": "Nada foi alterado.", + "repair_failed": "A reparação falhou, nada foi alterado:\n{error}", "maint_cleanup_done": "{file}: removidas {orphans} linha(s) que sobravam e {registry} entradas do registo.", "maint_cleanup_nothing": "{file}: nada para limpar.", "maint_clear_confirm": "Apagar as {rows} linhas da tabela {table}?\nDono: {owner}\n\nA tabela fica. As linhas só podem ser recuperadas a partir de uma cópia de segurança.", diff --git a/src/opensak/lang/se.py b/src/opensak/lang/se.py index 63247692..9a76f2d8 100644 --- a/src/opensak/lang/se.py +++ b/src/opensak/lang/se.py @@ -1107,6 +1107,38 @@ "maint_compact_total": "Totalt: {before} → {after}.", "maint_backup_failed": "Inget komprimerades:\n{error}", "maint_backup_done": "Säkerhetskopia gjord: {path}", + "maint_btn_repair": "Reparera…", + "maint_tip_repair": "Kontrollerar data (rader utan cache, trasiga länkar, ogiltiga värden) och rättar det som kan rättas säkert.", + "repair_title": "Reparera — {name}", + "repair_intro": "Kontrollerar databasens data. De markerade problemen rättas. Problem märkta \"Endast rapport\" har ingen säker automatisk rättning: kontrollera de cacherna själv.", + "repair_col_check": "Kontroll", + "repair_col_count": "Problem", + "repair_col_fix": "Rättning", + "repair_col_examples": "Exempel", + "repair_chk_orphans": "Rader i {table} vars cache inte längre finns", + "repair_chk_parent_link": "Egna waypoints länkade till en cache som inte finns i databasen", + "repair_chk_waypoint_parent": "Waypoints med fel överordnad GC-kod", + "repair_chk_corrected_flag": "Cacher markerade som korrigerade utan korrigerade koordinater", + "repair_chk_companion_orphans": "Rader i följedatabasen vars cache inte längre finns", + "repair_chk_coords": "Koordinater saknas eller utanför giltigt område", + "repair_chk_corrected_coords": "Korrigerade koordinater utanför giltigt område", + "repair_chk_ratings": "Svårighet eller terräng inte mellan 1 och 5 i steg om 0,5", + "repair_chk_identity": "Tom GC-kod eller namn", + "repair_fix_delete": "Ta bort", + "repair_fix_clear_link": "Ta bort länken", + "repair_fix_set_parent": "Sätt cachens kod", + "repair_fix_clear_flag": "Ta bort markeringen", + "repair_report_only": "Endast rapport", + "repair_btn_repair": "Reparera", + "repair_btn_rescan": "Kontrollera igen", + "repair_step_scan": "Kontrollerar data…", + "repair_step_fix": "Rättar de markerade problemen…", + "repair_scan_clean": "Inga problem hittades.", + "repair_scan_summary": "{problems} kontroll(er) hittade problem, {fixable} av dem kan rättas här.", + "repair_damaged": "Själva databasfilen är skadad och SQLite kan inte reparera den.", + "repair_fixed": "Rättat {count}: {check}", + "repair_nothing_fixed": "Inget ändrades.", + "repair_failed": "Reparationen misslyckades, inget ändrades:\n{error}", "maint_cleanup_done": "{file}: tog bort {orphans} överbliven(a) rad(er) och {registry} registerposter.", "maint_cleanup_nothing": "{file}: inget att städa upp.", "maint_clear_confirm": "Radera alla {rows} rader i tabellen {table}?\nÄgare: {owner}\n\nTabellen finns kvar. Raderna kan bara hämtas tillbaka från en säkerhetskopia.", diff --git a/tests/unit-tests/test_db_repair_1051.py b/tests/unit-tests/test_db_repair_1051.py new file mode 100644 index 00000000..639b1f91 --- /dev/null +++ b/tests/unit-tests/test_db_repair_1051.py @@ -0,0 +1,351 @@ +""" +tests/unit-tests/test_db_repair_1051.py — database repair, phase 1 (#1051): +the data checks of db/repair.py on a database with deliberately broken +rows, the fixes, a second run that finds nothing, and the Repair dialog +opened from the Maintenance dialog. +""" + +from __future__ import annotations + +import sqlite3 +from pathlib import Path +from types import SimpleNamespace +from unittest.mock import patch + +import pytest + +pytest.importorskip("pytestqt") + +from opensak.db import companion, maintenance, repair +from opensak.db.database import dispose_engine, get_session, init_db +from opensak.db.models import Cache, Log, UserNote, Waypoint +from opensak.gui.dialogs import database_maintenance_dialog as dmd +from opensak.gui.dialogs import database_repair_dialog as drd +from opensak.lang import load_language, tr +from opensak.utils import flags + +USER_DATE = "2025-01-02 03:04:05.000000" + + +@pytest.fixture(autouse=True) +def _fresh(monkeypatch): + load_language("en") + monkeypatch.setattr(flags, "companion_db", True) + companion.reset() + yield + dispose_engine() + companion.reset() + + +def _healthy(path: Path) -> Path: + """A database without problems: two caches, a custom waypoint linked to + GC1, a waypoint, a log and a correction to the listed coordinates.""" + path.parent.mkdir(parents=True, exist_ok=True) + init_db(db_path=path) + with get_session() as s: + gc1 = Cache(gc_code="GC1", name="One", cache_type="Traditional Cache", + latitude=47.0, longitude=8.0, difficulty=1.5, terrain=5.0) + gc2 = Cache(gc_code="GC2", name="Two", cache_type="Mystery Cache", + latitude=47.1, longitude=8.1, difficulty=2.0, terrain=2.0) + cw = Cache(gc_code="CW1", name="Parking", cache_type="Custom", + latitude=47.2, longitude=8.2, parent_gc_code="GC1") + s.add_all([gc1, gc2, cw]) + s.flush() + s.add(Waypoint(cache_id=gc1.id, parent_gc_code="GC1", prefix="PK", name="Parking")) + s.add(Log(cache_id=gc1.id, log_id="1", log_type="Found it", finder="me")) + # Corrected to the listed coordinates: a real choice, not a problem. + s.add(UserNote(cache_id=gc2.id, corrected_lat=47.1, corrected_lon=8.1, + is_corrected=True)) + dispose_engine(path) + return path + + +def _broken(path: Path) -> Path: + """_healthy() plus one problem of every kind.""" + _healthy(path) + conn = sqlite3.connect(str(path)) + try: + conn.execute("PRAGMA foreign_keys=OFF") + gc1 = conn.execute("SELECT id FROM caches WHERE gc_code = 'GC1'").fetchone()[0] + gc2 = conn.execute("SELECT id FROM caches WHERE gc_code = 'GC2'").fetchone()[0] + conn.executescript(f""" + INSERT INTO logs (cache_id, log_id, log_type) VALUES (9999, 'x1', 'Note'); + INSERT INTO logs (cache_id, log_id, log_type) VALUES (9999, 'x2', 'Note'); + INSERT INTO waypoints (cache_id, prefix, name) VALUES (9999, 'S1', 'Stage'); + INSERT INTO attributes (cache_id, attribute_id) VALUES (9998, 1); + INSERT INTO trackables (cache_id, name) VALUES (9998, 'TB'); + INSERT INTO user_notes (cache_id, note) VALUES (9997, 'gone'); + INSERT INTO caches (gc_code, name, cache_type, latitude, longitude, parent_gc_code) + VALUES ('CW2', 'Lost link', 'Custom', 47.3, 8.3, 'gc404'); + UPDATE waypoints SET parent_gc_code = 'GC2' WHERE cache_id = {gc1}; + UPDATE user_notes SET corrected_lat = NULL WHERE cache_id = {gc2}; + INSERT INTO caches (gc_code, name, cache_type, latitude, longitude, + difficulty, terrain) + VALUES ('GC3', 'Bad', 'Traditional Cache', 95.0, 8.0, 1.3, 6.0); + INSERT INTO caches (gc_code, name, cache_type, latitude, longitude) + VALUES ('GC4', ' ', 'Traditional Cache', 47.0, 8.0); + """) + conn.execute("UPDATE caches SET last_user_update = ? WHERE id = ?", (USER_DATE, gc2)) + conn.commit() + finally: + conn.close() + return path + + +def _counts(result: repair.ScanResult) -> dict[str, int]: + return {f.key: f.count for f in result.problems} + + +class TestScan: + def test_healthy_database_has_no_problems(self, tmp_path): + result = repair.scan(_healthy(tmp_path / "Ok.db"), with_companion=True) + assert not result.damaged + assert result.problems == () + + def test_finds_every_problem(self, tmp_path): + result = repair.scan(_broken(tmp_path / "Broken.db")) + assert _counts(result) == { + "orphans:logs": 2, "orphans:waypoints": 1, "orphans:attributes": 1, + "orphans:trackables": 1, "orphans:user_notes": 1, + repair.PARENT_LINK: 1, repair.WAYPOINT_PARENT: 1, repair.CORRECTED_FLAG: 1, + repair.COORDS: 1, repair.RATINGS: 1, repair.IDENTITY: 1, + } + by_key = {f.key: f for f in result.findings} + assert by_key[repair.PARENT_LINK].examples == ("CW2",) + assert by_key[repair.WAYPOINT_PARENT].examples == ("GC1",) + assert by_key[repair.CORRECTED_FLAG].examples == ("GC2",) + assert by_key[repair.RATINGS].examples == ("GC3",) + assert by_key[repair.IDENTITY].examples == ("GC4",) + assert not by_key[repair.COORDS].fixable + assert {f.key for f in result.fixable} == { + "orphans:logs", "orphans:waypoints", "orphans:attributes", "orphans:trackables", + "orphans:user_notes", repair.PARENT_LINK, repair.WAYPOINT_PARENT, + repair.CORRECTED_FLAG, + } + + def test_ratings_in_half_steps_are_fine(self, tmp_path): + db = _healthy(tmp_path / "Ok.db") + conn = sqlite3.connect(str(db)) + with conn: + conn.execute("UPDATE caches SET difficulty = 4.5, terrain = 1.0 WHERE gc_code = 'GC1'") + conn.close() + assert repair.RATINGS not in _counts(repair.scan(db)) + + def test_corrected_coordinates_out_of_range(self, tmp_path): + db = _healthy(tmp_path / "Ok.db") + conn = sqlite3.connect(str(db)) + with conn: + conn.execute("UPDATE user_notes SET corrected_lon = 200") + conn.close() + assert _counts(repair.scan(db)) == {repair.CORRECTED_COORDS: 1} + + def test_examples_are_limited(self, tmp_path): + db = _healthy(tmp_path / "Ok.db") + conn = sqlite3.connect(str(db)) + with conn: + for n in range(repair.MAX_EXAMPLES + 3): + conn.execute("INSERT INTO caches (gc_code, name, cache_type, latitude, longitude, " + f"difficulty) VALUES ('GCX{n}', 'x', 'T', 1, 1, 7)") + conn.close() + finding = next(f for f in repair.scan(db).findings if f.check == repair.RATINGS) + assert finding.count == repair.MAX_EXAMPLES + 3 + assert len(finding.examples) == repair.MAX_EXAMPLES + + def test_damaged_file_skips_the_data_checks(self, tmp_path): + db = _healthy(tmp_path / "Ok.db") + with patch.object(repair, "_connect") as connect: + connect.return_value.execute.return_value.fetchall.return_value = [ + ("*** in database main ***",)] + result = repair.scan(db) + assert result.damaged == ("*** in database main ***",) + assert result.findings == () + + def test_companion_orphans(self, tmp_path): + db = _healthy(tmp_path / "Ok.db") + companion.create(db) + conn = sqlite3.connect(str(companion.companion_path(db))) + with conn: + conn.execute("CREATE TABLE notes (gc_code TEXT PRIMARY KEY, note TEXT)") + conn.execute("INSERT INTO notes VALUES ('GC1', 'kept'), ('GC404', 'orphan')") + conn.close() + result = repair.scan(db, with_companion=True) + assert _counts(result) == {repair.COMPANION_ORPHANS: 1} + assert repair.repair(db, {repair.COMPANION_ORPHANS}).fixed == {repair.COMPANION_ORPHANS: 1} + assert repair.scan(db, with_companion=True).problems == () + + +class TestRepair: + def test_fixes_everything_and_a_second_run_finds_nothing_fixable(self, tmp_path): + db = _broken(tmp_path / "Broken.db") + keys = {f.key for f in repair.scan(db).fixable} + result = repair.repair(db, keys) + assert result.fixed == { + "orphans:logs": 2, "orphans:waypoints": 1, "orphans:attributes": 1, + "orphans:trackables": 1, "orphans:user_notes": 1, + repair.PARENT_LINK: 1, repair.WAYPOINT_PARENT: 1, repair.CORRECTED_FLAG: 1, + } + again = repair.scan(db) + assert again.fixable == () + # Report-only problems are left for the user. + assert set(_counts(again)) == {repair.COORDS, repair.RATINGS, repair.IDENTITY} + assert maintenance.check_file(db).ok + assert repair.repair(db, keys).fixed == {} + + def test_keeps_the_good_rows(self, tmp_path): + db = _broken(tmp_path / "Broken.db") + repair.repair(db, {f.key for f in repair.scan(db).fixable}) + init_db(db_path=db) + with get_session() as s: + gc1 = s.query(Cache).filter_by(gc_code="GC1").one() + assert [lg.log_id for lg in gc1.logs] == ["1"] + assert [w.parent_gc_code for w in gc1.waypoints] == ["GC1"] + assert s.query(Cache).filter_by(gc_code="CW1").one().parent_gc_code == "GC1" + assert s.query(Cache).filter_by(gc_code="CW2").one().parent_gc_code is None + gc2 = s.query(Cache).filter_by(gc_code="GC2").one() + assert gc2.user_note is not None and not gc2.user_note.is_corrected + assert gc2.user_note.corrected_source is None + + def test_clearing_the_flag_keeps_last_user_update(self, tmp_path): + db = _broken(tmp_path / "Broken.db") + repair.repair(db, {repair.CORRECTED_FLAG}) + conn = sqlite3.connect(str(db)) + stamp = conn.execute( + "SELECT last_user_update FROM caches WHERE gc_code = 'GC2'").fetchone()[0] + conn.close() + assert stamp == USER_DATE + + def test_only_the_chosen_fixes(self, tmp_path): + db = _broken(tmp_path / "Broken.db") + result = repair.repair(db, {"orphans:logs", repair.COORDS}) + assert result.fixed == {"orphans:logs": 2} + counts = _counts(repair.scan(db)) + assert "orphans:logs" not in counts + assert counts["orphans:waypoints"] == 1 + assert counts[repair.COORDS] == 1 + + def test_a_failure_changes_nothing(self, tmp_path): + db = _broken(tmp_path / "Broken.db") + with patch.object(repair, "_clear_corrected_flags", side_effect=sqlite3.OperationalError): + with pytest.raises(sqlite3.OperationalError): + repair.repair(db, {"orphans:logs", repair.CORRECTED_FLAG}) + assert _counts(repair.scan(db))["orphans:logs"] == 2 + + +# ── The dialog's work and text ──────────────────────────────────────────────── + +class TestRunRepair: + def test_backup_fix_and_summary(self, tmp_path): + db = _broken(tmp_path / "Broken.db") + keys = {f.key for f in repair.scan(db).fixable} + made = SimpleNamespace(backup_set=SimpleNamespace(path=tmp_path / "set")) + with patch("opensak.backup.backupset.write_backup_set", return_value=made) as write: + outcome = drd.run_repair(SimpleNamespace(name="Broken", path=db), keys, True, + lambda _s: None) + write.assert_called_once() + lines = drd.repair_lines(outcome) + assert lines[0] == tr("maint_backup_done", path=str(tmp_path / "set")) + assert tr("repair_fixed", count=2, check=tr("repair_chk_orphans", table="logs")) in lines + assert outcome.compact is None or outcome.compact.status in ( + maintenance.DONE, maintenance.NOT_WORTHWHILE) + + def test_nothing_to_fix(self, tmp_path): + db = _healthy(tmp_path / "Ok.db") + outcome = drd.run_repair(SimpleNamespace(name="Ok", path=db), set(), False, + lambda _s: None) + assert drd.repair_lines(outcome)[0] == tr("repair_nothing_fixed") + + def test_compacts_when_a_quarter_is_free(self, tmp_path): + db = _healthy(tmp_path / "Ok.db") + conn = sqlite3.connect(str(db)) + with conn: + conn.execute("CREATE TABLE filler (x BLOB)") + for _ in range(60): + conn.execute("INSERT INTO filler VALUES (zeroblob(1024 * 1024))") + with conn: + conn.execute("DROP TABLE filler") + conn.close() + with patch.object(maintenance, "MANUAL_MIN_RECLAIM_BYTES", 0): + outcome = drd.run_repair(SimpleNamespace(name="Ok", path=db), set(), False, + lambda _s: None) + assert outcome.compact is not None and outcome.compact.ran + assert maintenance.file_info(db).stats.free_bytes == 0 + + +# ── The dialog ──────────────────────────────────────────────────────────────── + +def _dialog(qtbot, db: Path) -> drd.DatabaseRepairDialog: + dlg = drd.DatabaseRepairDialog(SimpleNamespace(name=db.stem, path=db)) + qtbot.addWidget(dlg) + qtbot.waitUntil(lambda: not dlg.is_running() and dlg.scan_result is not None, timeout=10000) + return dlg + + +def _rows(dlg) -> dict[str, object]: + out = {} + for i in range(dlg._tree.topLevelItemCount()): + item = dlg._tree.topLevelItem(i) + assert item is not None + out[item.data(0, drd.KEY_ROLE)] = item + return out + + +class TestDialog: + def test_lists_the_problems_and_ticks_the_fixable_ones(self, qtbot, tmp_path): + dlg = _dialog(qtbot, _broken(tmp_path / "Broken.db")) + rows = _rows(dlg) + assert rows["orphans:logs"].checkState(0) == drd.Qt.CheckState.Checked + report = rows[repair.COORDS] + assert not report.flags() & drd.Qt.ItemFlag.ItemIsUserCheckable + assert report.text(2) == tr("repair_report_only") + assert "GC3" in rows[repair.COORDS].text(3) + assert dlg._backup.isChecked() + assert dlg._btn_repair.isEnabled() + assert dlg.checked_keys() == {f.key for f in dlg.scan_result.fixable} + + def test_unticking_everything_disables_repair(self, qtbot, tmp_path): + dlg = _dialog(qtbot, _broken(tmp_path / "Broken.db")) + for item in _rows(dlg).values(): + if item.flags() & drd.Qt.ItemFlag.ItemIsUserCheckable: + item.setCheckState(0, drd.Qt.CheckState.Unchecked) + assert not dlg._btn_repair.isEnabled() + + def test_healthy_database(self, qtbot, tmp_path): + dlg = _dialog(qtbot, _healthy(tmp_path / "Ok.db")) + assert dlg._tree.topLevelItemCount() == 0 + assert dlg._summary.text() == tr("repair_scan_clean") + assert not dlg._btn_repair.isEnabled() + + def test_repair_then_scans_again(self, qtbot, tmp_path): + db = _broken(tmp_path / "Broken.db") + init_db(db_path=db) # the active database + dlg = _dialog(qtbot, db) + dlg._backup.setChecked(False) + dlg._repair() + qtbot.waitUntil(lambda: not dlg.is_running() and dlg.scan_result is not None + and not dlg.scan_result.fixable, timeout=20000) + assert tr("repair_fixed", count=2, check=tr("repair_chk_orphans", table="logs")) \ + in dlg._results.toPlainText() + assert set(_rows(dlg)) == {repair.COORDS, repair.RATINGS, repair.IDENTITY} + with get_session() as s: # the active database is still usable + assert s.query(Log).count() == 1 + + def test_damaged_file_points_to_restore(self, qtbot, tmp_path): + db = _healthy(tmp_path / "Ok.db") + damaged = repair.ScanResult(db, damaged=("page 3 is never used",)) + with patch.object(repair, "scan", return_value=damaged): + dlg = _dialog(qtbot, db) + assert dlg._summary.text().startswith(tr("repair_damaged")) + assert tr("maint_check_main_failed") in dlg._summary.text() + assert "page 3 is never used" in dlg._results.toPlainText() + assert not dlg._btn_repair.isEnabled() + + def test_maintenance_dialog_opens_repair(self, qtbot, tmp_path): + db = _healthy(tmp_path / "Ok.db") + dlg = dmd.DatabaseMaintenanceDialog(SimpleNamespace(name="Ok", path=db)) + qtbot.addWidget(dlg) + qtbot.waitUntil(lambda: not dlg.is_running() and dlg._info.main is not None, + timeout=10000) + with patch.object(drd.DatabaseRepairDialog, "exec", return_value=0) as run: + dlg._btn_repair.click() + run.assert_called_once() + qtbot.waitUntil(lambda: not dlg.is_running(), timeout=10000)