nex_buddy/server/app/models/firmware.py

58 lines
2.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""固件包:OTA 升级的版本仓。
设计要点(为什么这么建):
- **按机型 + 通道隔离**:一个平台可能接多种硬件(当前只有 JC3636K518C_I_W),
不同机型的 bin 不能互刷;stable/beta 两个通道让测试版和设备自更新互不干扰。
- **published 而不是删表**:设备手上可能还跑着旧版本,删一行会丢历史;
「下线」只是不再下发给新设备,文件仍在,管理端还能下载复核。
- **version_code 冗余存一份**:semver 的比较在 SQL 里写起来很丑,
落库时算成整数(major*10^6 + minor*10^3 + patch),排序与取最新都变成一次 ORDER BY。
- **sha256 必存**:设备端下载完要校验,服务端自己也要能自证文件没被替换。
"""
from __future__ import annotations
import uuid
from datetime import datetime
from typing import TYPE_CHECKING
from sqlalchemy import BigInteger, Boolean, DateTime, Integer, String, Text, UniqueConstraint, func
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column
from app.db.session import Base
if TYPE_CHECKING:
pass
class Firmware(Base):
__tablename__ = "firmwares"
__table_args__ = (UniqueConstraint("model", "channel", "version", name="uq_firmware_version"),)
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
model: Mapped[str] = mapped_column(String(64), default="JC3636K518C_I_W", index=True)
version: Mapped[str] = mapped_column(String(32))
version_code: Mapped[int] = mapped_column(Integer, default=0, index=True)
channel: Mapped[str] = mapped_column(String(16), default="stable")
size_bytes: Mapped[int] = mapped_column(BigInteger, default=0)
sha256: Mapped[str] = mapped_column(String(64), default="")
# 落盘文件名(相对 data/firmware),原始上传名只作展示
filename: Mapped[str] = mapped_column(String(128), default="")
original_name: Mapped[str] = mapped_column(String(160), default="")
notes: Mapped[str] = mapped_column(Text, default="")
notes_en: Mapped[str] = mapped_column(Text, default="")
# 低于此版本不允许直接跨刷(留空表示不限制)
min_version: Mapped[str] = mapped_column(String(32), default="")
published: Mapped[bool] = mapped_column(Boolean, default=False, index=True)
download_count: Mapped[int] = mapped_column(Integer, default=0)
created_by: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True))
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
published_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))