Skip to content
Python

Escape LIKE wildcards when re-keying board cursors

tyoon264e2b949e1278
diff --git a/coworker/teams/store.py b/coworker/teams/store.py
index 876347bf..b1de1a5e 100644
--- a/coworker/teams/store.py
+++ b/coworker/teams/store.py
@@ -999,8 +999,9 @@ class TeamStore:
# Cursor keys embed the space as a suffix ("feed:<actor>:<space>",
# "sub:<sub>:<space>") — rewrite the suffix, keep consumed positions.
cur_rows = self._conn.execute(
- "SELECT cursor_key FROM team_cursors WHERE cursor_key LIKE ?",
- ("%:" + old,),
+ "SELECT cursor_key FROM team_cursors"
+ " WHERE cursor_key LIKE ? ESCAPE '\\'",
+ ("%" + _like_escape(":" + old),),
).fetchall()
for crow in cur_rows:
new_key = crow["cursor_key"][: -len(old)] + new
@@ -1045,6 +1046,15 @@ class TeamStore:
)
+def _like_escape(text: str) -> str:
+ """Quote LIKE metacharacters so a literal string matches only itself. Space
+ keys are filesystem paths, where `_` (LIKE's single-character wildcard) is
+ ordinary — left raw it also matches a neighbouring space's rows."""
+ for char in ("\\", "%", "_"):
+ text = text.replace(char, "\\" + char)
+ return text
+
+
def _canonical(payload: dict[str, Any]) -> str:
return json.dumps(payload, sort_keys=True, separators=(",", ":"), default=str)
diff --git a/tests/test_projects.py b/tests/test_projects.py
index dc2b0e72..57efee97 100644
--- a/tests/test_projects.py
+++ b/tests/test_projects.py
@@ -143,6 +143,21 @@ class TestMigration:
assert store.rekey_space("/old", "/new") is False
assert store.event_count("/old") == 1 # dormant, untouched
+ def test_board_rekey_leaves_lookalike_space_cursors_alone(self, tmp_path):
+ """`_` is ordinary in a path but a LIKE wildcard: a neighbouring space
+ whose key differs only there must keep its consumed position."""
+ store = TeamStore(tmp_path / "b.db")
+ lead = Actor(id="lead", role=Role.LEAD)
+ old, other = "/w/my_app", "/w/myXapp"
+ store.create_item(old, lead, title="one", criteria="c")
+ store.create_item(other, lead, title="two", criteria="c")
+ store.consume_feed(old, "worker", 1)
+ store.consume_feed(other, "worker", 2)
+
+ assert store.rekey_space(old, "git:acme/app") is True
+ assert store._cursor("feed:worker:git:acme/app") == 1
+ assert store._cursor(f"feed:worker:{other}") == 2
+
class TestResolvers:
def test_binding_beats_derivation(self, tmp_path):