Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(scripts): 人工合并允许一个保留侧收走多个旧艺名

longmeidaoebb57df8bd68
diff --git a/scripts/merge_duplicate_identities.py b/scripts/merge_duplicate_identities.py
index e5c64cf6c..992281a0f 100644
--- a/scripts/merge_duplicate_identities.py
+++ b/scripts/merge_duplicate_identities.py
@@ -819,13 +819,15 @@ def named_pairs(connection: sqlite3.Connection, pairs: list[str],
写法 `保留id:丢弃id:证据`。方向不自动挑:作品数多的一侧不一定是名字对的一侧。
- 一条实体在整批 `--pair` 里只能出现一次。合并会删掉被丢弃的那一条,同一个 id 在
- 第二对里再被引用,就是合进(或合出)一条已经不存在的实体;一批十几对时这种重复
- 肉眼看不出来。
+ 保留侧允许在一批里重复出现,丢弃侧不允许:一个人有几个旧艺名就有几条要并进来,
+ `香椎美铃` 同时收走 `みすず` 和 `加山優衣` 是常态。被丢弃的那条会被删掉,所以它
+ 一旦出现过,后面无论摆在哪一侧都是引用一条已经不存在的实体——包括「先丢后留」这种
+ 顺序写反的情形,一批十几对时肉眼看不出来。
"""
connection.row_factory = sqlite3.Row
engaged = engaged or set()
- seen: set[int] = set()
+ kept: set[int] = set()
+ dropped: set[int] = set()
plan: list[dict[str, object]] = []
for pair in pairs:
parts = pair.split(":", 2)
@@ -834,8 +836,14 @@ def named_pairs(connection: sqlite3.Connection, pairs: list[str],
keep_id, drop_id, why = int(parts[0]), int(parts[1]), parts[2].strip()
if keep_id == drop_id:
raise SystemExit(f"--pair {pair}:两侧是同一条实体 {keep_id},不合并")
- if keep_id in seen or drop_id in seen:
- raise SystemExit(f"--pair {pair}:这条实体已经出现在前面的 --pair 里")
+ if keep_id in dropped:
+ raise SystemExit(
+ f"--pair {pair}:保留侧 {keep_id} 在前面的 --pair 里已经被丢弃")
+ if drop_id in dropped:
+ raise SystemExit(f"--pair {pair}:丢弃侧 {drop_id} 在前面的 --pair 里已经被丢弃")
+ if drop_id in kept:
+ raise SystemExit(
+ f"--pair {pair}:丢弃侧 {drop_id} 在前面的 --pair 里是保留侧")
sides = {}
for name, entity_id in (("keep", keep_id), ("drop", drop_id)):
row = connection.execute(
@@ -851,7 +859,8 @@ def named_pairs(connection: sqlite3.Connection, pairs: list[str],
sides[name] = row
if sides["keep"]["kind"] != sides["drop"]["kind"]:
raise SystemExit(f"--pair {pair}:两侧 kind 不同,不合并")
- seen.update((keep_id, drop_id))
+ kept.add(keep_id)
+ dropped.add(drop_id)
plan.append({
"normalized_name": sides["drop"]["normalized_name"],
"match_evidence": why,
diff --git a/tests/test_duplicate_identity_merge.py b/tests/test_duplicate_identity_merge.py
index 644d70d7d..b031fec67 100644
--- a/tests/test_duplicate_identity_merge.py
+++ b/tests/test_duplicate_identity_merge.py
@@ -481,7 +481,8 @@ class NamedPairTests(unittest.TestCase):
" VALUES(?,?,?,?,'t','t')",
[(1, "performer", "五十岚星兰", "五十岚星兰"),
(2, "performer", "五十嵐星蘭", "五十嵐星蘭"),
- (3, "studio", "别的种类", "别的种类")])
+ (3, "studio", "别的种类", "别的种类"),
+ (6, "performer", "加山優衣", "加山優衣")])
self.con.commit()
def tearDown(self):
@@ -521,10 +522,25 @@ class NamedPairTests(unittest.TestCase):
with self.assertRaises(SystemExit):
self.pairs("1:1:证据")
- def test_an_entity_named_twice_in_one_batch_is_refused(self):
- """第一对合完 id 就没了,第二对再引用它就是合进一条不存在的实体。"""
+ def test_one_survivor_can_take_in_several_old_stage_names(self):
+ """一个人有几个旧艺名就有几条要并进来,保留侧当然会重复出现。"""
+ rows = self.pairs("1:2:javdb 同一页", "1:6:javdb 同一页")
+ self.assertEqual([(row["keep_id"], row["drop_id"]) for row in rows],
+ [(1, 2), (1, 6)])
+
+ def test_an_entity_dropped_earlier_cannot_come_back_as_a_survivor(self):
+ """前一对已经把它删了,再拿它当保留侧就是往一条不存在的实体里搬东西。"""
+ with self.assertRaises(SystemExit):
+ self.pairs("1:2:证据", "2:6:另一段证据")
+
+ def test_an_entity_dropped_earlier_cannot_be_dropped_twice(self):
+ with self.assertRaises(SystemExit):
+ self.pairs("1:2:证据", "6:2:另一段证据")
+
+ def test_a_survivor_cannot_turn_into_a_drop_later(self):
+ """顺序写反:先说留它,后面又要丢它,两条不可能同时成立。"""
with self.assertRaises(SystemExit):
- self.pairs("1:2:证据", "1:2:另一段证据")
+ self.pairs("1:2:证据", "6:1:另一段证据")
def test_merging_series_rewrites_the_flat_column(self):
"""只搬关系不改 `asset.series`,卡片上还写着被丢弃的系列名。"""