From 1152255c844392e58a2fdf7f8a1d30d51f3769aa Mon Sep 17 00:00:00 2001 From: nagisml Date: Tue, 17 Nov 2026 21:54:16 +0100 Subject: [PATCH] Strip GSAK's leading
from trackable names GSAK starts every CacheMemo.TravelBugs line with "
", and the GSAK import kept it as part of the trackable name. The import now removes leading
tags, and migration 29 cleans names already imported. fixes #1331 --- src/opensak/db/database.py | 24 ++++++- src/opensak/importer/gsak_importer.py | 13 +++- tests/unit-tests/test_gsak_importer.py | 30 ++++++++ .../test_trackable_br_migration_1331.py | 68 +++++++++++++++++++ 4 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 tests/unit-tests/test_trackable_br_migration_1331.py diff --git a/src/opensak/db/database.py b/src/opensak/db/database.py index e3fdc764..94402ea4 100644 --- a/src/opensak/db/database.py +++ b/src/opensak/db/database.py @@ -127,7 +127,7 @@ def _register_sql_functions(dbapi_connection, _connection_record) -> None: # bumped to the highest migration number whenever a new migration is added # below — _run_migrations() skips the whole block when the database already # reports this version, so a stale constant means new migrations never run. -SCHEMA_VERSION = 28 +SCHEMA_VERSION = 29 # Steps of opening a database that can take a while, for set_progress_callback(). PROGRESS_MIGRATING = "migrating" @@ -1181,6 +1181,28 @@ def _run_migrations(engine: Engine) -> None: logger.info(f"Migration: set caches.is_owner on {changed_28} caches") conn.commit() + # ── Migration 29: GSAK's
in trackable names (#1331) ───────────── + logger.debug("[migrations] entering migration 29 (+%.2fs)", time.monotonic() - _mig_t0) + # The GSAK import kept the "
" GSAK puts in front of every + # TravelBugs line as part of the name. Strip it from names imported + # before the fix. LIKE is case-insensitive for ASCII, so this finds + #
,
etc.; a name without the tag is never touched, which + # also makes this safe to run again. + from opensak.importer.gsak_importer import strip_leading_br + cleaned_29 = [ + {"id": row_id, "name": strip_leading_br(name)} + for row_id, name in conn.execute(text( + "SELECT id, name FROM trackables WHERE name LIKE ' from {len(cleaned_29)} trackable names") + # ── Stamp the schema version so the next launch skips the probes ───── # PRAGMA does not accept bind parameters; SCHEMA_VERSION is a trusted # int constant, so inlining it is safe. diff --git a/src/opensak/importer/gsak_importer.py b/src/opensak/importer/gsak_importer.py index a282df14..0e51c0d3 100644 --- a/src/opensak/importer/gsak_importer.py +++ b/src/opensak/importer/gsak_importer.py @@ -246,6 +246,17 @@ def _looks_like_html(text: str) -> bool: r"^(?P.*?)\s*\(id\s*=\s*(?P\d+)\s*,\s*ref\s*=\s*(?P[A-Za-z0-9]+)\)\s*$" ) +# Issue #1331: GSAK starts every TravelBugs line with an HTML line break +# (``
Best TB ever (id = ...)``). It is formatting, not part of the name. +# Strip any number of leading ``
`` / ``
`` / ``
``, any case. +_LEADING_BR_PATTERN = re.compile(r"^(?:\s*)+", re.IGNORECASE) + + +def strip_leading_br(text: str) -> str: + """*text* without GSAK's leading ``
`` tags (#1331). Also used by + migration 29 to clean trackable names imported before the fix.""" + return _LEADING_BR_PATTERN.sub("", text) + def _parse_gsak_trackables(raw: Optional[str]) -> list[dict]: """Parse GSAK's ``CacheMemo.TravelBugs`` free-text field into a list of @@ -259,7 +270,7 @@ def _parse_gsak_trackables(raw: Optional[str]) -> list[dict]: return [] trackables: list[dict] = [] for line in raw.splitlines(): - line = line.strip() + line = strip_leading_br(line.strip()) if not line: continue m = _TRAVELBUG_LINE_PATTERN.match(line) diff --git a/tests/unit-tests/test_gsak_importer.py b/tests/unit-tests/test_gsak_importer.py index 2d08927c..a5d249d3 100644 --- a/tests/unit-tests/test_gsak_importer.py +++ b/tests/unit-tests/test_gsak_importer.py @@ -835,6 +835,36 @@ def test_parse_gsak_trackables_skips_unmatched_lines_without_crashing(): assert [p["ref"] for p in parsed] == ["TBAB12CD", "TB999X"] +# ── Issue #1331: GSAK's leading
on every TravelBugs line ──────────────── + + +def test_parse_gsak_trackables_strips_leading_br_1331(): + # Real GSAK format: every line starts with "
" and ends with CRLF. + raw = ( + "
And we're back in the car (id = 5836847, ref = TB6QPGA)\r\n" + "
Hedgie 1 (id = 6874668, ref = TB7WHED)\r\n" + ) + assert _parse_gsak_trackables(raw) == [ + {"name": "And we're back in the car", "ref": "TB6QPGA", "tracking_code": "5836847"}, + {"name": "Hedgie 1", "ref": "TB7WHED", "tracking_code": "6874668"}, + ] + + +@pytest.mark.parametrize("prefix", ["
", "
", "
", "

"]) +def test_parse_gsak_trackables_strips_br_variants_1331(prefix): + parsed = _parse_gsak_trackables(f"{prefix}Hedgie 1 (id = 6874668, ref = TB7WHED)") + assert parsed[0]["name"] == "Hedgie 1" + + +def test_parse_gsak_trackables_keeps_rest_of_name_1331(): + # Only the leading tag is GSAK formatting; the rest of the name is kept + # as GSAK stores it, including GSAK's own "(null)". + parsed = _parse_gsak_trackables( + "
Block Party Nano (null) (id = 9806985, ref = TBB1ZR9)\r\n" + ) + assert parsed[0]["name"] == "Block Party Nano (null)" + + def test_trackable_mapping_via_import(db_session, tmp_path): db = _make_gsak_db( tmp_path / "gsak.db3", diff --git a/tests/unit-tests/test_trackable_br_migration_1331.py b/tests/unit-tests/test_trackable_br_migration_1331.py new file mode 100644 index 00000000..09818e7f --- /dev/null +++ b/tests/unit-tests/test_trackable_br_migration_1331.py @@ -0,0 +1,68 @@ +# tests/unit-tests/test_trackable_br_migration_1331.py — migration 29 (#1331): +# strip GSAK's leading "
" from trackable names imported before the fix. + +from pathlib import Path + +from sqlalchemy import text + +from opensak.db.database import SCHEMA_VERSION, _run_migrations, get_engine, init_db + + +def _make_v28_db(path: Path) -> None: + """A database as a GSAK import before #1331 left it: trackable names + starting with
, user_version 28.""" + init_db(db_path=path) + with get_engine().connect() as c: + c.execute(text( + "INSERT INTO caches (gc_code, name, cache_type, latitude, longitude) " + "VALUES ('GC1TB', 'With bugs', 'Traditional Cache', 47.0, 8.0)" + )) + for name in ( + "
Hedgie 1", + "

Block Party Nano (null)", + "Clean name", + "Name with
inside", + ): + c.execute(text( + "INSERT INTO trackables (cache_id, name, ref) VALUES (1, :name, 'TB1')" + ), {"name": name}) + c.execute(text("PRAGMA user_version = 28")) + c.commit() + + +def _names() -> list[str]: + with get_engine().connect() as c: + return [r[0] for r in c.execute(text("SELECT name FROM trackables ORDER BY id"))] + + +def test_schema_version(): + assert SCHEMA_VERSION >= 29 + + +def test_strips_leading_br_from_existing_names(tmp_path): + _make_v28_db(tmp_path / "v28.db") + _run_migrations(get_engine()) + assert _names() == [ + "Hedgie 1", + "Block Party Nano (null)", + "Clean name", + "Name with
inside", + ] + with get_engine().connect() as c: + assert c.execute(text("PRAGMA user_version")).scalar() == SCHEMA_VERSION + + +def test_rerun_is_a_no_op(tmp_path): + _make_v28_db(tmp_path / "v28.db") + engine = get_engine() + _run_migrations(engine) + with engine.connect() as c: + c.execute(text("PRAGMA user_version = 28")) + c.commit() + _run_migrations(engine) + assert _names() == [ + "Hedgie 1", + "Block Party Nano (null)", + "Clean name", + "Name with
inside", + ]