-
Notifications
You must be signed in to change notification settings - Fork 301
fix(bible): 修复角色关系指数增长导致数据库爆炸的问题 #160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -315,6 +315,51 @@ def exists(self, bible_id: str) -> bool: | |
| r = self.db.fetch_one("SELECT 1 AS o FROM bibles WHERE id = ?", (bible_id,)) | ||
| return r is not None | ||
|
|
||
| def add_character_incremental( | ||
| self, | ||
| novel_id: str, | ||
| character_id: str, | ||
| name: str, | ||
| description: str, | ||
| relationships: List[Dict[str, str]], | ||
| *, | ||
| mental_state: str = "NORMAL", | ||
| verbal_tic: str = "", | ||
| idle_behavior: str = "", | ||
| ) -> None: | ||
| """增量添加单个角色及其关系(不触碰其他角色)。""" | ||
| now = self._now() | ||
| conn = self._conn() | ||
| try: | ||
| conn.execute( | ||
| """ | ||
| INSERT OR REPLACE INTO bible_characters ( | ||
| id, novel_id, name, description, | ||
| mental_state, mental_state_reason, verbal_tic, idle_behavior, | ||
| created_at, updated_at | ||
| ) VALUES (?, ?, ?, ?, ?, '', ?, ?, ?, ?) | ||
| """, | ||
| (character_id, novel_id, name, description, | ||
| mental_state, verbal_tic, idle_behavior, now, now), | ||
| ) | ||
| for i, rel in enumerate(relationships): | ||
| rid = f"{character_id}-rel-{i}-{uuid.uuid4().hex[:6]}" | ||
| target_name = rel.get("target", "") or "" | ||
| relation = rel.get("relation", "") or "" | ||
| desc = rel.get("description", "") or "" | ||
| conn.execute( | ||
| """ | ||
| INSERT INTO bible_character_relationships | ||
| (id, character_id, target_name, relation, description) | ||
| VALUES (?, ?, ?, ?, ?) | ||
| """, | ||
| (rid, character_id, target_name, relation, desc), | ||
| ) | ||
|
Comment on lines
+345
to
+357
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The fix is a targeted DELETE at the top of the relationship loop: 🛡️ Proposed fix+ conn.execute(
+ "DELETE FROM bible_character_relationships WHERE character_id = ?",
+ (character_id,),
+ )
for i, rel in enumerate(relationships):
rid = f"{character_id}-rel-{i}-{uuid.uuid4().hex[:6]}"This makes the method self-consistent regardless of whether the service-level duplicate guard fires first (e.g., TOCTOU, direct repo invocation). 🤖 Prompt for AI Agents |
||
| conn.commit() | ||
| except Exception: | ||
| conn.rollback() | ||
| raise | ||
|
|
||
| def update_character_anchors( | ||
| self, | ||
| novel_id: str, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential
NoneTypecrash:BibleDTO.from_domain(bible)is called without a null-check after the reload.get_by_novel_idreturnsOptional[Bible]. If the Bible is concurrently deleted betweenadd_character_incrementaland the reload on line 161,bibleisNoneandBibleDTO.from_domain(None)will raise anAttributeError.🐛 Proposed fix
🤖 Prompt for AI Agents