feat(11-01): create sites table migration, model, and schemas

- Add migration 030 with sites table, RLS policy, and device site_id FK
- Add Site SQLAlchemy model with tenant isolation
- Add site_id nullable FK and relationship to Device model
- Add sites relationship to Tenant model
- Register Site in models __init__.py
- Add SiteCreate, SiteUpdate, SiteResponse, SiteListResponse schemas

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jason Staack
2026-03-18 21:37:08 -05:00
parent 0693e0898b
commit f7e678532c
6 changed files with 229 additions and 0 deletions

View File

@@ -13,6 +13,7 @@ from app.models.device import (
from app.models.alert import AlertRule, NotificationChannel, AlertRuleChannel, AlertEvent
from app.models.firmware import FirmwareVersion, FirmwareUpgradeJob
from app.models.config_template import ConfigTemplate, ConfigTemplateTag, TemplatePushJob
from app.models.site import Site
from app.models.audit_log import AuditLog
from app.models.maintenance_window import MaintenanceWindow
from app.models.api_key import ApiKey
@@ -28,6 +29,7 @@ __all__ = [
"DeviceGroupMembership",
"DeviceTagAssignment",
"DeviceStatus",
"Site",
"AlertRule",
"NotificationChannel",
"AlertRuleChannel",

View File

@@ -101,6 +101,13 @@ class Device(Base):
tag_assignments: Mapped[list["DeviceTagAssignment"]] = relationship(
"DeviceTagAssignment", back_populates="device", cascade="all, delete-orphan"
)
site_id: Mapped[uuid.UUID | None] = mapped_column(
UUID(as_uuid=True),
ForeignKey("sites.id", ondelete="SET NULL"),
nullable=True,
index=True,
)
site: Mapped["Site"] = relationship("Site", back_populates="devices") # type: ignore[name-defined]
def __repr__(self) -> str:
return f"<Device id={self.id} hostname={self.hostname!r} tenant_id={self.tenant_id}>"

View File

@@ -0,0 +1,49 @@
"""Site model -- physical location grouping for devices."""
import uuid
from datetime import datetime
from sqlalchemy import DateTime, Float, ForeignKey, String, Text, UniqueConstraint, func
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.models.base import Base
class Site(Base):
__tablename__ = "sites"
__table_args__ = (UniqueConstraint("tenant_id", "name", name="uq_sites_tenant_name"),)
id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True),
primary_key=True,
default=uuid.uuid4,
server_default=func.gen_random_uuid(),
)
tenant_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True),
ForeignKey("tenants.id", ondelete="CASCADE"),
nullable=False,
index=True,
)
name: Mapped[str] = mapped_column(String(255), nullable=False)
latitude: Mapped[float | None] = mapped_column(Float, nullable=True)
longitude: Mapped[float | None] = mapped_column(Float, nullable=True)
address: Mapped[str | None] = mapped_column(Text, nullable=True)
elevation: Mapped[float | None] = mapped_column(Float, nullable=True)
notes: Mapped[str | None] = mapped_column(Text, nullable=True)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), nullable=False
)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False
)
# Relationships
tenant: Mapped["Tenant"] = relationship("Tenant", back_populates="sites") # type: ignore[name-defined]
devices: Mapped[list["Device"]] = relationship( # type: ignore[name-defined]
"Device", back_populates="site", foreign_keys="[Device.site_id]"
)
def __repr__(self) -> str:
return f"<Site id={self.id} name={self.name!r} tenant_id={self.tenant_id}>"

View File

@@ -52,6 +52,9 @@ class Tenant(Base):
device_tags: Mapped[list["DeviceTag"]] = relationship(
"DeviceTag", back_populates="tenant", passive_deletes=True
) # type: ignore[name-defined]
sites: Mapped[list["Site"]] = relationship(
"Site", back_populates="tenant", cascade="all, delete-orphan"
) # type: ignore[name-defined]
def __repr__(self) -> str:
return f"<Tenant id={self.id} name={self.name!r}>"