Fix nullable attribute for created_at and updated_at fields in Variable model (#1669)

* Fix nullable attribute for created_at field in variable table

* Update package versions in pyproject.toml and poetry.lock files

* Fix nullable attribute for created_at and updated_at fields in Variable model

* Refactor VariableService class to use VariableCreate model for creating variables
This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-04-10 20:56:46 -03:00 committed by GitHub
commit 5e5ae3911d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 72 additions and 10 deletions

View file

@ -0,0 +1,60 @@
"""Fix nullable
Revision ID: e3bc869fa272
Revises: 1a110b568907
Create Date: 2024-04-10 19:17:22.820455
"""
from typing import Sequence, Union
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql
from sqlalchemy.engine.reflection import Inspector
# revision identifiers, used by Alembic.
revision: str = "e3bc869fa272"
down_revision: Union[str, None] = "1a110b568907"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
conn = op.get_bind()
inspector = Inspector.from_engine(conn) # type: ignore
table_names = inspector.get_table_names()
# ### commands auto generated by Alembic - please adjust! ###
if "variable" not in table_names:
return
column_names = [column["name"] for column in inspector.get_columns("variable")]
with op.batch_alter_table("variable", schema=None) as batch_op:
if "created_at" in column_names:
batch_op.alter_column(
"created_at",
existing_type=sa.TIMESTAMP(timezone=True),
nullable=True,
existing_server_default=sa.text("now()"),
)
# ### end Alembic commands ###
def downgrade() -> None:
conn = op.get_bind()
inspector = Inspector.from_engine(conn) # type: ignore
table_names = inspector.get_table_names()
# ### commands auto generated by Alembic - please adjust! ###
if "variable" not in table_names:
return
with op.batch_alter_table("variable", schema=None) as batch_op:
if "created_at" in inspector.get_columns("variable"):
batch_op.alter_column(
"created_at",
existing_type=sa.TIMESTAMP(timezone=True),
nullable=False,
existing_server_default=sa.text("now()"),
)
# ### end Alembic commands ###

View file

@ -26,11 +26,11 @@ class Variable(VariableBase, table=True):
)
# name is unique per user
created_at: datetime = Field(
sa_column=Column(DateTime(timezone=True), server_default=func.now(), nullable=False),
sa_column=Column(DateTime(timezone=True), server_default=func.now(), nullable=True),
description="Creation time of the variable",
)
updated_at: Optional[datetime] = Field(
sa_column=Column(DateTime(timezone=True)),
sa_column=Column(DateTime(timezone=True), nullable=True),
description="Last update time of the variable",
)
# foreign key to user table
@ -39,7 +39,9 @@ class Variable(VariableBase, table=True):
class VariableCreate(VariableBase):
type: Optional[str] = Field(None, description="Type of the variable")
created_at: Optional[datetime] = Field(default_factory=utc_now, description="Creation time of the variable")
updated_at: Optional[datetime] = Field(default_factory=utc_now, description="Creation time of the variable")
class VariableRead(SQLModel):

View file

@ -3,13 +3,14 @@ from typing import TYPE_CHECKING, Optional, Union
from uuid import UUID
from fastapi import Depends
from langflow.services.auth import utils as auth_utils
from langflow.services.base import Service
from langflow.services.database.models.variable.model import Variable
from langflow.services.deps import get_session
from loguru import logger
from sqlmodel import Session, select
from langflow.services.auth import utils as auth_utils
from langflow.services.base import Service
from langflow.services.database.models.variable.model import Variable, VariableCreate
from langflow.services.deps import get_session
if TYPE_CHECKING:
from langflow.services.settings.service import SettingsService
@ -93,14 +94,13 @@ class VariableService(Service):
_type: str = "Generic",
session: Session = Depends(get_session),
):
variable = Variable(
user_id=user_id,
variable_base = VariableCreate(
name=name,
type=_type,
value=auth_utils.encrypt_api_key(value, settings_service=self.settings_service),
)
variable = Variable.model_validate(variable_base, from_attributes=True, update={"user_id": user_id})
session.add(variable)
session.commit()
session.refresh(variable)
return variable
return variable