models.py•3.1 kB
"""
Database models for the Sectional MCP Panel.
"""
from sqlalchemy import Column, Integer, String, Text, Boolean, ForeignKey, JSON, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from datetime import datetime
Base = declarative_base()
class Panel(Base):
"""Global panel configuration."""
__tablename__ = "panel"
id = Column(Integer, primary_key=True)
name = Column(String(255), nullable=False)
version = Column(String(50), nullable=False)
config_schema_version = Column(String(50), nullable=False)
global_settings = Column(JSON, nullable=True)
created_at = Column(DateTime, default=datetime.utcnow)
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
class Section(Base):
"""Section definition."""
__tablename__ = "sections"
id = Column(Integer, primary_key=True)
name = Column(String(255), nullable=False, unique=True)
description = Column(Text, nullable=True)
settings = Column(JSON, nullable=True)
created_at = Column(DateTime, default=datetime.utcnow)
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
# Relationships
servers = relationship("Server", back_populates="section", cascade="all, delete-orphan")
class Server(Base):
"""Server definition."""
__tablename__ = "servers"
id = Column(Integer, primary_key=True)
name = Column(String(255), nullable=False)
section_id = Column(Integer, ForeignKey("sections.id"), nullable=False)
description = Column(Text, nullable=True)
runtime_definition = Column(JSON, nullable=False)
settings = Column(JSON, nullable=True)
status = Column(String(50), default="Stopped")
process_id = Column(String(255), nullable=True) # PID or container ID
created_at = Column(DateTime, default=datetime.utcnow)
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
# Relationships
section = relationship("Section", back_populates="servers")
class Task(Base):
"""Asynchronous task tracking."""
__tablename__ = "tasks"
id = Column(Integer, primary_key=True)
task_id = Column(String(255), nullable=False, unique=True)
task_type = Column(String(50), nullable=False)
status = Column(String(50), default="Pending")
details = Column(JSON, nullable=True)
result = Column(JSON, nullable=True)
error = Column(Text, nullable=True)
created_at = Column(DateTime, default=datetime.utcnow)
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
class AuditLog(Base):
"""Audit logging for panel operations."""
__tablename__ = "audit_logs"
id = Column(Integer, primary_key=True)
action = Column(String(255), nullable=False)
entity_type = Column(String(50), nullable=False)
entity_id = Column(String(255), nullable=True)
user = Column(String(255), nullable=True)
details = Column(JSON, nullable=True)
timestamp = Column(DateTime, default=datetime.utcnow)