diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 62888fa..1b69bc2 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,12 +1,12 @@ repos: - - repo: https://github.com/psf/black - rev: 25.1.0 - hooks: - - id: black - args: [--line-length=88, --target-version=py310] - - repo: local hooks: + - id: black + name: black + entry: black --line-length=88 --target-version=py310 + language: system + types: [python] + - id: flake8 name: flake8 entry: flake8 --select=E9,F63,F7,F82 --show-source --exclude=reference/,web/ diff --git a/database/models.py b/database/models.py index e026029..0a028d6 100644 --- a/database/models.py +++ b/database/models.py @@ -68,8 +68,7 @@ class PromptModel: - prompts table: Stores prompt text and metadata - generated_images table: Links generated images to their source prompts """ - conn.execute( - """ + conn.execute(""" CREATE TABLE IF NOT EXISTS prompts ( id INTEGER PRIMARY KEY AUTOINCREMENT, text TEXT NOT NULL, @@ -81,12 +80,10 @@ class PromptModel: notes TEXT, hash TEXT UNIQUE ) - """ - ) + """) # Create images table for gallery functionality - conn.execute( - """ + conn.execute(""" CREATE TABLE IF NOT EXISTS generated_images ( id INTEGER PRIMARY KEY AUTOINCREMENT, prompt_id INTEGER NOT NULL, @@ -103,20 +100,16 @@ class PromptModel: FOREIGN KEY (prompt_id) REFERENCES prompts(id) ON DELETE CASCADE, UNIQUE(prompt_id, filename) ) - """ - ) + """) # Create normalized tag tables (junction table pattern) - conn.execute( - """ + conn.execute(""" CREATE TABLE IF NOT EXISTS tags ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL UNIQUE ) - """ - ) - conn.execute( - """ + """) + conn.execute(""" CREATE TABLE IF NOT EXISTS prompt_tags ( prompt_id INTEGER NOT NULL, tag_id INTEGER NOT NULL, @@ -124,8 +117,7 @@ class PromptModel: FOREIGN KEY (prompt_id) REFERENCES prompts(id) ON DELETE CASCADE, FOREIGN KEY (tag_id) REFERENCES tags(id) ON DELETE CASCADE ) - """ - ) + """) # Add unique constraint to existing databases (migration) self._migrate_add_unique_constraint(conn) @@ -208,8 +200,7 @@ class PromptModel: self.logger.info("Migrating database: removing workflow_name column") # Create new table without workflow_name - conn.execute( - """ + conn.execute(""" CREATE TABLE prompts_new ( id INTEGER PRIMARY KEY AUTOINCREMENT, text TEXT NOT NULL, @@ -221,17 +212,14 @@ class PromptModel: notes TEXT, hash TEXT UNIQUE ) - """ - ) + """) # Copy data from old table to new table - conn.execute( - """ + conn.execute(""" INSERT INTO prompts_new (id, text, created_at, updated_at, category, tags, rating, notes, hash) SELECT id, text, created_at, updated_at, category, tags, rating, notes, hash FROM prompts - """ - ) + """) # Drop old table and rename new one conn.execute("DROP TABLE prompts") @@ -264,8 +252,7 @@ class PromptModel: ) # Create new table with correct types - conn.execute( - """ + conn.execute(""" CREATE TABLE generated_images_new ( id INTEGER PRIMARY KEY AUTOINCREMENT, prompt_id INTEGER NOT NULL, @@ -281,12 +268,10 @@ class PromptModel: parameters TEXT, FOREIGN KEY (prompt_id) REFERENCES prompts(id) ON DELETE CASCADE ) - """ - ) + """) # Copy data, converting prompt_id from TEXT to INTEGER - conn.execute( - """ + conn.execute(""" INSERT INTO generated_images_new (id, prompt_id, image_path, filename, generation_time, file_size, width, height, format, workflow_data, prompt_metadata, parameters) @@ -295,8 +280,7 @@ class PromptModel: FROM generated_images WHERE prompt_id != '' AND prompt_id IS NOT NULL AND CAST(prompt_id AS INTEGER) IN (SELECT id FROM prompts) - """ - ) + """) # Drop old table and rename new one conn.execute("DROP TABLE generated_images") @@ -345,15 +329,13 @@ class PromptModel: ) # First, remove duplicates keeping only the most recent (highest id) - conn.execute( - """ + conn.execute(""" DELETE FROM generated_images WHERE id NOT IN ( SELECT MAX(id) FROM generated_images GROUP BY prompt_id, filename ) - """ - ) + """) duplicates_removed = conn.total_changes if duplicates_removed > 0: @@ -362,8 +344,7 @@ class PromptModel: ) # Create new table with UNIQUE constraint - conn.execute( - """ + conn.execute(""" CREATE TABLE IF NOT EXISTS generated_images_new ( id INTEGER PRIMARY KEY AUTOINCREMENT, prompt_id INTEGER NOT NULL, @@ -380,20 +361,17 @@ class PromptModel: FOREIGN KEY (prompt_id) REFERENCES prompts(id) ON DELETE CASCADE, UNIQUE(prompt_id, filename) ) - """ - ) + """) # Copy data - conn.execute( - """ + conn.execute(""" INSERT INTO generated_images_new (id, prompt_id, image_path, filename, generation_time, file_size, width, height, format, workflow_data, prompt_metadata, parameters) SELECT id, prompt_id, image_path, filename, generation_time, file_size, width, height, format, workflow_data, prompt_metadata, parameters FROM generated_images - """ - ) + """) # Drop old table and rename new one conn.execute("DROP TABLE generated_images") diff --git a/database/operations.py b/database/operations.py index 079a305..2e04d9a 100644 --- a/database/operations.py +++ b/database/operations.py @@ -946,16 +946,14 @@ class PromptDatabase: # Find duplicates by text content (case-insensitive) # Note: Removed ORDER BY from GROUP_CONCAT for SQLite compatibility # We'll sort the IDs manually after fetching - cursor = conn.execute( - """ + cursor = conn.execute(""" SELECT LOWER(TRIM(text)) as normalized_text, COUNT(*) as count, GROUP_CONCAT(id) as ids, GROUP_CONCAT(created_at) as created_dates FROM prompts GROUP BY LOWER(TRIM(text)) HAVING COUNT(*) > 1 - """ - ) + """) duplicate_groups = cursor.fetchall() self.logger.debug( @@ -1020,16 +1018,14 @@ class PromptDatabase: # Find duplicates by text content (case-insensitive) # Note: Removed ORDER BY from GROUP_CONCAT for SQLite compatibility # We'll sort the IDs manually after fetching - cursor = conn.execute( - """ + cursor = conn.execute(""" SELECT LOWER(TRIM(text)) as normalized_text, COUNT(*) as count, GROUP_CONCAT(id) as ids, GROUP_CONCAT(created_at) as created_dates FROM prompts GROUP BY LOWER(TRIM(text)) HAVING COUNT(*) > 1 - """ - ) + """) duplicates = cursor.fetchall() self.logger.debug( @@ -1749,8 +1745,7 @@ class PromptDatabase: Returns number of prompts removed. """ with self.model.get_connection() as conn: - cursor = conn.execute( - """ + cursor = conn.execute(""" SELECT p.id FROM prompts p LEFT JOIN generated_images gi ON p.id = gi.prompt_id WHERE gi.prompt_id IS NULL @@ -1759,8 +1754,7 @@ class PromptDatabase: JOIN tags t ON pt.tag_id = t.id WHERE pt.prompt_id = p.id AND t.name = '__protected__' ) - """ - ) + """) orphaned = [row["id"] for row in cursor.fetchall()] if not orphaned: return 0 @@ -1776,26 +1770,22 @@ class PromptDatabase: issues: List[str] = [] with self.model.get_connection() as conn: # Check for orphaned prompt_tags entries - cursor = conn.execute( - """ + cursor = conn.execute(""" SELECT pt.prompt_id, pt.tag_id FROM prompt_tags pt LEFT JOIN prompts p ON pt.prompt_id = p.id WHERE p.id IS NULL - """ - ) + """) for ref in cursor.fetchall(): issues.append( f"prompt_tags entry references non-existent prompt {ref['prompt_id']}" ) # Check for orphaned image entries - cursor = conn.execute( - """ + cursor = conn.execute(""" SELECT gi.id, gi.prompt_id FROM generated_images gi LEFT JOIN prompts p ON gi.prompt_id = p.id WHERE p.id IS NULL - """ - ) + """) for ref in cursor.fetchall(): issues.append( f"Image {ref['id']} references non-existent prompt {ref['prompt_id']}" diff --git a/utils/diagnostics.py b/utils/diagnostics.py index a611d2f..60bb7c6 100644 --- a/utils/diagnostics.py +++ b/utils/diagnostics.py @@ -137,12 +137,10 @@ class GalleryDiagnostics: self.logger.info(f" [NOTE] Prompts in database: {prompt_count}") # Check if generated_images table exists - cursor = conn.execute( - """ + cursor = conn.execute(""" SELECT name FROM sqlite_master WHERE type='table' AND name='generated_images' - """ - ) + """) has_images_table = cursor.fetchone() is not None self.logger.info(f" [IMG] Images table exists: {has_images_table}") @@ -176,12 +174,10 @@ class GalleryDiagnostics: conn.row_factory = sqlite3.Row # Check if table exists - cursor = conn.execute( - """ + cursor = conn.execute(""" SELECT name FROM sqlite_master WHERE type='table' AND name='generated_images' - """ - ) + """) if not cursor.fetchone(): return { @@ -195,15 +191,13 @@ class GalleryDiagnostics: self.logger.info(f" [STATS] Images in database: {image_count}") # Get recent images - cursor = conn.execute( - """ + cursor = conn.execute(""" SELECT gi.*, p.text FROM generated_images gi LEFT JOIN prompts p ON gi.prompt_id = p.id ORDER BY gi.generation_time DESC LIMIT 5 - """ - ) + """) recent_images = [dict(row) for row in cursor.fetchall()] self.logger.info(f" [TIME] Recent images: {len(recent_images)}")