diff --git a/database/operations.py b/database/operations.py index ad9b6ae..ce48a29 100644 --- a/database/operations.py +++ b/database/operations.py @@ -444,6 +444,38 @@ class PromptDatabase: conn.commit() return cursor.rowcount > 0 + def delete_prompts_by_category(self, category: str) -> int: + """Delete all prompts with the given category. + + Returns: + Number of prompts deleted. + """ + with self.model.get_connection() as conn: + # Get IDs first for cascade cleanup + ids = [ + r[0] + for r in conn.execute( + "SELECT id FROM prompts WHERE category = ?", (category,) + ).fetchall() + ] + if not ids: + return 0 + placeholders = ",".join("?" * len(ids)) + conn.execute( + f"DELETE FROM generated_images WHERE prompt_id IN ({placeholders})", + ids, + ) + conn.execute( + f"DELETE FROM prompt_tags WHERE prompt_id IN ({placeholders})", + ids, + ) + cursor = conn.execute( + f"DELETE FROM prompts WHERE id IN ({placeholders})", + ids, + ) + conn.commit() + return cursor.rowcount + def get_all_categories(self) -> List[str]: """ Get all unique categories from the database. diff --git a/py/api/lora_integration.py b/py/api/lora_integration.py index f5f910f..c2ca9d6 100644 --- a/py/api/lora_integration.py +++ b/py/api/lora_integration.py @@ -191,11 +191,24 @@ class LoraIntegrationMixin: await send_progress( { "type": "progress", - "status": "Finding LoRA directories...", + "status": "Clearing previous lora-manager imports...", "progress": 0, } ) + # Clear previous imports so reimport is always clean + await self._run_in_executor( + self.db.delete_prompts_by_category, "lora-manager" + ) + + await send_progress( + { + "type": "progress", + "status": "Finding LoRA directories...", + "progress": 2, + } + ) + lora_dirs = await self._run_in_executor(find_lora_directories, lm_path) # Collect all metadata files