File size: 2,248 Bytes
4e5a541
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""

数据库初始化脚本

创建所有表并插入默认数据

"""
import asyncio
import bcrypt
from sqlalchemy import select
from app.database import init_db, AsyncSessionLocal
from app.models import Setting
from app.config import settings


async def create_default_settings():
    """创建默认系统设置"""
    async with AsyncSessionLocal() as session:
        # 检查是否已经初始化
        result = await session.execute(select(Setting).where(Setting.key == "initialized"))
        existing = result.scalar_one_or_none()

        if existing:
            print("数据库已经初始化,跳过默认数据插入")
            return

        # 生成管理员密码哈希
        password_hash = bcrypt.hashpw(
            settings.admin_password.encode('utf-8'),
            bcrypt.gensalt()
        ).decode('utf-8')

        # 默认设置
        default_settings = [
            Setting(
                key="initialized",
                value="true",
                description="数据库初始化标记"
            ),
            Setting(
                key="admin_password_hash",
                value=password_hash,
                description="管理员密码哈希"
            ),
            Setting(
                key="proxy",
                value=settings.proxy,
                description="代理地址 (支持 http:// 和 socks5://)"
            ),
            Setting(
                key="proxy_enabled",
                value=str(settings.proxy_enabled).lower(),
                description="是否启用代理"
            ),
            Setting(
                key="log_level",
                value=settings.log_level,
                description="日志级别"
            ),
        ]

        session.add_all(default_settings)
        await session.commit()
        print("默认设置已创建")


async def main():
    """主函数"""
    print("开始初始化数据库...")

    # 创建所有表
    await init_db()
    print("数据库表创建完成")

    # 插入默认数据
    await create_default_settings()

    print("数据库初始化完成!")


if __name__ == "__main__":
    asyncio.run(main())