Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/opensak/db/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 <br> in trackable names (#1331) ─────────────
logger.debug("[migrations] entering migration 29 (+%.2fs)", time.monotonic() - _mig_t0)
# The GSAK import kept the "<BR>" 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
# <BR>, <br/> 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 '<br%'"
)).fetchall()
]
cleaned_29 = [r for r in cleaned_29 if r["name"]]
if cleaned_29:
conn.execute(
text("UPDATE trackables SET name = :name WHERE id = :id"), cleaned_29
)
conn.commit()
logger.info(f"Migration: removed <br> 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.
Expand Down
13 changes: 12 additions & 1 deletion src/opensak/importer/gsak_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,17 @@ def _looks_like_html(text: str) -> bool:
r"^(?P<name>.*?)\s*\(id\s*=\s*(?P<id>\d+)\s*,\s*ref\s*=\s*(?P<ref>[A-Za-z0-9]+)\)\s*$"
)

# Issue #1331: GSAK starts every TravelBugs line with an HTML line break
# (``<BR>Best TB ever (id = ...)``). It is formatting, not part of the name.
# Strip any number of leading ``<br>`` / ``<br/>`` / ``<br />``, any case.
_LEADING_BR_PATTERN = re.compile(r"^(?:<br\s*/?>\s*)+", re.IGNORECASE)


def strip_leading_br(text: str) -> str:
"""*text* without GSAK's leading ``<br>`` 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
Expand All @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions tests/unit-tests/test_gsak_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <BR> on every TravelBugs line ────────────────


def test_parse_gsak_trackables_strips_leading_br_1331():
# Real GSAK format: every line starts with "<BR>" and ends with CRLF.
raw = (
"<BR>And we're back in the car (id = 5836847, ref = TB6QPGA)\r\n"
"<BR>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", ["<br>", "<Br/>", "<br />", "<BR><BR> "])
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(
"<BR>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",
Expand Down
68 changes: 68 additions & 0 deletions tests/unit-tests/test_trackable_br_migration_1331.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# tests/unit-tests/test_trackable_br_migration_1331.py — migration 29 (#1331):
# strip GSAK's leading "<BR>" 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 <BR>, 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 (
"<BR>Hedgie 1",
"<br/><BR> Block Party Nano (null)",
"Clean name",
"Name with <BR> 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 <BR> 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 <BR> inside",
]
Loading