Peach: Local-first video metadata scraper and media manager.
fix(entities): 同一实体在一家来源只留一个外部 id,第二个丢掉而不是抛异常
longmeidao35e1f9fc6d0a
diff --git a/src/peach/entities.py b/src/peach/entities.pyindex a28163305..12109a386 100644--- a/src/peach/entities.py+++ b/src/peach/entities.py@@ -269,6 +269,25 @@ def merge_entity(return moved+def _ref_is_free(connection: Connection, entity_id: int, provider: str,+ external_kind: str, external_id: str) -> bool:+ """这个实体在这家来源下还没被别的 id 占着。++ 表上 `UNIQUE(entity_id, provider, external_kind)` 说的是「一个实体在一家来源只有一个+ id」,而插入语句的 `ON CONFLICT` 只认主键那一组,撞上这个索引就是一个未捕获的+ `IntegrityError`,把整笔事务连同同批次其它字段一起带走。++ 撞得上是因为艺名:实体 8004「桥本有菜」名下挂着「橋本ありな」「新ありな」等 8 个+ 别名,r18dev 给前者的 id 是 1032668、给后者是 1078619,账本里已经存着后者。人是一个,+ 来源那边是两个页面。先到的那条留着——它才是账本里其它引用和历史指向的那一个。+ """+ held = connection.execute(+ "SELECT external_id FROM entity_external_ref "+ "WHERE entity_id=? AND provider=? AND external_kind=?",+ (entity_id, provider, external_kind)).fetchone()+ return held is None or str(held[0]) == external_id++def upsert_asset_entity(connection: Connection, *, kind: str, name: str | None, asset_id: int,role: str, source: str, confidence: float = 1.0,@@ -326,7 +345,8 @@ def upsert_asset_entity(last_seen_at=excluded.last_seen_at""",(asset_id, entity_id, role, source, confidence, payload, stamp, stamp),)- if external_provider and external_id is not None:+ if external_provider and external_id is not None and _ref_is_free(+ connection, entity_id, external_provider, kind, str(external_id)):connection.execute("""INSERT INTO entity_external_ref(entity_id,provider,external_kind,external_id,metadata_json,last_synced_at)diff --git a/tests/test_entity_merge.py b/tests/test_entity_merge.pyindex 1152b8357..3776e869a 100644--- a/tests/test_entity_merge.py+++ b/tests/test_entity_merge.py@@ -95,6 +95,38 @@ class EntityMergeTests(unittest.TestCase):self.assertEqual(self.con.execute("PRAGMA foreign_key_check").fetchall(), [])+ def test_a_second_id_from_the_same_source_is_dropped_not_raised(self):+ """合并过的人在来源那边还是两个页面,第二个 id 只能丢掉。++ 实体 8004「桥本有菜」名下挂着「橋本ありな」「新ありな」等 8 个别名,r18dev 给+ 前者 1032668、给后者 1078619,账本存的是后者。按别名认出同一个人之后再插第二个+ id,撞的是 `UNIQUE(entity_id,provider,external_kind)`——插入语句的 `ON CONFLICT`+ 只认主键那一组,于是一条落库把整批自动落库连同别的字段一起带走。+ """+ self.con.execute(+ "INSERT INTO entity_alias(entity_id,alias,normalized_alias,source,confidence) "+ "VALUES(10,'橋本ありな別名','橋本ありな別名','mapping',1.0)")+ self.con.execute(+ "INSERT INTO entity_external_ref(entity_id,provider,external_kind,external_id)"+ " VALUES(10,'r18dev','performer','1078619')")+ self.con.commit()+ entity_id = upsert_asset_entity(+ self.con, kind="performer", name="橋本ありな別名", asset_id=3,+ role="performer", source="javinizer:r18dev:performer",+ external_provider="r18dev", external_id="1032668")+ self.con.commit()+ self.assertEqual(entity_id, 10)+ self.assertEqual(+ self.con.execute(+ "SELECT external_id FROM entity_external_ref "+ "WHERE entity_id=10 AND provider='r18dev'").fetchall(),+ [("1078619",)], "先到的那条留着")+ self.assertEqual(+ self.con.execute(+ "SELECT count(*) FROM asset_entity WHERE asset_id=3 AND entity_id=10 "+ "AND source='javinizer:r18dev:performer'").fetchone()[0], 1,+ "引用写不进去不影响这条出演关系本身")+def test_person_upsert_collapses_repeated_name_and_reuses_unique_alias(self):self.con.execute("INSERT INTO entity_alias(entity_id,alias,normalized_alias,source,confidence) "