Skip to content

Add a Repair… action to the database Maintenance dialog - #1337

Open
nagisml wants to merge 1 commit into
OpenSAK-Org:betafrom
nagisml:feature/db-repair-1051
Open

nagisml wants to merge 1 commit into
OpenSAK-Org:betafrom
nagisml:feature/db-repair-1051

Conversation

@nagisml

@nagisml nagisml commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Add a Repair… action to the database Maintenance dialog

Part of #1051 (phase 1 of 3: check framework, rows without a cache, broken links, report-only checks).

What changed and why

The Maintenance dialog (#1048) checks the database file. This PR adds Repair…, which checks the data in it and fixes what can be fixed safely, like GSAK's Repair/Defrag.

The dialog:

  1. Checks the database in the background, without changing it. It lists every check that found something, with the number of problems, the fix, and a few example GC codes.
  2. Lets the user tick what to fix. Checks marked "Report only" can't be ticked. Make a backup first is ticked by default and uses the backup core from Backup core: write, list and rotate backup sets (part of #942) #952.
  3. Runs the ticked fixes in one transaction. It then optimizes the database, and compacts it when more than 25 % of the file is free, as GSAK does (with the existing disk-space check). A summary follows, and the check runs again.

If quick_check fails, the file itself is damaged and SQLite can't repair it, so the dialog points to File → Restore from backup… (#954) instead.

It works on the active database (its connections are released first, as for Compact) and on any other database in the list. The checks and fixes are in the new module db/repair.py, which has no GUI code.

Checks

Check Fix
Rows whose cache no longer exists (foreign_key_check: logs, waypoints, attributes, trackables, user notes) Delete
Custom waypoints whose parent_gc_code points to a cache that isn't in the database Clear the link
Waypoints whose parent_gc_code isn't their own cache's code Set it from the cache, as the importer and macros do
Corrected-coordinates flag without corrected coordinates (filters count the cache as corrected; the map and distances don't) Clear the flag. last_user_update is kept, because a repair is not a user edit (#1140 trigger).
Companion database rows whose cache is gone companion.cleanup()
Coordinates missing or out of range, corrected coordinates out of range, D/T outside 1–5 or not in 0.5 steps, empty GC code or name Report only

There is no duplicate check. The unique constraints on attributes, logs.log_id and waypoints exist in every database (in the models since v0.2.0; migrations 2 and 21), and the importers replace a cache's waypoints and match logs by id.

Tests

tests/unit-tests/test_db_repair_1051.py uses a fixture database with one broken row of every kind. It checks the scan, each fix, and that only the ticked fixes run. It checks that a failed fix changes nothing, and that a second run finds nothing fixable. It also checks the dialog, including repairing the active database.

Tested by hand on a real 1.3 GB database (152k caches, 732k logs).

The new texts are in all 11 language files. The translations are machine-made.

The Maintenance dialog (OpenSAK-Org#1048) only checks the database file. Repair checks
the data in it and fixes what can be fixed safely, like GSAK's
Repair/Defrag. This is phase 1 of OpenSAK-Org#1051: the check framework, rows whose
cache is missing, broken links, and checks that only report.

Repair… opens a dialog that checks the database in the background without
changing it. It lists every check that found something, with the number of
problems, the fix and a few example GC codes. The user ticks what to fix.
Checks marked "Report only" can't be ticked. "Make a backup first" is ticked
and uses the backup core (OpenSAK-Org#952). The ticked fixes run in one transaction.
Then the database is optimized, and compacted when more than a quarter of
the file is free, as GSAK does. A summary follows and the check runs again.
If quick_check fails, the file itself is damaged and SQLite can't repair
it, so the dialog points to File → Restore from backup… (OpenSAK-Org#954) instead.

Fixable checks:
- rows that point to a missing cache, found with foreign_key_check (logs,
  waypoints, attributes, trackables, user notes): deleted
- custom waypoints whose parent_gc_code points to a cache that isn't in the
  database: link cleared
- waypoints whose parent_gc_code isn't their own cache's code: set from the
  cache, as the importer and macros do
- the corrected flag set without corrected coordinates, so filters count the
  cache as corrected while the map and distances don't: flag cleared. The
  cache keeps its last_user_update (the OpenSAK-Org#1140 trigger would set it to now),
  because a repair is not a user edit.
- companion database rows whose cache is gone: companion.cleanup()

Report only: coordinates missing or out of range, corrected coordinates out
of range, difficulty or terrain outside 1–5 or not in 0.5 steps, an empty
GC code or name.

There is no duplicate check. The unique constraints on attributes, log_id
and waypoints exist in every database, and the importers don't fill their
NULL gaps twice.

The checks and fixes are in db/repair.py, which has no GUI code. The dialog
works on the active database (its connections are released first, as for
Compact) and on any other database in the list.

The new texts are in all language files; the translations are machine-made.

Part of OpenSAK-Org#1051

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant