From 3222a474c911f7524760dbb2928f75d8c4c51bf8 Mon Sep 17 00:00:00 2001 From: tegwick Date: Tue, 30 Sep 2025 22:55:13 +0200 Subject: [PATCH] fix: Correct database API usage in stats command - achieve 100% system health MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed the database connection error that was causing degraded system health by using the proper DatabaseManager API instead of non-existent methods. ## Root Cause Analysis: - **Issue**: `_show_core_system_stats()` tried to call `db_manager.get_connection()` - **Problem**: DatabaseManager class doesn't have a `get_connection()` method - **Impact**: System health reported as "Degraded (66.7%)" due to database unavailability ## Why No Tests Caught This: 1. **Existing tests** only test public API methods (`store_markdown_file`, `get_markdown_file`, etc.) 2. **No tests existed** for `get_connection()` because the method doesn't exist 3. **New stats function** was the first code to assume this method existed 4. **Database pattern**: Uses temporary connections per operation, not persistent connections ## Solution Applied: - **Replaced** `conn = db_manager.get_connection()` with proper `execute_query()` API - **Updated queries** to use named columns: `SELECT COUNT(*) as count FROM table` - **Added resilience** for optional tables (schema_files) with try/catch - **Result**: System health now reports ✅ **100% Healthy** ## Changes Made: ```python # Before (broken): conn = db_manager.get_connection() cursor.execute("SELECT COUNT(*) FROM markdown_files") total_files = cursor.fetchone()[0] # After (correct): result = db_manager.execute_query("SELECT COUNT(*) as count FROM markdown_files") total_files = result[0]['count'] if result else 0 ``` ## Current System Health: ``` 🏥 System Health: ✅ Healthy (100.0%) Healthy Subsystems: 3/3 🗄️ Database: ✅ Available (56.0 KB) - 11 files processed 🗃️ Cache: ✅ Available (0 B) 🖥️ AST Service: ✅ Available ``` This fix demonstrates the value of the health monitoring system - it successfully identified a real integration issue and provided clear diagnostic information. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- markitect/cli.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/markitect/cli.py b/markitect/cli.py index 3d5643f6..4f698e76 100644 --- a/markitect/cli.py +++ b/markitect/cli.py @@ -288,25 +288,26 @@ def _show_core_system_stats(config, format): db_size = 0 db_size_human = '0 B' - # Get file counts from database + # Get file counts from database using proper API try: - conn = db_manager.get_connection() - cursor = conn.cursor() - - # Total files - cursor.execute("SELECT COUNT(*) FROM markdown_files") - total_files = cursor.fetchone()[0] + # Use execute_query method for database queries + total_files_result = db_manager.execute_query("SELECT COUNT(*) as count FROM markdown_files") + total_files = total_files_result[0]['count'] if total_files_result else 0 # Recent files (last 7 days) - cursor.execute(""" - SELECT COUNT(*) FROM markdown_files + recent_files_result = db_manager.execute_query(""" + SELECT COUNT(*) as count FROM markdown_files WHERE created_at >= datetime('now', '-7 days') """) - recent_files = cursor.fetchone()[0] + recent_files = recent_files_result[0]['count'] if recent_files_result else 0 - # Schema count - cursor.execute("SELECT COUNT(*) FROM schema_files") - schema_count = cursor.fetchone()[0] + # Schema count (table might not exist) + try: + schema_count_result = db_manager.execute_query("SELECT COUNT(*) as count FROM schema_files") + schema_count = schema_count_result[0]['count'] if schema_count_result else 0 + except Exception: + # schema_files table doesn't exist - this is okay + schema_count = 0 stats['database'] = { 'path': db_path,