Update db_manager.py
This commit is contained in:
217
db_manager.py
217
db_manager.py
@@ -3,132 +3,109 @@ import json
|
|||||||
import os
|
import os
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
class DatabaseManager:
|
DB_FILE = "app_data.db"
|
||||||
def __init__(self, db_name="app_data.db"):
|
JSON_BACKUP = "settings.json" # The old file you want to import from
|
||||||
self.db_name = db_name
|
|
||||||
self.conn = None
|
|
||||||
self.cursor = None
|
|
||||||
self.connect()
|
|
||||||
self.create_tables()
|
|
||||||
|
|
||||||
def connect(self):
|
def get_db_connection():
|
||||||
"""Establish connection to the SQLite database."""
|
conn = sqlite3.connect(DB_FILE)
|
||||||
self.conn = sqlite3.connect(self.db_name)
|
conn.row_factory = sqlite3.Row # Allows accessing columns by name
|
||||||
# Return rows as dictionaries for easier access (e.g., row['path'])
|
return conn
|
||||||
self.conn.row_factory = sqlite3.Row
|
|
||||||
self.cursor = self.conn.cursor()
|
|
||||||
|
|
||||||
def create_tables(self):
|
def init_db():
|
||||||
"""Initialize the database schema."""
|
"""Creates tables and imports JSON if DB is new."""
|
||||||
# Table for Single Tab Settings (keyed by Path)
|
conn = get_db_connection()
|
||||||
self.cursor.execute('''
|
cursor = conn.cursor()
|
||||||
CREATE TABLE IF NOT EXISTS path_settings (
|
|
||||||
path TEXT PRIMARY KEY,
|
|
||||||
config_data TEXT,
|
|
||||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
||||||
)
|
|
||||||
''')
|
|
||||||
|
|
||||||
# Table for Batch Tab History
|
# 1. Create Settings Table (Keyed by Path)
|
||||||
self.cursor.execute('''
|
cursor.execute('''
|
||||||
CREATE TABLE IF NOT EXISTS batch_history (
|
CREATE TABLE IF NOT EXISTS settings (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
path TEXT PRIMARY KEY,
|
||||||
batch_name TEXT,
|
params TEXT,
|
||||||
batch_data TEXT,
|
updated_at TIMESTAMP
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
)
|
||||||
)
|
''')
|
||||||
''')
|
|
||||||
self.conn.commit()
|
|
||||||
|
|
||||||
# --- SETTINGS / SINGLE TAB OPERATIONS ---
|
# 2. Create Batch History Table
|
||||||
|
cursor.execute('''
|
||||||
|
CREATE TABLE IF NOT EXISTS batch_log (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
timestamp TEXT,
|
||||||
|
path TEXT,
|
||||||
|
status TEXT,
|
||||||
|
details TEXT
|
||||||
|
)
|
||||||
|
''')
|
||||||
|
|
||||||
def save_setting(self, path, data_dict):
|
# 3. Check for Migration (If DB is empty but JSON exists)
|
||||||
"""
|
cursor.execute('SELECT count(*) FROM settings')
|
||||||
Saves settings for a specific path.
|
count = cursor.fetchone()[0]
|
||||||
Upsert: Inserts if new, Updates if exists.
|
|
||||||
"""
|
|
||||||
# Convert dictionary to JSON string for storage
|
|
||||||
json_data = json.dumps(data_dict)
|
|
||||||
|
|
||||||
query = '''
|
if count == 0 and os.path.exists(JSON_BACKUP):
|
||||||
INSERT INTO path_settings (path, config_data, updated_at)
|
print(f"Migration: Database empty. Importing from {JSON_BACKUP}...")
|
||||||
VALUES (?, ?, ?)
|
|
||||||
ON CONFLICT(path) DO UPDATE SET
|
|
||||||
config_data=excluded.config_data,
|
|
||||||
updated_at=excluded.updated_at
|
|
||||||
'''
|
|
||||||
self.cursor.execute(query, (path, json_data, datetime.now()))
|
|
||||||
self.conn.commit()
|
|
||||||
print(f"[DB] Settings saved for path: {path}")
|
|
||||||
|
|
||||||
def get_setting(self, path):
|
|
||||||
"""Retrieves settings for a specific path."""
|
|
||||||
query = "SELECT config_data FROM path_settings WHERE path = ?"
|
|
||||||
self.cursor.execute(query, (path,))
|
|
||||||
row = self.cursor.fetchone()
|
|
||||||
|
|
||||||
if row:
|
|
||||||
# Convert JSON string back to Python Dictionary
|
|
||||||
return json.loads(row['config_data'])
|
|
||||||
return None
|
|
||||||
|
|
||||||
# --- BATCH TAB OPERATIONS ---
|
|
||||||
|
|
||||||
def save_batch(self, batch_name, batch_data_dict):
|
|
||||||
"""Saves a batch job result."""
|
|
||||||
json_data = json.dumps(batch_data_dict)
|
|
||||||
query = "INSERT INTO batch_history (batch_name, batch_data) VALUES (?, ?)"
|
|
||||||
self.cursor.execute(query, (batch_name, json_data))
|
|
||||||
self.conn.commit()
|
|
||||||
print(f"[DB] Batch '{batch_name}' saved.")
|
|
||||||
|
|
||||||
def get_all_batches(self):
|
|
||||||
"""Retrieves all batch history."""
|
|
||||||
self.cursor.execute("SELECT * FROM batch_history ORDER BY created_at DESC")
|
|
||||||
rows = self.cursor.fetchall()
|
|
||||||
|
|
||||||
results = []
|
|
||||||
for row in rows:
|
|
||||||
results.append({
|
|
||||||
"id": row['id'],
|
|
||||||
"name": row['batch_name'],
|
|
||||||
"data": json.loads(row['batch_data']),
|
|
||||||
"date": row['created_at']
|
|
||||||
})
|
|
||||||
return results
|
|
||||||
|
|
||||||
# --- MIGRATION LOGIC ---
|
|
||||||
|
|
||||||
def is_db_empty(self):
|
|
||||||
"""Checks if the settings table is empty."""
|
|
||||||
self.cursor.execute("SELECT COUNT(*) FROM path_settings")
|
|
||||||
return self.cursor.fetchone()[0] == 0
|
|
||||||
|
|
||||||
def import_from_json(self, json_file_path):
|
|
||||||
"""
|
|
||||||
Reads a legacy JSON file and populates the database.
|
|
||||||
Assumes JSON structure: { "path_to_file": {setting_dict}, ... }
|
|
||||||
"""
|
|
||||||
if not os.path.exists(json_file_path):
|
|
||||||
print("[DB] No legacy JSON file found to import.")
|
|
||||||
return
|
|
||||||
|
|
||||||
print(f"[DB] Importing legacy data from {json_file_path}...")
|
|
||||||
try:
|
try:
|
||||||
with open(json_file_path, 'r') as f:
|
with open(JSON_BACKUP, 'r') as f:
|
||||||
legacy_data = json.load(f)
|
data = json.load(f)
|
||||||
|
# Assumes JSON structure: {"/path/to/img1": {config}, "/path/to/img2": {config}}
|
||||||
# Iterate through the JSON and save to DB
|
for path, config in data.items():
|
||||||
# Adjust this loop based on your exact JSON structure
|
cursor.execute(
|
||||||
if isinstance(legacy_data, dict):
|
'INSERT OR REPLACE INTO settings (path, params, updated_at) VALUES (?, ?, ?)',
|
||||||
for path_key, settings in legacy_data.items():
|
(path, json.dumps(config), datetime.now())
|
||||||
self.save_setting(path_key, settings)
|
)
|
||||||
print("[DB] Migration complete.")
|
conn.commit()
|
||||||
else:
|
print("Migration successful.")
|
||||||
print("[DB] JSON format not recognized (expected dict).")
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[DB] Error importing JSON: {e}")
|
print(f"Migration failed: {e}")
|
||||||
|
|
||||||
def close(self):
|
conn.commit()
|
||||||
self.conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
# --- SETTINGS FUNCTIONS ---
|
||||||
|
|
||||||
|
def load_settings_for_path(path):
|
||||||
|
"""Returns a dict of settings for the specific path, or None."""
|
||||||
|
conn = get_db_connection()
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute("SELECT params FROM settings WHERE path = ?", (path,))
|
||||||
|
row = cursor.fetchone()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
if row:
|
||||||
|
return json.loads(row['params'])
|
||||||
|
return None
|
||||||
|
|
||||||
|
def save_settings_for_path(path, settings_dict):
|
||||||
|
"""Saves the settings dict into the DB associated with the path."""
|
||||||
|
conn = get_db_connection()
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
|
json_str = json.dumps(settings_dict)
|
||||||
|
timestamp = datetime.now()
|
||||||
|
|
||||||
|
cursor.execute('''
|
||||||
|
INSERT INTO settings (path, params, updated_at)
|
||||||
|
VALUES (?, ?, ?)
|
||||||
|
ON CONFLICT(path) DO UPDATE SET
|
||||||
|
params=excluded.params,
|
||||||
|
updated_at=excluded.updated_at
|
||||||
|
''', (path, json_str, timestamp))
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
return f"Saved settings for: {path}"
|
||||||
|
|
||||||
|
# --- BATCH FUNCTIONS ---
|
||||||
|
|
||||||
|
def log_batch_run(path, status, details_dict):
|
||||||
|
conn = get_db_connection()
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
|
cursor.execute('''
|
||||||
|
INSERT INTO batch_log (timestamp, path, status, details)
|
||||||
|
VALUES (?, ?, ?, ?)
|
||||||
|
''', (datetime.now(), path, status, json.dumps(details_dict)))
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
# Initialize DB immediately when this module is imported
|
||||||
|
init_db()
|
||||||
|
|||||||
Reference in New Issue
Block a user