Files
gpt-pilot/core/db/models/file.py
Senko Rasic 5b474ccc1f merge gpt-pilot 0.2 codebase
This is a complete rewrite of the GPT Pilot core, from the ground
up, making the agentic architecture front and center, and also
fixing some long-standing problems with the database architecture
that weren't feasible to solve without breaking compatibility.

As the database structure and config file syntax have changed,
we have automatic imports for projects and current configs,
see the README.md file for details.

This also relicenses the project to FSL-1.1-MIT license.
2024-05-22 21:42:25 +02:00

44 lines
1.4 KiB
Python

from typing import TYPE_CHECKING, Optional
from uuid import UUID
from sqlalchemy import ForeignKey, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column, relationship
from core.db.models import Base
if TYPE_CHECKING:
from core.db.models import FileContent, ProjectState
class File(Base):
__tablename__ = "files"
__table_args__ = (UniqueConstraint("project_state_id", "path"),)
# ID and parent FKs
id: Mapped[int] = mapped_column(primary_key=True)
project_state_id: Mapped[UUID] = mapped_column(ForeignKey("project_states.id", ondelete="CASCADE"))
content_id: Mapped[str] = mapped_column(ForeignKey("file_contents.id", ondelete="RESTRICT"))
# Attributes
path: Mapped[str] = mapped_column()
meta: Mapped[dict] = mapped_column(default=dict, server_default="{}")
# Relationships
project_state: Mapped[Optional["ProjectState"]] = relationship(back_populates="files")
content: Mapped["FileContent"] = relationship(back_populates="files", lazy="selectin")
def clone(self) -> "File":
"""
Clone the file object, to be used in a new project state.
The clone references the same file content object as the original.
:return: The cloned file object.
"""
return File(
project_state=None,
content_id=self.content_id,
path=self.path,
meta=self.meta,
)