From bf57dd808e7e4be729eca565fb201595be95aeab Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 1 Oct 2024 17:35:10 -0700 Subject: [PATCH] feat: project migration --- backend/open_webui/config.py | 3 - backend/open_webui/migrations/util.py | 3 +- .../6a39f3d8e55c_add_project_table.py | 74 +++++++++++++++++++ 3 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 backend/open_webui/migrations/versions/6a39f3d8e55c_add_project_table.py diff --git a/backend/open_webui/config.py b/backend/open_webui/config.py index 3c28ab01f..bd02c917c 100644 --- a/backend/open_webui/config.py +++ b/backend/open_webui/config.py @@ -56,9 +56,6 @@ def run_migrations(): print(f"Error: {e}") -run_migrations() - - class Config(Base): __tablename__ = "config" diff --git a/backend/open_webui/migrations/util.py b/backend/open_webui/migrations/util.py index d4cc00a68..955066602 100644 --- a/backend/open_webui/migrations/util.py +++ b/backend/open_webui/migrations/util.py @@ -1,6 +1,5 @@ from alembic import op from sqlalchemy import Inspector -import uuid def get_existing_tables(): @@ -11,4 +10,6 @@ def get_existing_tables(): def get_revision_id(): + import uuid + return str(uuid.uuid4()).replace("-", "")[:12] diff --git a/backend/open_webui/migrations/versions/6a39f3d8e55c_add_project_table.py b/backend/open_webui/migrations/versions/6a39f3d8e55c_add_project_table.py new file mode 100644 index 000000000..c6c42f64b --- /dev/null +++ b/backend/open_webui/migrations/versions/6a39f3d8e55c_add_project_table.py @@ -0,0 +1,74 @@ +"""Add project table + +Revision ID: 6a39f3d8e55c +Revises: c0fbf31ca0db +Create Date: 2024-10-01 14:02:35.241684 + +""" + +from alembic import op +import sqlalchemy as sa +from sqlalchemy.sql import table, column, select + +revision = "6a39f3d8e55c" +down_revision = "c0fbf31ca0db" +branch_labels = None +depends_on = None + + +def upgrade(): + # Creating the 'project' table + print("Creating project table") + project_table = op.create_table( + "project", + sa.Column("id", sa.Text(), primary_key=True), + sa.Column("user_id", sa.Text(), nullable=False), + sa.Column("name", sa.Text(), nullable=False), + sa.Column("description", sa.Text(), nullable=True), + sa.Column("data", sa.JSON(), nullable=True), + sa.Column("meta", sa.JSON(), nullable=True), + sa.Column("created_at", sa.BigInteger(), nullable=False), + sa.Column("updated_at", sa.BigInteger(), nullable=True), + ) + + print("Migrating data from document table to project table") + # Representation of the existing 'document' table + document_table = table( + "document", + column("collection_name", sa.String()), + column("user_id", sa.String()), + column("name", sa.String()), + column("title", sa.Text()), + column("timestamp", sa.BigInteger()), + ) + + # Select all from existing document table + documents = op.get_bind().execute( + select( + document_table.c.collection_name, + document_table.c.user_id, + document_table.c.name, + document_table.c.title, + document_table.c.timestamp, + ) + ) + + # Insert data into project table from document table + for doc in documents: + op.get_bind().execute( + project_table.insert().values( + id=doc.collection_name, + user_id=doc.user_id, + description=doc.name, + meta={ + "legacy": True, + }, + name=doc.title, + created_at=doc.timestamp, + updated_at=doc.timestamp, # using created_at for both created_at and updated_at in project + ) + ) + + +def downgrade(): + op.drop_table("project")