From a9b148791d982b9635935a41ca6bdc3aa47165c3 Mon Sep 17 00:00:00 2001 From: Jonathan Rohde Date: Mon, 24 Jun 2024 13:21:51 +0200 Subject: [PATCH] feat(sqlalchemy): fix wrong column types --- backend/apps/webui/models/auths.py | 4 +- backend/apps/webui/models/chats.py | 8 +- backend/apps/webui/models/documents.py | 8 +- backend/apps/webui/models/files.py | 4 +- backend/apps/webui/models/memories.py | 4 +- backend/apps/webui/models/models.py | 10 +- backend/apps/webui/models/prompts.py | 6 +- backend/apps/webui/models/tags.py | 4 +- backend/apps/webui/models/tools.py | 6 +- backend/apps/webui/models/users.py | 2 +- .../migrations/versions/7e5b5dc7342b_init.py | 186 ++++++++++++++++++ .../migrations/versions/ba76b0bae648_init.py | 174 ---------------- 12 files changed, 214 insertions(+), 202 deletions(-) create mode 100644 backend/migrations/versions/7e5b5dc7342b_init.py delete mode 100644 backend/migrations/versions/ba76b0bae648_init.py diff --git a/backend/apps/webui/models/auths.py b/backend/apps/webui/models/auths.py index 1858b2c0d..aef895619 100644 --- a/backend/apps/webui/models/auths.py +++ b/backend/apps/webui/models/auths.py @@ -2,7 +2,7 @@ from pydantic import BaseModel from typing import Optional import uuid import logging -from sqlalchemy import String, Column, Boolean +from sqlalchemy import String, Column, Boolean, Text from apps.webui.models.users import UserModel, Users from utils.utils import verify_password @@ -24,7 +24,7 @@ class Auth(Base): id = Column(String, primary_key=True) email = Column(String) - password = Column(String) + password = Column(Text) active = Column(Boolean) diff --git a/backend/apps/webui/models/chats.py b/backend/apps/webui/models/chats.py index abf5b544c..1cf56c351 100644 --- a/backend/apps/webui/models/chats.py +++ b/backend/apps/webui/models/chats.py @@ -5,7 +5,7 @@ import json import uuid import time -from sqlalchemy import Column, String, BigInteger, Boolean +from sqlalchemy import Column, String, BigInteger, Boolean, Text from apps.webui.internal.db import Base, Session @@ -20,13 +20,13 @@ class Chat(Base): id = Column(String, primary_key=True) user_id = Column(String) - title = Column(String) - chat = Column(String) # Save Chat JSON as Text + title = Column(Text) + chat = Column(Text) # Save Chat JSON as Text created_at = Column(BigInteger) updated_at = Column(BigInteger) - share_id = Column(String, unique=True, nullable=True) + share_id = Column(Text, unique=True, nullable=True) archived = Column(Boolean, default=False) diff --git a/backend/apps/webui/models/documents.py b/backend/apps/webui/models/documents.py index f8e7153c5..1b69d44a5 100644 --- a/backend/apps/webui/models/documents.py +++ b/backend/apps/webui/models/documents.py @@ -3,7 +3,7 @@ from typing import List, Optional import time import logging -from sqlalchemy import String, Column, BigInteger +from sqlalchemy import String, Column, BigInteger, Text from apps.webui.internal.db import Base, Session @@ -24,9 +24,9 @@ class Document(Base): collection_name = Column(String, primary_key=True) name = Column(String, unique=True) - title = Column(String) - filename = Column(String) - content = Column(String, nullable=True) + title = Column(Text) + filename = Column(Text) + content = Column(Text, nullable=True) user_id = Column(String) timestamp = Column(BigInteger) diff --git a/backend/apps/webui/models/files.py b/backend/apps/webui/models/files.py index 7664bf4f1..ce904215d 100644 --- a/backend/apps/webui/models/files.py +++ b/backend/apps/webui/models/files.py @@ -3,7 +3,7 @@ from typing import List, Union, Optional import time import logging -from sqlalchemy import Column, String, BigInteger +from sqlalchemy import Column, String, BigInteger, Text from apps.webui.internal.db import JSONField, Base, Session @@ -24,7 +24,7 @@ class File(Base): id = Column(String, primary_key=True) user_id = Column(String) - filename = Column(String) + filename = Column(Text) meta = Column(JSONField) created_at = Column(BigInteger) diff --git a/backend/apps/webui/models/memories.py b/backend/apps/webui/models/memories.py index 263d1b5ab..f0bd6e291 100644 --- a/backend/apps/webui/models/memories.py +++ b/backend/apps/webui/models/memories.py @@ -1,7 +1,7 @@ from pydantic import BaseModel, ConfigDict from typing import List, Union, Optional -from sqlalchemy import Column, String, BigInteger +from sqlalchemy import Column, String, BigInteger, Text from apps.webui.internal.db import Base, Session @@ -18,7 +18,7 @@ class Memory(Base): id = Column(String, primary_key=True) user_id = Column(String) - content = Column(String) + content = Column(Text) updated_at = Column(BigInteger) created_at = Column(BigInteger) diff --git a/backend/apps/webui/models/models.py b/backend/apps/webui/models/models.py index dd736a73e..7d1da54ff 100644 --- a/backend/apps/webui/models/models.py +++ b/backend/apps/webui/models/models.py @@ -3,7 +3,7 @@ import logging from typing import Optional from pydantic import BaseModel, ConfigDict -from sqlalchemy import String, Column, BigInteger +from sqlalchemy import String, Column, BigInteger, Text from apps.webui.internal.db import Base, JSONField, Session @@ -46,18 +46,18 @@ class ModelMeta(BaseModel): class Model(Base): __tablename__ = "model" - id = Column(String, primary_key=True) + id = Column(Text, primary_key=True) """ The model's id as used in the API. If set to an existing model, it will override the model. """ - user_id = Column(String) + user_id = Column(Text) - base_model_id = Column(String, nullable=True) + base_model_id = Column(Text, nullable=True) """ An optional pointer to the actual model that should be used when proxying requests. """ - name = Column(String) + name = Column(Text) """ The human-readable display name of the model. """ diff --git a/backend/apps/webui/models/prompts.py b/backend/apps/webui/models/prompts.py index a2fd0366b..ab8cc04ce 100644 --- a/backend/apps/webui/models/prompts.py +++ b/backend/apps/webui/models/prompts.py @@ -2,7 +2,7 @@ from pydantic import BaseModel, ConfigDict from typing import List, Optional import time -from sqlalchemy import String, Column, BigInteger +from sqlalchemy import String, Column, BigInteger, Text from apps.webui.internal.db import Base, Session @@ -18,8 +18,8 @@ class Prompt(Base): command = Column(String, primary_key=True) user_id = Column(String) - title = Column(String) - content = Column(String) + title = Column(Text) + content = Column(Text) timestamp = Column(BigInteger) diff --git a/backend/apps/webui/models/tags.py b/backend/apps/webui/models/tags.py index 6cfe39d0c..87238c2a3 100644 --- a/backend/apps/webui/models/tags.py +++ b/backend/apps/webui/models/tags.py @@ -6,7 +6,7 @@ import uuid import time import logging -from sqlalchemy import String, Column, BigInteger +from sqlalchemy import String, Column, BigInteger, Text from apps.webui.internal.db import Base, Session @@ -26,7 +26,7 @@ class Tag(Base): id = Column(String, primary_key=True) name = Column(String) user_id = Column(String) - data = Column(String, nullable=True) + data = Column(Text, nullable=True) class ChatIdTag(Base): diff --git a/backend/apps/webui/models/tools.py b/backend/apps/webui/models/tools.py index 20c608921..f5df10637 100644 --- a/backend/apps/webui/models/tools.py +++ b/backend/apps/webui/models/tools.py @@ -2,7 +2,7 @@ from pydantic import BaseModel, ConfigDict from typing import List, Optional import time import logging -from sqlalchemy import String, Column, BigInteger +from sqlalchemy import String, Column, BigInteger, Text from apps.webui.internal.db import Base, JSONField, Session from apps.webui.models.users import Users @@ -26,8 +26,8 @@ class Tool(Base): id = Column(String, primary_key=True) user_id = Column(String) - name = Column(String) - content = Column(String) + name = Column(Text) + content = Column(Text) specs = Column(JSONField) meta = Column(JSONField) valves = Column(JSONField) diff --git a/backend/apps/webui/models/users.py b/backend/apps/webui/models/users.py index 252e3f122..e1c5ca9f3 100644 --- a/backend/apps/webui/models/users.py +++ b/backend/apps/webui/models/users.py @@ -21,7 +21,7 @@ class User(Base): name = Column(String) email = Column(String) role = Column(String) - profile_image_url = Column(String) + profile_image_url = Column(Text) last_active_at = Column(BigInteger) updated_at = Column(BigInteger) diff --git a/backend/migrations/versions/7e5b5dc7342b_init.py b/backend/migrations/versions/7e5b5dc7342b_init.py new file mode 100644 index 000000000..bd49d1b43 --- /dev/null +++ b/backend/migrations/versions/7e5b5dc7342b_init.py @@ -0,0 +1,186 @@ +"""init + +Revision ID: 7e5b5dc7342b +Revises: +Create Date: 2024-06-24 13:15:33.808998 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa +import apps.webui.internal.db +from migrations.util import get_existing_tables + +# revision identifiers, used by Alembic. +revision: str = '7e5b5dc7342b' +down_revision: Union[str, None] = None +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + existing_tables = set(get_existing_tables()) + + # ### commands auto generated by Alembic - please adjust! ### + if "auth" not in existing_tables: + op.create_table('auth', + sa.Column('id', sa.String(), nullable=False), + sa.Column('email', sa.String(), nullable=True), + sa.Column('password', sa.Text(), nullable=True), + sa.Column('active', sa.Boolean(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + + if "chat" not in existing_tables: + op.create_table('chat', + sa.Column('id', sa.String(), nullable=False), + sa.Column('user_id', sa.String(), nullable=True), + sa.Column('title', sa.Text(), nullable=True), + sa.Column('chat', sa.Text(), nullable=True), + sa.Column('created_at', sa.BigInteger(), nullable=True), + sa.Column('updated_at', sa.BigInteger(), nullable=True), + sa.Column('share_id', sa.Text(), nullable=True), + sa.Column('archived', sa.Boolean(), nullable=True), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('share_id') + ) + + if "chatidtag" not in existing_tables: + op.create_table('chatidtag', + sa.Column('id', sa.String(), nullable=False), + sa.Column('tag_name', sa.String(), nullable=True), + sa.Column('chat_id', sa.String(), nullable=True), + sa.Column('user_id', sa.String(), nullable=True), + sa.Column('timestamp', sa.BigInteger(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + + if "document" not in existing_tables: + op.create_table('document', + sa.Column('collection_name', sa.String(), nullable=False), + sa.Column('name', sa.String(), nullable=True), + sa.Column('title', sa.Text(), nullable=True), + sa.Column('filename', sa.Text(), nullable=True), + sa.Column('content', sa.Text(), nullable=True), + sa.Column('user_id', sa.String(), nullable=True), + sa.Column('timestamp', sa.BigInteger(), nullable=True), + sa.PrimaryKeyConstraint('collection_name'), + sa.UniqueConstraint('name') + ) + + if "file" not in existing_tables: + op.create_table('file', + sa.Column('id', sa.String(), nullable=False), + sa.Column('user_id', sa.String(), nullable=True), + sa.Column('filename', sa.Text(), nullable=True), + sa.Column('meta', apps.webui.internal.db.JSONField(), nullable=True), + sa.Column('created_at', sa.BigInteger(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + + if "function" not in existing_tables: + op.create_table('function', + sa.Column('id', sa.String(), nullable=False), + sa.Column('user_id', sa.String(), nullable=True), + sa.Column('name', sa.Text(), nullable=True), + sa.Column('type', sa.Text(), nullable=True), + sa.Column('content', sa.Text(), nullable=True), + sa.Column('meta', apps.webui.internal.db.JSONField(), nullable=True), + sa.Column('valves', apps.webui.internal.db.JSONField(), nullable=True), + sa.Column('is_active', sa.Boolean(), nullable=True), + sa.Column('updated_at', sa.BigInteger(), nullable=True), + sa.Column('created_at', sa.BigInteger(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + + if "memory" not in existing_tables: + op.create_table('memory', + sa.Column('id', sa.String(), nullable=False), + sa.Column('user_id', sa.String(), nullable=True), + sa.Column('content', sa.Text(), nullable=True), + sa.Column('updated_at', sa.BigInteger(), nullable=True), + sa.Column('created_at', sa.BigInteger(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + + if "model" not in existing_tables: + op.create_table('model', + sa.Column('id', sa.Text(), nullable=False), + sa.Column('user_id', sa.Text(), nullable=True), + sa.Column('base_model_id', sa.Text(), nullable=True), + sa.Column('name', sa.Text(), nullable=True), + sa.Column('params', apps.webui.internal.db.JSONField(), nullable=True), + sa.Column('meta', apps.webui.internal.db.JSONField(), nullable=True), + sa.Column('updated_at', sa.BigInteger(), nullable=True), + sa.Column('created_at', sa.BigInteger(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + + if "prompt" not in existing_tables: + op.create_table('prompt', + sa.Column('command', sa.String(), nullable=False), + sa.Column('user_id', sa.String(), nullable=True), + sa.Column('title', sa.Text(), nullable=True), + sa.Column('content', sa.Text(), nullable=True), + sa.Column('timestamp', sa.BigInteger(), nullable=True), + sa.PrimaryKeyConstraint('command') + ) + + if "tag" not in existing_tables: + op.create_table('tag', + sa.Column('id', sa.String(), nullable=False), + sa.Column('name', sa.String(), nullable=True), + sa.Column('user_id', sa.String(), nullable=True), + sa.Column('data', sa.Text(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + + if "tool" not in existing_tables: + op.create_table('tool', + sa.Column('id', sa.String(), nullable=False), + sa.Column('user_id', sa.String(), nullable=True), + sa.Column('name', sa.Text(), nullable=True), + sa.Column('content', sa.Text(), nullable=True), + sa.Column('specs', apps.webui.internal.db.JSONField(), nullable=True), + sa.Column('meta', apps.webui.internal.db.JSONField(), nullable=True), + sa.Column('valves', apps.webui.internal.db.JSONField(), nullable=True), + sa.Column('updated_at', sa.BigInteger(), nullable=True), + sa.Column('created_at', sa.BigInteger(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + + if "user" not in existing_tables: + op.create_table('user', + sa.Column('id', sa.String(), nullable=False), + sa.Column('name', sa.String(), nullable=True), + sa.Column('email', sa.String(), nullable=True), + sa.Column('role', sa.String(), nullable=True), + sa.Column('profile_image_url', sa.Text(), nullable=True), + sa.Column('last_active_at', sa.BigInteger(), nullable=True), + sa.Column('updated_at', sa.BigInteger(), nullable=True), + sa.Column('created_at', sa.BigInteger(), nullable=True), + sa.Column('api_key', sa.String(), nullable=True), + sa.Column('settings', apps.webui.internal.db.JSONField(), nullable=True), + sa.Column('info', apps.webui.internal.db.JSONField(), nullable=True), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('api_key') + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('user') + op.drop_table('tool') + op.drop_table('tag') + op.drop_table('prompt') + op.drop_table('model') + op.drop_table('memory') + op.drop_table('function') + op.drop_table('file') + op.drop_table('document') + op.drop_table('chatidtag') + op.drop_table('chat') + op.drop_table('auth') + # ### end Alembic commands ### diff --git a/backend/migrations/versions/ba76b0bae648_init.py b/backend/migrations/versions/ba76b0bae648_init.py deleted file mode 100644 index c491ed46c..000000000 --- a/backend/migrations/versions/ba76b0bae648_init.py +++ /dev/null @@ -1,174 +0,0 @@ -"""init - -Revision ID: ba76b0bae648 -Revises: -Create Date: 2024-06-24 09:09:11.636336 - -""" - -from typing import Sequence, Union - -from alembic import op -import sqlalchemy as sa -import apps.webui.internal.db - - -# revision identifiers, used by Alembic. -revision: str = "ba76b0bae648" -down_revision: Union[str, None] = None -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.create_table( - "auth", - sa.Column("id", sa.String(), nullable=False), - sa.Column("email", sa.String(), nullable=True), - sa.Column("password", sa.String(), nullable=True), - sa.Column("active", sa.Boolean(), nullable=True), - sa.PrimaryKeyConstraint("id"), - ) - op.create_table( - "chat", - sa.Column("id", sa.String(), nullable=False), - sa.Column("user_id", sa.String(), nullable=True), - sa.Column("title", sa.String(), nullable=True), - sa.Column("chat", sa.String(), nullable=True), - sa.Column("created_at", sa.BigInteger(), nullable=True), - sa.Column("updated_at", sa.BigInteger(), nullable=True), - sa.Column("share_id", sa.String(), nullable=True), - sa.Column("archived", sa.Boolean(), nullable=True), - sa.PrimaryKeyConstraint("id"), - sa.UniqueConstraint("share_id"), - ) - op.create_table( - "chatidtag", - sa.Column("id", sa.String(), nullable=False), - sa.Column("tag_name", sa.String(), nullable=True), - sa.Column("chat_id", sa.String(), nullable=True), - sa.Column("user_id", sa.String(), nullable=True), - sa.Column("timestamp", sa.BigInteger(), nullable=True), - sa.PrimaryKeyConstraint("id"), - ) - op.create_table( - "document", - sa.Column("collection_name", sa.String(), nullable=False), - sa.Column("name", sa.String(), nullable=True), - sa.Column("title", sa.String(), nullable=True), - sa.Column("filename", sa.String(), nullable=True), - sa.Column("content", sa.String(), nullable=True), - sa.Column("user_id", sa.String(), nullable=True), - sa.Column("timestamp", sa.BigInteger(), nullable=True), - sa.PrimaryKeyConstraint("collection_name"), - sa.UniqueConstraint("name"), - ) - op.create_table( - "file", - sa.Column("id", sa.String(), nullable=False), - sa.Column("user_id", sa.String(), nullable=True), - sa.Column("filename", sa.String(), nullable=True), - sa.Column("meta", apps.webui.internal.db.JSONField(), nullable=True), - sa.Column("created_at", sa.BigInteger(), nullable=True), - sa.PrimaryKeyConstraint("id"), - ) - op.create_table( - "function", - sa.Column("id", sa.String(), nullable=False), - sa.Column("user_id", sa.String(), nullable=True), - sa.Column("name", sa.Text(), nullable=True), - sa.Column("type", sa.Text(), nullable=True), - sa.Column("content", sa.Text(), nullable=True), - sa.Column("meta", apps.webui.internal.db.JSONField(), nullable=True), - sa.Column("valves", apps.webui.internal.db.JSONField(), nullable=True), - sa.Column("is_active", sa.Boolean(), nullable=True), - sa.Column("updated_at", sa.BigInteger(), nullable=True), - sa.Column("created_at", sa.BigInteger(), nullable=True), - sa.PrimaryKeyConstraint("id"), - ) - op.create_table( - "memory", - sa.Column("id", sa.String(), nullable=False), - sa.Column("user_id", sa.String(), nullable=True), - sa.Column("content", sa.String(), nullable=True), - sa.Column("updated_at", sa.BigInteger(), nullable=True), - sa.Column("created_at", sa.BigInteger(), nullable=True), - sa.PrimaryKeyConstraint("id"), - ) - op.create_table( - "model", - sa.Column("id", sa.String(), nullable=False), - sa.Column("user_id", sa.String(), nullable=True), - sa.Column("base_model_id", sa.String(), nullable=True), - sa.Column("name", sa.String(), nullable=True), - sa.Column("params", apps.webui.internal.db.JSONField(), nullable=True), - sa.Column("meta", apps.webui.internal.db.JSONField(), nullable=True), - sa.Column("updated_at", sa.BigInteger(), nullable=True), - sa.Column("created_at", sa.BigInteger(), nullable=True), - sa.PrimaryKeyConstraint("id"), - ) - op.create_table( - "prompt", - sa.Column("command", sa.String(), nullable=False), - sa.Column("user_id", sa.String(), nullable=True), - sa.Column("title", sa.String(), nullable=True), - sa.Column("content", sa.String(), nullable=True), - sa.Column("timestamp", sa.BigInteger(), nullable=True), - sa.PrimaryKeyConstraint("command"), - ) - op.create_table( - "tag", - sa.Column("id", sa.String(), nullable=False), - sa.Column("name", sa.String(), nullable=True), - sa.Column("user_id", sa.String(), nullable=True), - sa.Column("data", sa.String(), nullable=True), - sa.PrimaryKeyConstraint("id"), - ) - op.create_table( - "tool", - sa.Column("id", sa.String(), nullable=False), - sa.Column("user_id", sa.String(), nullable=True), - sa.Column("name", sa.String(), nullable=True), - sa.Column("content", sa.String(), nullable=True), - sa.Column("specs", apps.webui.internal.db.JSONField(), nullable=True), - sa.Column("meta", apps.webui.internal.db.JSONField(), nullable=True), - sa.Column("valves", apps.webui.internal.db.JSONField(), nullable=True), - sa.Column("updated_at", sa.BigInteger(), nullable=True), - sa.Column("created_at", sa.BigInteger(), nullable=True), - sa.PrimaryKeyConstraint("id"), - ) - op.create_table( - "user", - sa.Column("id", sa.String(), nullable=False), - sa.Column("name", sa.String(), nullable=True), - sa.Column("email", sa.String(), nullable=True), - sa.Column("role", sa.String(), nullable=True), - sa.Column("profile_image_url", sa.String(), nullable=True), - sa.Column("last_active_at", sa.BigInteger(), nullable=True), - sa.Column("updated_at", sa.BigInteger(), nullable=True), - sa.Column("created_at", sa.BigInteger(), nullable=True), - sa.Column("api_key", sa.String(), nullable=True), - sa.Column("settings", apps.webui.internal.db.JSONField(), nullable=True), - sa.Column("info", apps.webui.internal.db.JSONField(), nullable=True), - sa.PrimaryKeyConstraint("id"), - sa.UniqueConstraint("api_key"), - ) - # ### end Alembic commands ### - - -def downgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - op.drop_table("user") - op.drop_table("tool") - op.drop_table("tag") - op.drop_table("prompt") - op.drop_table("model") - op.drop_table("memory") - op.drop_table("function") - op.drop_table("file") - op.drop_table("document") - op.drop_table("chatidtag") - op.drop_table("chat") - op.drop_table("auth") - # ### end Alembic commands ###