mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
ix_memory_card_versions_card and ix_devices_user_id are leftmost prefixes of a wider index on the same table, the shape 0125 exists to remove, so move them there rather than declaring them on the models. Also replace the deprecated version_path_separator that path_separator was added alongside, and cut the migration and model comments that restate each other or explain the change rather than the schema. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
84 lines
2.7 KiB
Python
84 lines
2.7 KiB
Python
from __future__ import annotations
|
|
|
|
import enum
|
|
from dataclasses import dataclass
|
|
from datetime import datetime
|
|
from typing import TYPE_CHECKING
|
|
|
|
from sqlalchemy import JSON, TIMESTAMP, Boolean, Enum, ForeignKey, Index, String
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from models.base import BaseModel
|
|
|
|
if TYPE_CHECKING:
|
|
from models.device_save_sync import DeviceSaveSync
|
|
from models.user import User
|
|
|
|
|
|
class SyncMode(enum.StrEnum):
|
|
API = "api"
|
|
FILE_TRANSFER = "file_transfer"
|
|
PUSH_PULL = "push_pull"
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class DeviceType:
|
|
"""A preconfigured device type with its client identifier and sync mode."""
|
|
|
|
platform: str
|
|
client: str
|
|
sync_mode: SyncMode
|
|
|
|
|
|
KNOWN_DEVICES: dict[str, DeviceType] = {
|
|
"web": DeviceType(platform="Web", client="web", sync_mode=SyncMode.API),
|
|
"grout": DeviceType(platform="muOS", client="grout", sync_mode=SyncMode.API),
|
|
"argosy-launcher": DeviceType(
|
|
platform="Android", client="argosy-launcher", sync_mode=SyncMode.API
|
|
),
|
|
}
|
|
|
|
|
|
class Device(BaseModel):
|
|
__tablename__ = "devices"
|
|
__table_args__ = (
|
|
Index(
|
|
"ix_devices_user_client_identifier",
|
|
"user_id",
|
|
"client_device_identifier",
|
|
unique=True,
|
|
),
|
|
{"extend_existing": True},
|
|
)
|
|
|
|
id: Mapped[str] = mapped_column(String(255), primary_key=True)
|
|
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
|
|
|
|
name: Mapped[str | None] = mapped_column(String(255))
|
|
platform: Mapped[str | None] = mapped_column(String(50))
|
|
client: Mapped[str | None] = mapped_column(String(50))
|
|
client_version: Mapped[str | None] = mapped_column(String(50))
|
|
|
|
ip_address: Mapped[str | None] = mapped_column(String(45))
|
|
mac_address: Mapped[str | None] = mapped_column(String(17))
|
|
hostname: Mapped[str | None] = mapped_column(String(255))
|
|
|
|
# Stable identifier supplied by the client itself (install UUID, hardware ID),
|
|
# used to dedupe re-registrations of the same device across token resets.
|
|
client_device_identifier: Mapped[str | None] = mapped_column(String(255))
|
|
|
|
sync_mode: Mapped[SyncMode] = mapped_column(Enum(SyncMode), default=SyncMode.API)
|
|
sync_enabled: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
sync_config: Mapped[dict | None] = mapped_column(JSON, nullable=True)
|
|
|
|
last_seen: Mapped[datetime | None] = mapped_column(
|
|
TIMESTAMP(timezone=True), index=True
|
|
)
|
|
|
|
user: Mapped[User] = relationship(lazy="joined")
|
|
save_syncs: Mapped[list[DeviceSaveSync]] = relationship(
|
|
back_populates="device",
|
|
cascade="all, delete-orphan",
|
|
lazy="raise",
|
|
)
|