Fix get_user_api_tokens to handle missing 'name' column

parent 817b4e0e
...@@ -372,6 +372,11 @@ def init_db(conn) -> None: ...@@ -372,6 +372,11 @@ def init_db(conn) -> None:
FOREIGN KEY (user_id) REFERENCES users (id) FOREIGN KEY (user_id) REFERENCES users (id)
) )
''') ''')
# Add name column if it doesn't exist (for migration)
try:
cursor.execute('ALTER TABLE user_api_tokens ADD COLUMN name TEXT')
except sqlite3.OperationalError:
pass # Column already exists
# Insert default admin user if not exist # Insert default admin user if not exist
import hashlib import hashlib
...@@ -1150,14 +1155,28 @@ def get_user_api_tokens(user_id: int) -> List[Dict[str, Any]]: ...@@ -1150,14 +1155,28 @@ def get_user_api_tokens(user_id: int) -> List[Dict[str, Any]]:
"""Get all API tokens for a user.""" """Get all API tokens for a user."""
conn = get_db_connection() conn = get_db_connection()
cursor = conn.cursor() cursor = conn.cursor()
try:
cursor.execute(''' cursor.execute('''
SELECT id, name, created_at, last_used, active SELECT id, name, created_at, last_used, active
FROM user_api_tokens FROM user_api_tokens
WHERE user_id = ? ORDER BY created_at DESC WHERE user_id = ? ORDER BY created_at DESC
''', (user_id,)) ''', (user_id,))
except sqlite3.OperationalError:
# Fallback if 'name' column doesn't exist
cursor.execute('''
SELECT id, created_at, last_used, active
FROM user_api_tokens
WHERE user_id = ? ORDER BY created_at DESC
''', (user_id,))
rows = cursor.fetchall() rows = cursor.fetchall()
conn.close() conn.close()
return [dict(row) for row in rows] tokens = []
for row in rows:
token_dict = dict(row)
if 'name' not in token_dict:
token_dict['name'] = 'Unnamed Token' # Default name
tokens.append(token_dict)
return tokens
def delete_user_api_token(user_id: int, token_id: int) -> bool: def delete_user_api_token(user_id: int, token_id: int) -> bool:
......
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