Fix SQLite ALTER TABLE for updated_at column

- Remove DEFAULT CURRENT_TIMESTAMP from SQLite CREATE TABLE for updated_at
- Modify ALTER TABLE to add column without default and update existing rows
- Ensure compatibility with SQLite's ALTER TABLE restrictions
parent 7f83290e
...@@ -272,7 +272,7 @@ def init_db(conn) -> None: ...@@ -272,7 +272,7 @@ def init_db(conn) -> None:
result TEXT, result TEXT,
error_message TEXT, error_message TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP,
started_at TIMESTAMP, started_at TIMESTAMP,
completed_at TIMESTAMP, completed_at TIMESTAMP,
estimated_time INTEGER, estimated_time INTEGER,
...@@ -645,15 +645,23 @@ def init_db(conn) -> None: ...@@ -645,15 +645,23 @@ 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:
# First check if column exists # First check if column exists
cursor.execute("PRAGMA table_info(processing_queue)" if config['type'] == 'sqlite' else "DESCRIBE processing_queue") if config['type'] == 'sqlite':
cursor.execute("PRAGMA table_info(processing_queue)")
columns = cursor.fetchall()
column_names = [col[1] for col in columns]
else:
cursor.execute("DESCRIBE processing_queue")
columns = cursor.fetchall() columns = cursor.fetchall()
column_names = [col[1] if config['type'] == 'sqlite' else col[0] for col in columns] column_names = [col[0] for col in columns]
if 'updated_at' not in column_names: if 'updated_at' not in column_names:
if config['type'] == 'mysql': if config['type'] == 'mysql':
cursor.execute('ALTER TABLE processing_queue ADD COLUMN updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP') cursor.execute('ALTER TABLE processing_queue ADD COLUMN updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
else: else:
cursor.execute('ALTER TABLE processing_queue ADD COLUMN updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP') # For SQLite, add column without default first
cursor.execute('ALTER TABLE processing_queue ADD COLUMN updated_at TIMESTAMP')
# Then set default for future inserts
cursor.execute('UPDATE processing_queue SET updated_at = CURRENT_TIMESTAMP WHERE updated_at IS NULL')
print("Added updated_at column to processing_queue table") print("Added updated_at column to processing_queue table")
except Exception as e: except Exception as e:
print(f"Error adding updated_at column: {e}") print(f"Error adding updated_at column: {e}")
......
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