homelab_automation/app/models/docker_volume.py
Bruno Charest 817f8b4ee7
Some checks failed
Tests / Backend Tests (Python) (3.10) (push) Has been cancelled
Tests / Backend Tests (Python) (3.11) (push) Has been cancelled
Tests / Backend Tests (Python) (3.12) (push) Has been cancelled
Tests / Frontend Tests (JS) (push) Has been cancelled
Tests / Integration Tests (push) Has been cancelled
Tests / All Tests Passed (push) Has been cancelled
feat: Implement Homelab Automation API v2, introducing a new dashboard, comprehensive backend models, and API routes.
2026-03-06 09:31:08 -05:00

49 lines
1.8 KiB
Python

"""Docker Volume model for Homelab Automation."""
from datetime import datetime
from typing import Optional
from sqlalchemy import DateTime, ForeignKey, Integer, String, Text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.sql import func
from .database import Base
class DockerVolume(Base):
"""Model representing a Docker volume on a host."""
__tablename__ = "docker_volumes"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
host_id: Mapped[str] = mapped_column(String(50), ForeignKey("hosts.id", ondelete="CASCADE"), nullable=False)
name: Mapped[str] = mapped_column(String(255), nullable=False)
driver: Mapped[str] = mapped_column(String(50), nullable=True)
mountpoint: Mapped[str] = mapped_column(Text, nullable=True)
scope: Mapped[str] = mapped_column(String(20), nullable=True) # local/global
last_update_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False, server_default=func.now(), onupdate=func.now()
)
# Relationship to host
host: Mapped["Host"] = relationship("Host", back_populates="docker_volumes")
__table_args__ = (
UniqueConstraint('host_id', 'name', name='uq_docker_volumes_host_name'),
{"sqlite_autoincrement": True},
)
def __repr__(self) -> str: # pragma: no cover
return f"<DockerVolume id={self.id} name={self.name}>"
def to_dict(self) -> dict:
"""Convert to dictionary for API responses."""
return {
"id": self.id,
"host_id": self.host_id,
"name": self.name,
"driver": self.driver,
"mountpoint": self.mountpoint,
"scope": self.scope,
"last_update_at": self.last_update_at.isoformat() if self.last_update_at else None,
}