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:
@@ -1,3 +1,7 @@
|
||||
from app.database import Base
|
||||
from app.models.ssh_key import SshKey
|
||||
from app.models.server import Server
|
||||
from app.models.repo import Repo
|
||||
from app.models.sync_log import SyncLog
|
||||
|
||||
__all__ = ['Base']
|
||||
__all__ = ['Base', 'SshKey', 'Server', 'Repo', 'SyncLog']
|
||||
|
||||
33
backend/app/models/repo.py
Normal file
33
backend/app/models/repo.py
Normal file
@@ -0,0 +1,33 @@
|
||||
"""
|
||||
仓库 ORM 模型.
|
||||
"""
|
||||
from sqlalchemy import String, Integer, ForeignKey
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from app.database import Base
|
||||
from typing import Optional, List
|
||||
|
||||
|
||||
class Repo(Base):
|
||||
"""
|
||||
Git 仓库镜像模型.
|
||||
|
||||
存储从 Gitea 服务器同步的仓库信息.
|
||||
"""
|
||||
__tablename__ = "repos"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
server_id: Mapped[int] = mapped_column(Integer, ForeignKey("servers.id"), nullable=False)
|
||||
name: Mapped[str] = mapped_column(String(200), nullable=False)
|
||||
full_name: Mapped[str] = mapped_column(String(300), nullable=False)
|
||||
clone_url: Mapped[str] = mapped_column(String(500), nullable=False)
|
||||
local_path: Mapped[str] = mapped_column(String(500), nullable=False)
|
||||
last_sync_at: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
|
||||
status: Mapped[str] = mapped_column(String(20), nullable=False, default="pending")
|
||||
created_at: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
|
||||
# 关系
|
||||
server: Mapped["Server"] = relationship("Server", back_populates="repos")
|
||||
sync_logs: Mapped[List["SyncLog"]] = relationship("SyncLog", back_populates="repo", cascade="all, delete-orphan")
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<Repo(id={self.id}, name='{self.name}', full_name='{self.full_name}', status='{self.status}')>"
|
||||
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}')>"
|
||||
28
backend/app/models/ssh_key.py
Normal file
28
backend/app/models/ssh_key.py
Normal file
@@ -0,0 +1,28 @@
|
||||
"""
|
||||
SSH 密钥 ORM 模型.
|
||||
"""
|
||||
from sqlalchemy import String, Integer, Text
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from app.database import Base
|
||||
from typing import Optional, List
|
||||
|
||||
|
||||
class SshKey(Base):
|
||||
"""
|
||||
SSH 密钥模型.
|
||||
|
||||
存储加密的 SSH 私钥,用于 Git 操作.
|
||||
"""
|
||||
__tablename__ = "ssh_keys"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
name: Mapped[str] = mapped_column(String(100), nullable=False, unique=True)
|
||||
private_key: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
fingerprint: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||||
created_at: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
|
||||
# 关系
|
||||
servers: Mapped[List["Server"]] = relationship("Server", back_populates="ssh_key")
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<SshKey(id={self.id}, name='{self.name}', fingerprint='{self.fingerprint}')>"
|
||||
31
backend/app/models/sync_log.py
Normal file
31
backend/app/models/sync_log.py
Normal file
@@ -0,0 +1,31 @@
|
||||
"""
|
||||
同步日志 ORM 模型.
|
||||
"""
|
||||
from sqlalchemy import String, Integer, Text, ForeignKey
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from app.database import Base
|
||||
from typing import Optional, List
|
||||
|
||||
|
||||
class SyncLog(Base):
|
||||
"""
|
||||
同步日志模型.
|
||||
|
||||
记录仓库同步操作的详细信息.
|
||||
"""
|
||||
__tablename__ = "sync_logs"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
repo_id: Mapped[int] = mapped_column(Integer, ForeignKey("repos.id"), nullable=False)
|
||||
status: Mapped[str] = mapped_column(String(20), nullable=False)
|
||||
started_at: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
finished_at: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
commits_count: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
|
||||
error_msg: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
||||
created_at: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
|
||||
# 关系
|
||||
repo: Mapped["Repo"] = relationship("Repo", back_populates="sync_logs")
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<SyncLog(id={self.id}, repo_id={self.repo_id}, status='{self.status}', commits_count={self.commits_count})>"
|
||||
Reference in New Issue
Block a user