import sqlite3 import os DATABASE_PATH = os.path.join(os.path.dirname(__file__), '../database/app.db') def migrate(): conn = sqlite3.connect(DATABASE_PATH) cursor = conn.cursor() try: print('开始数据库迁移...') cursor.execute(''' CREATE TABLE IF NOT EXISTS system_config ( id INTEGER PRIMARY KEY AUTOINCREMENT, config_key TEXT UNIQUE NOT NULL, config_value TEXT NOT NULL, description TEXT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ) ''') print('✓ system_config 表已创建') cursor.execute('SELECT COUNT(*) FROM system_config') count = cursor.fetchone()[0] if count == 0: cursor.execute(''' INSERT INTO system_config (config_key, config_value, description) VALUES ('max_publish_tasks', '3', '用户最多可发布的任务数(待处理+进行中)'), ('max_accept_tasks', '3', '用户最多可承接的任务数(进行中)') ''') print('✓ 默认配置数据已插入') else: print('配置数据已存在,跳过插入') conn.commit() print('✓ 数据库迁移完成!') except Exception as e: print(f'迁移失败: {e}') conn.rollback() finally: conn.close() if __name__ == '__main__': migrate()