- 分支管理新增「分支 / Tag」类型选择,Tag 存于 config["tags"],与分支不可重名 - 每个 Tag 与分支一样拥有同名独立版本轨道,默认上架轨 - 打包页按「分支」「Tag」分组选择;任务新增 ref_type 字段记录引用类型 - Tag 打包时 fetch --tags --force 后以游离 HEAD 检出 refs/tags/<tag> - 历史记录与下载页按类型显示「分支」或「Tag」 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
64 lines
1.9 KiB
Python
64 lines
1.9 KiB
Python
"""数据库配置"""
|
|
from sqlalchemy import create_engine, text
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from .config import DATABASE_URL
|
|
|
|
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
Base = declarative_base()
|
|
|
|
|
|
def get_db():
|
|
"""获取数据库会话"""
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
def _migrate_db():
|
|
"""简单的数据库迁移:为旧表添加缺失的列"""
|
|
migrations = [
|
|
("tasks", "branch", "VARCHAR DEFAULT 'main'"),
|
|
("tasks", "ref_type", "VARCHAR DEFAULT 'branch'"),
|
|
("tasks", "error_category", "VARCHAR"),
|
|
("tasks", "superseded_at", "TIMESTAMP"),
|
|
("tasks", "dsym_url", "VARCHAR"),
|
|
]
|
|
with engine.connect() as conn:
|
|
for table, column, col_type in migrations:
|
|
try:
|
|
conn.execute(text(f"ALTER TABLE {table} ADD COLUMN {column} {col_type}"))
|
|
conn.commit()
|
|
except Exception:
|
|
pass # 列已存在则忽略
|
|
|
|
|
|
def _seed_admin():
|
|
"""初始化管理员账号(从 .env 配置)"""
|
|
from .models import User
|
|
from .config import ADMIN_USERNAME, ADMIN_PASSWORD
|
|
from .security import hash_password
|
|
|
|
db = SessionLocal()
|
|
try:
|
|
if not db.query(User).filter(User.username == ADMIN_USERNAME).first():
|
|
db.add(User(id="admin-001", username=ADMIN_USERNAME,
|
|
password_hash=hash_password(ADMIN_PASSWORD), is_admin=True))
|
|
db.commit()
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
def init_db():
|
|
"""初始化数据库"""
|
|
# Load model declarations before create_all so a standalone startup creates every table.
|
|
from . import models # noqa: F401
|
|
|
|
Base.metadata.create_all(bind=engine)
|
|
_migrate_db()
|
|
_seed_admin()
|