#!/usr/bin/env python3
"""
添加感兴趣标记字段脚本
为articles表添加is_interested字段，默认值为0
"""

import sqlite3
import os
from loguru import logger

def add_interested_field():
    """为articles表添加is_interested字段"""
    # 获取当前脚本所在目录的数据库文件路径
    script_dir = os.path.dirname(os.path.abspath(__file__))
    db_path = os.path.join(script_dir, "tophub_data.db")
    
    # 检查数据库文件是否存在
    if not os.path.exists(db_path):
        logger.error(f"数据库文件不存在: {db_path}")
        return False
    
    try:
        # 连接数据库
        conn = sqlite3.connect(db_path)
        cursor = conn.cursor()
        
        # 检查is_interested字段是否已存在
        cursor.execute("PRAGMA table_info(articles)")
        columns = cursor.fetchall()
        column_names = [column[1] for column in columns]
        
        if "is_interested" in column_names:
            logger.info("is_interested字段已存在，无需添加")
            conn.close()
            return True
        
        # 添加is_interested字段，默认值为0
        logger.info("正在添加is_interested字段...")
        cursor.execute("ALTER TABLE articles ADD COLUMN is_interested INTEGER DEFAULT 0")
        
        # 提交更改
        conn.commit()
        logger.info("成功添加is_interested字段")
        
        # 验证字段是否添加成功
        cursor.execute("PRAGMA table_info(articles)")
        columns = cursor.fetchall()
        column_names = [column[1] for column in columns]
        
        if "is_interested" in column_names:
            logger.info("验证成功：is_interested字段已添加到articles表")
        else:
            logger.error("验证失败：is_interested字段未成功添加")
            conn.close()
            return False
            
        conn.close()
        return True
        
    except sqlite3.Error as e:
        logger.error(f"数据库操作出错: {str(e)}")
        return False
    except Exception as e:
        logger.error(f"添加字段时出错: {str(e)}")
        return False

if __name__ == "__main__":
    logger.add("db_modify.log", rotation="10 MB", level="INFO")
    if add_interested_field():
        logger.info("数据库修改完成")
    else:
        logger.error("数据库修改失败")