feat: implement all 4 ORM models (SshKey, Server, Repo, SyncLog)
- Created SshKey model with encrypted private key storage - Created Server model with Gitea configuration and SshKey relationship - Created Repo model with repository mirror info and Server relationship - Created SyncLog model with sync operation logs and Repo relationship - Updated models/__init__.py to export all models - All models use Integer (Unix timestamp) for datetime fields - Proper bidirectional relationships using back_populates - Added comprehensive test suite for all models and relationships Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
35
backend/app/models/server.py
Normal file
35
backend/app/models/server.py
Normal file
@@ -0,0 +1,35 @@
|
||||
"""
|
||||
服务器 ORM 模型.
|
||||
"""
|
||||
from sqlalchemy import String, Integer, Text, Boolean, ForeignKey
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from app.database import Base
|
||||
from typing import Optional, List
|
||||
|
||||
|
||||
class Server(Base):
|
||||
"""
|
||||
Gitea 服务器模型.
|
||||
|
||||
存储 Gitea 服务器配置和连接信息.
|
||||
"""
|
||||
__tablename__ = "servers"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
name: Mapped[str] = mapped_column(String(100), nullable=False, unique=True)
|
||||
url: Mapped[str] = mapped_column(String(500), nullable=False)
|
||||
api_token: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
ssh_key_id: Mapped[int] = mapped_column(Integer, ForeignKey("ssh_keys.id"), nullable=False)
|
||||
sync_enabled: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
|
||||
schedule_cron: Mapped[Optional[str]] = mapped_column(String(50), nullable=True)
|
||||
local_path: Mapped[str] = mapped_column(String(500), nullable=False)
|
||||
status: Mapped[str] = mapped_column(String(20), nullable=False, default="untested")
|
||||
created_at: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
updated_at: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
|
||||
# 关系
|
||||
ssh_key: Mapped["SshKey"] = relationship("SshKey", back_populates="servers")
|
||||
repos: Mapped[List["Repo"]] = relationship("Repo", back_populates="server", cascade="all, delete-orphan")
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<Server(id={self.id}, name='{self.name}', url='{self.url}', status='{self.status}')>"
|
||||
Reference in New Issue
Block a user