diff --git a/amt/migrations/versions/f6da4d6dd867_remove_project_table.py b/amt/migrations/versions/f6da4d6dd867_remove_project_table.py new file mode 100644 index 00000000..30e2e168 --- /dev/null +++ b/amt/migrations/versions/f6da4d6dd867_remove_project_table.py @@ -0,0 +1,40 @@ +"""remove project table + +Revision ID: f6da4d6dd867 +Revises: ff46b7ecc348 +Create Date: 2024-11-15 11:30:25.352543 + +""" + +from collections.abc import Sequence + +import sqlalchemy as sa +from alembic import op +from sqlalchemy.dialects import sqlite + +# revision identifiers, used by Alembic. +revision: str = "f6da4d6dd867" +down_revision: str | None = "ff46b7ecc348" +branch_labels: str | Sequence[str] | None = None +depends_on: str | Sequence[str] | None = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table("project") + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table( + "project", + sa.Column("id", sa.INTEGER(), nullable=False), + sa.Column("name", sa.VARCHAR(length=255), nullable=False), + sa.Column("lifecycle", sa.VARCHAR(length=32), nullable=True), + sa.Column("last_edited", sa.DATETIME(), server_default=sa.text("(CURRENT_TIMESTAMP)"), nullable=False), + sa.Column("system_card_json", sqlite.JSON(), nullable=True), + sa.Column("deleted_at", sa.DATETIME(), nullable=True), + sa.PrimaryKeyConstraint("id", name="pk_project"), + ) + # ### end Alembic commands ### diff --git a/amt/models/__init__.py b/amt/models/__init__.py index 6491ac32..9a921ea7 100644 --- a/amt/models/__init__.py +++ b/amt/models/__init__.py @@ -1,6 +1,5 @@ from .algorithm import Algorithm -from .project import Project from .task import Task from .user import User -__all__ = ["Task", "User", "Algorithm", "Project"] +__all__ = ["Task", "User", "Algorithm"] diff --git a/amt/models/project.py b/amt/models/project.py deleted file mode 100644 index fe5cf22b..00000000 --- a/amt/models/project.py +++ /dev/null @@ -1,97 +0,0 @@ -import json -from datetime import datetime -from enum import Enum -from typing import Any, TypeVar - -from sqlalchemy import String, func -from sqlalchemy.dialects.postgresql import ENUM -from sqlalchemy.orm import Mapped, mapped_column -from sqlalchemy.types import JSON - -from amt.api.lifecycles import Lifecycles -from amt.models.base import Base -from amt.schema.system_card import SystemCard - -T = TypeVar("T", bound="Project") - - -class CustomJSONEncoder(json.JSONEncoder): - def default(self, o: Any) -> Any: # noqa: ANN401 - if isinstance(o, datetime): - return o.isoformat() - if isinstance(o, Enum): - return o.name - return super().default(o) - - -class ProjectSystemCard(SystemCard): - def __init__(self, parent: "Project", **data: Any) -> None: # noqa: ANN401 - super().__init__(**data) - self._parent = parent - - def __setattr__(self, name: str, value: Any) -> None: # noqa: ANN401 - super().__setattr__(name, value) - if name != "_parent" and hasattr(self, "_parent"): - self._parent.sync_system_card() - - def __eq__(self, other: Any) -> bool: # noqa: ANN401 - if isinstance(other, ProjectSystemCard | SystemCard): - return self.model_dump(exclude={"_parent"}) == other.model_dump() - return False - - def model_dump(self, *args: Any, **kwargs: Any) -> dict[str, Any]: # noqa: ANN401 - exclude_unset = kwargs.pop("exclude_unset", False) - by_alias = kwargs.pop("by_alias", False) - exclude_none = kwargs.pop("exclude_none", False) - - dumped = super().model_dump( - *args, exclude_unset=exclude_unset, by_alias=by_alias, exclude_none=exclude_none, **kwargs - ) - - return json.loads(json.dumps(dumped, cls=CustomJSONEncoder)) - - -class Project(Base): - __tablename__ = "project" - - id: Mapped[int] = mapped_column(primary_key=True) - name: Mapped[str] = mapped_column(String(255)) - lifecycle: Mapped[Lifecycles | None] = mapped_column(ENUM(Lifecycles, name="lifecycle"), nullable=True) - system_card_json: Mapped[dict[str, Any]] = mapped_column(JSON, default=dict) - last_edited: Mapped[datetime] = mapped_column(server_default=func.now(), onupdate=func.now(), nullable=False) - deleted_at: Mapped[datetime | None] = mapped_column(server_default=None, nullable=True) - - def __init__(self, *args: Any, **kwargs: Any) -> None: # noqa: ANN401 - system_card: SystemCard | None = kwargs.pop("system_card", None) - super().__init__(*args, **kwargs) - self._system_card: ProjectSystemCard | None = None - if system_card is not None: - self.system_card = system_card - - @property - def system_card(self) -> ProjectSystemCard: - if not hasattr(self, "_system_card"): - self._system_card: ProjectSystemCard | None = None - - if self._system_card is None: - if self.system_card_json: - self._system_card = ProjectSystemCard(self, **self.system_card_json) - else: - self._system_card = ProjectSystemCard(self) - self.sync_system_card() - return self._system_card - - @system_card.setter - def system_card(self, value: SystemCard | None) -> None: - if value is None: - self._system_card = ProjectSystemCard(self) - else: - self._system_card = ProjectSystemCard(self, **value.model_dump(exclude_unset=True, by_alias=True)) - self.sync_system_card() - - def sync_system_card(self) -> None: - if self._system_card is not None: - self.system_card_json = self._system_card.model_dump(exclude_unset=True, by_alias=True) - - -Project.__mapper_args__ = {"exclude_properties": ["_system_card"]}