Files
gpt-pilot/core/db/models/file_content.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

48 lines
1.4 KiB
Python

from typing import TYPE_CHECKING
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import Mapped, mapped_column, relationship
from core.db.models import Base
if TYPE_CHECKING:
from core.db.models import File
class FileContent(Base):
__tablename__ = "file_contents"
# ID and parent FKs
id: Mapped[str] = mapped_column(primary_key=True)
# Attributes
content: Mapped[str] = mapped_column()
# Relationships
files: Mapped[list["File"]] = relationship(back_populates="content")
@classmethod
async def store(cls, session: AsyncSession, hash: str, content: str) -> "FileContent":
"""
Store the file content in the database.
If the content is already stored, returns the reference to the existing
content object. Otherwise stores it to the database and returns the newly
created content object.
:param session: The database session.
:param hash: The hash of the file content, used as an unique ID.
:param content: The file content as unicode string.
:return: The file content object.
"""
result = await session.execute(select(FileContent).where(FileContent.id == hash))
fc = result.scalar_one_or_none()
if fc is not None:
return fc
fc = cls(id=hash, content=content)
session.add(fc)
return fc