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:
parent
cc4d8c8326
commit
5e5ae3911d
3 changed files with 72 additions and 10 deletions
|
|
@ -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 ###
|
||||||
|
|
@ -26,11 +26,11 @@ class Variable(VariableBase, table=True):
|
||||||
)
|
)
|
||||||
# name is unique per user
|
# name is unique per user
|
||||||
created_at: datetime = Field(
|
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",
|
description="Creation time of the variable",
|
||||||
)
|
)
|
||||||
updated_at: Optional[datetime] = Field(
|
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",
|
description="Last update time of the variable",
|
||||||
)
|
)
|
||||||
# foreign key to user table
|
# foreign key to user table
|
||||||
|
|
@ -39,7 +39,9 @@ class Variable(VariableBase, table=True):
|
||||||
|
|
||||||
|
|
||||||
class VariableCreate(VariableBase):
|
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):
|
class VariableRead(SQLModel):
|
||||||
|
|
|
||||||
|
|
@ -3,13 +3,14 @@ from typing import TYPE_CHECKING, Optional, Union
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
from fastapi import Depends
|
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 loguru import logger
|
||||||
from sqlmodel import Session, select
|
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:
|
if TYPE_CHECKING:
|
||||||
from langflow.services.settings.service import SettingsService
|
from langflow.services.settings.service import SettingsService
|
||||||
|
|
||||||
|
|
@ -93,14 +94,13 @@ class VariableService(Service):
|
||||||
_type: str = "Generic",
|
_type: str = "Generic",
|
||||||
session: Session = Depends(get_session),
|
session: Session = Depends(get_session),
|
||||||
):
|
):
|
||||||
variable = Variable(
|
variable_base = VariableCreate(
|
||||||
user_id=user_id,
|
|
||||||
name=name,
|
name=name,
|
||||||
type=_type,
|
type=_type,
|
||||||
value=auth_utils.encrypt_api_key(value, settings_service=self.settings_service),
|
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.add(variable)
|
||||||
session.commit()
|
session.commit()
|
||||||
session.refresh(variable)
|
session.refresh(variable)
|
||||||
return variable
|
return variable
|
||||||
return variable
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue