-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Migrate FileMetadata to ORM (#2028)
- Loading branch information
Showing
19 changed files
with
247 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"""Move files to orm | ||
Revision ID: c85a3d07c028 | ||
Revises: cda66b6cb0d6 | ||
Create Date: 2024-11-12 13:58:57.221081 | ||
""" | ||
|
||
from typing import Sequence, Union | ||
|
||
import sqlalchemy as sa | ||
|
||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "c85a3d07c028" | ||
down_revision: Union[str, None] = "cda66b6cb0d6" | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column("files", sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=True)) | ||
op.add_column("files", sa.Column("is_deleted", sa.Boolean(), server_default=sa.text("FALSE"), nullable=False)) | ||
op.add_column("files", sa.Column("_created_by_id", sa.String(), nullable=True)) | ||
op.add_column("files", sa.Column("_last_updated_by_id", sa.String(), nullable=True)) | ||
op.add_column("files", sa.Column("organization_id", sa.String(), nullable=True)) | ||
# Populate `organization_id` based on `user_id` | ||
# Use a raw SQL query to update the organization_id | ||
op.execute( | ||
""" | ||
UPDATE files | ||
SET organization_id = users.organization_id | ||
FROM users | ||
WHERE files.user_id = users.id | ||
""" | ||
) | ||
op.alter_column("files", "organization_id", nullable=False) | ||
op.create_foreign_key(None, "files", "organizations", ["organization_id"], ["id"]) | ||
op.create_foreign_key(None, "files", "sources", ["source_id"], ["id"]) | ||
op.drop_column("files", "user_id") | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column("files", sa.Column("user_id", sa.VARCHAR(), autoincrement=False, nullable=False)) | ||
op.drop_constraint(None, "files", type_="foreignkey") | ||
op.drop_constraint(None, "files", type_="foreignkey") | ||
op.drop_column("files", "organization_id") | ||
op.drop_column("files", "_last_updated_by_id") | ||
op.drop_column("files", "_created_by_id") | ||
op.drop_column("files", "is_deleted") | ||
op.drop_column("files", "updated_at") | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from typing import TYPE_CHECKING, Optional | ||
|
||
from sqlalchemy import Integer, String | ||
from sqlalchemy.orm import Mapped, mapped_column, relationship | ||
|
||
from letta.orm.mixins import OrganizationMixin, SourceMixin | ||
from letta.orm.sqlalchemy_base import SqlalchemyBase | ||
from letta.schemas.file import FileMetadata as PydanticFileMetadata | ||
|
||
if TYPE_CHECKING: | ||
from letta.orm.organization import Organization | ||
|
||
|
||
class FileMetadata(SqlalchemyBase, OrganizationMixin, SourceMixin): | ||
"""Represents metadata for an uploaded file.""" | ||
|
||
__tablename__ = "files" | ||
__pydantic_model__ = PydanticFileMetadata | ||
|
||
file_name: Mapped[Optional[str]] = mapped_column(String, nullable=True, doc="The name of the file.") | ||
file_path: Mapped[Optional[str]] = mapped_column(String, nullable=True, doc="The file path on the system.") | ||
file_type: Mapped[Optional[str]] = mapped_column(String, nullable=True, doc="The type of the file.") | ||
file_size: Mapped[Optional[int]] = mapped_column(Integer, nullable=True, doc="The size of the file in bytes.") | ||
file_creation_date: Mapped[Optional[str]] = mapped_column(String, nullable=True, doc="The creation date of the file.") | ||
file_last_modified_date: Mapped[Optional[str]] = mapped_column(String, nullable=True, doc="The last modified date of the file.") | ||
|
||
# relationships | ||
organization: Mapped["Organization"] = relationship("Organization", back_populates="files", lazy="selectin") | ||
source: Mapped["Source"] = relationship("Source", back_populates="files", lazy="selectin") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.