Fix database migration for updated_at column

- Add proper column existence check before ALTER TABLE
- Make update_queue_status work with or without updated_at column
- Update API to handle jobs without updated_at field
- Ensure backward compatibility with existing databases
parent bcab8d79
...@@ -644,12 +644,19 @@ def init_db(conn) -> None: ...@@ -644,12 +644,19 @@ def init_db(conn) -> None:
# Add updated_at column to processing_queue table if it doesn't exist # Add updated_at column to processing_queue table if it doesn't exist
try: try:
if config['type'] == 'mysql': # First check if column exists
cursor.execute('ALTER TABLE processing_queue ADD COLUMN updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP') cursor.execute("PRAGMA table_info(processing_queue)" if config['type'] == 'sqlite' else "DESCRIBE processing_queue")
else: columns = cursor.fetchall()
cursor.execute('ALTER TABLE processing_queue ADD COLUMN updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP') column_names = [col[1] if config['type'] == 'sqlite' else col[0] for col in columns]
except:
# Column might already exist if 'updated_at' not in column_names:
if config['type'] == 'mysql':
cursor.execute('ALTER TABLE processing_queue ADD COLUMN updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
else:
cursor.execute('ALTER TABLE processing_queue ADD COLUMN updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP')
print("Added updated_at column to processing_queue table")
except Exception as e:
print(f"Error adding updated_at column: {e}")
pass pass
# Cluster processes table # Cluster processes table
...@@ -1185,9 +1192,17 @@ def update_queue_status(queue_id: int, status: str, result: dict = None, error: ...@@ -1185,9 +1192,17 @@ def update_queue_status(queue_id: int, status: str, result: dict = None, error:
conn = get_db_connection() conn = get_db_connection()
cursor = conn.cursor() cursor = conn.cursor()
update_fields = ['status = ?', 'updated_at = CURRENT_TIMESTAMP'] update_fields = ['status = ?']
params = [status] params = [status]
# Check if updated_at column exists
try:
cursor.execute("SELECT updated_at FROM processing_queue LIMIT 1")
update_fields.append('updated_at = CURRENT_TIMESTAMP')
except:
# updated_at column doesn't exist, skip it
pass
if status == 'processing': if status == 'processing':
update_fields.append('started_at = CURRENT_TIMESTAMP') update_fields.append('started_at = CURRENT_TIMESTAMP')
elif status in ['completed', 'failed']: elif status in ['completed', 'failed']:
......
...@@ -533,7 +533,16 @@ def api_job_status_updates(): ...@@ -533,7 +533,16 @@ def api_job_status_updates():
else: else:
job_timestamp = job_time or 0 job_timestamp = job_time or 0
if job_timestamp > since_timestamp: # For jobs without updated_at, use created_at but only include if status changed recently
# Since we can't track updates, include all active jobs
include_job = False
if 'updated_at' in job and job['updated_at']:
include_job = job_timestamp > since_timestamp
else:
# For jobs without updated_at, include if they're active (not completed/failed)
include_job = job['status'] not in ['completed', 'failed']
if include_job:
updates.append({ updates.append({
'id': job['id'], 'id': job['id'],
'status': job['status'], 'status': job['status'],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment