init. project
This commit is contained in:
90
rag-web-ui/backend/alembic/env.py
Normal file
90
rag-web-ui/backend/alembic/env.py
Normal file
@@ -0,0 +1,90 @@
|
||||
import os
|
||||
import sys
|
||||
from logging.config import fileConfig
|
||||
from sqlalchemy import engine_from_config
|
||||
from sqlalchemy import pool
|
||||
from alembic import context
|
||||
|
||||
# Add the parent directory to Python path
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from app.models.base import Base
|
||||
from app.models.user import User
|
||||
from app.models.knowledge import KnowledgeBase, Document
|
||||
from app.models.chat import Chat, Message
|
||||
from app.models.tooling import ToolJob, SRSExtraction, SRSRequirement
|
||||
from app.core.config import settings
|
||||
|
||||
# this is the Alembic Config object, which provides
|
||||
# access to the values within the .ini file in use.
|
||||
config = context.config
|
||||
|
||||
# Interpret the config file for Python logging.
|
||||
# This line sets up loggers basically.
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
# add your model's MetaData object here
|
||||
# for 'autogenerate' support
|
||||
target_metadata = Base.metadata
|
||||
|
||||
# other values from the config, defined by the needs of env.py,
|
||||
# can be acquired:
|
||||
# my_important_option = config.get_main_option("my_important_option")
|
||||
# ... etc.
|
||||
|
||||
def get_url():
|
||||
return settings.get_database_url
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
"""Run migrations in 'offline' mode.
|
||||
|
||||
This configures the context with just a URL
|
||||
and not an Engine, though an Engine is acceptable
|
||||
here as well. By skipping the Engine creation
|
||||
we don't even need a DBAPI to be available.
|
||||
|
||||
Calls to context.execute() here emit the given string to the
|
||||
script output.
|
||||
|
||||
"""
|
||||
url = get_url()
|
||||
context.configure(
|
||||
url=url,
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
"""Run migrations in 'online' mode.
|
||||
|
||||
In this scenario we need to create an Engine
|
||||
and associate a connection with the context.
|
||||
|
||||
"""
|
||||
configuration = config.get_section(config.config_ini_section)
|
||||
configuration["sqlalchemy.url"] = get_url()
|
||||
connectable = engine_from_config(
|
||||
configuration,
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
|
||||
with connectable.connect() as connection:
|
||||
context.configure(
|
||||
connection=connection, target_metadata=target_metadata
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
26
rag-web-ui/backend/alembic/script.py.mako
Normal file
26
rag-web-ui/backend/alembic/script.py.mako
Normal file
@@ -0,0 +1,26 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = ${repr(up_revision)}
|
||||
down_revision: Union[str, None] = ${repr(down_revision)}
|
||||
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
|
||||
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
${downgrades if downgrades else "pass"}
|
||||
@@ -0,0 +1,36 @@
|
||||
"""increase_api_key_length
|
||||
|
||||
Revision ID: 3580c0dcd005
|
||||
Revises: e214adf7fb66
|
||||
Create Date: 2024-01-20 14:25:00.000000
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '3580c0dcd005'
|
||||
down_revision: Union[str, None] = 'e214adf7fb66'
|
||||
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.alter_column('api_keys', 'key',
|
||||
existing_type=sa.String(length=64),
|
||||
type_=sa.String(length=128),
|
||||
existing_nullable=False)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.alter_column('api_keys', 'key',
|
||||
existing_type=sa.String(length=128),
|
||||
type_=sa.String(length=64),
|
||||
existing_nullable=False)
|
||||
# ### end Alembic commands ###
|
||||
@@ -0,0 +1,116 @@
|
||||
"""rename_metadata_to_chunk_metadata
|
||||
|
||||
Revision ID: 59cfa0f1361d
|
||||
Revises: initial_schema
|
||||
Create Date: 2025-01-13 23:26:38.232326
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects import mysql
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '59cfa0f1361d'
|
||||
down_revision: Union[str, None] = 'initial_schema'
|
||||
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_index(op.f('ix_chats_id'), 'chats', ['id'], unique=False)
|
||||
op.add_column('document_chunks', sa.Column('document_id', sa.Integer(), nullable=False))
|
||||
op.add_column('document_chunks', sa.Column('chunk_metadata', sa.JSON(), nullable=True))
|
||||
op.alter_column('document_chunks', 'created_at',
|
||||
existing_type=mysql.TIMESTAMP(),
|
||||
type_=sa.DateTime(),
|
||||
nullable=False,
|
||||
existing_server_default=sa.text('CURRENT_TIMESTAMP'))
|
||||
op.alter_column('document_chunks', 'updated_at',
|
||||
existing_type=mysql.TIMESTAMP(),
|
||||
type_=sa.DateTime(),
|
||||
nullable=False,
|
||||
existing_server_default=sa.text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'))
|
||||
op.drop_index('idx_hash', table_name='document_chunks')
|
||||
op.create_index(op.f('ix_document_chunks_hash'), 'document_chunks', ['hash'], unique=False)
|
||||
op.create_foreign_key(None, 'document_chunks', 'knowledge_bases', ['kb_id'], ['id'])
|
||||
op.create_foreign_key(None, 'document_chunks', 'documents', ['document_id'], ['id'])
|
||||
op.drop_column('document_chunks', 'metadata')
|
||||
op.drop_index('idx_file_hash', table_name='documents')
|
||||
op.create_index(op.f('ix_documents_file_hash'), 'documents', ['file_hash'], unique=False)
|
||||
op.create_index(op.f('ix_documents_id'), 'documents', ['id'], unique=False)
|
||||
op.create_index(op.f('ix_knowledge_bases_id'), 'knowledge_bases', ['id'], unique=False)
|
||||
op.create_index(op.f('ix_messages_id'), 'messages', ['id'], unique=False)
|
||||
op.alter_column('processing_tasks', 'knowledge_base_id',
|
||||
existing_type=mysql.INTEGER(),
|
||||
nullable=True)
|
||||
op.alter_column('processing_tasks', 'document_id',
|
||||
existing_type=mysql.INTEGER(),
|
||||
nullable=True)
|
||||
op.alter_column('processing_tasks', 'status',
|
||||
existing_type=mysql.VARCHAR(length=50),
|
||||
nullable=True)
|
||||
op.alter_column('processing_tasks', 'created_at',
|
||||
existing_type=mysql.DATETIME(),
|
||||
nullable=True)
|
||||
op.alter_column('processing_tasks', 'updated_at',
|
||||
existing_type=mysql.DATETIME(),
|
||||
nullable=True)
|
||||
op.create_index(op.f('ix_processing_tasks_id'), 'processing_tasks', ['id'], unique=False)
|
||||
op.drop_index('email', table_name='users')
|
||||
op.drop_index('username', table_name='users')
|
||||
op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True)
|
||||
op.create_index(op.f('ix_users_id'), 'users', ['id'], unique=False)
|
||||
op.create_index(op.f('ix_users_username'), 'users', ['username'], unique=True)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_index(op.f('ix_users_username'), table_name='users')
|
||||
op.drop_index(op.f('ix_users_id'), table_name='users')
|
||||
op.drop_index(op.f('ix_users_email'), table_name='users')
|
||||
op.create_index('username', 'users', ['username'], unique=True)
|
||||
op.create_index('email', 'users', ['email'], unique=True)
|
||||
op.drop_index(op.f('ix_processing_tasks_id'), table_name='processing_tasks')
|
||||
op.alter_column('processing_tasks', 'updated_at',
|
||||
existing_type=mysql.DATETIME(),
|
||||
nullable=False)
|
||||
op.alter_column('processing_tasks', 'created_at',
|
||||
existing_type=mysql.DATETIME(),
|
||||
nullable=False)
|
||||
op.alter_column('processing_tasks', 'status',
|
||||
existing_type=mysql.VARCHAR(length=50),
|
||||
nullable=False)
|
||||
op.alter_column('processing_tasks', 'document_id',
|
||||
existing_type=mysql.INTEGER(),
|
||||
nullable=False)
|
||||
op.alter_column('processing_tasks', 'knowledge_base_id',
|
||||
existing_type=mysql.INTEGER(),
|
||||
nullable=False)
|
||||
op.drop_index(op.f('ix_messages_id'), table_name='messages')
|
||||
op.drop_index(op.f('ix_knowledge_bases_id'), table_name='knowledge_bases')
|
||||
op.drop_index(op.f('ix_documents_id'), table_name='documents')
|
||||
op.drop_index(op.f('ix_documents_file_hash'), table_name='documents')
|
||||
op.create_index('idx_file_hash', 'documents', ['file_hash'], unique=False)
|
||||
op.add_column('document_chunks', sa.Column('metadata', mysql.JSON(), nullable=True))
|
||||
op.drop_constraint(None, 'document_chunks', type_='foreignkey')
|
||||
op.drop_constraint(None, 'document_chunks', type_='foreignkey')
|
||||
op.drop_index(op.f('ix_document_chunks_hash'), table_name='document_chunks')
|
||||
op.create_index('idx_hash', 'document_chunks', ['hash'], unique=False)
|
||||
op.alter_column('document_chunks', 'updated_at',
|
||||
existing_type=sa.DateTime(),
|
||||
type_=mysql.TIMESTAMP(),
|
||||
nullable=True,
|
||||
existing_server_default=sa.text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'))
|
||||
op.alter_column('document_chunks', 'created_at',
|
||||
existing_type=sa.DateTime(),
|
||||
type_=mysql.TIMESTAMP(),
|
||||
nullable=True,
|
||||
existing_server_default=sa.text('CURRENT_TIMESTAMP'))
|
||||
op.drop_column('document_chunks', 'chunk_metadata')
|
||||
op.drop_column('document_chunks', 'document_id')
|
||||
op.drop_index(op.f('ix_chats_id'), table_name='chats')
|
||||
# ### end Alembic commands ###
|
||||
@@ -0,0 +1,37 @@
|
||||
"""add_document_upload_id_to_processing_tasks
|
||||
|
||||
Revision ID: 5be054bd6587
|
||||
Revises: fd73eebc87c1
|
||||
Create Date: 2025-01-14 01:17:24.164593
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '5be054bd6587'
|
||||
down_revision: Union[str, None] = 'fd73eebc87c1'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# 1. 添加 document_upload_id 字段
|
||||
op.execute("""
|
||||
ALTER TABLE processing_tasks
|
||||
ADD COLUMN document_upload_id INT,
|
||||
ADD CONSTRAINT processing_tasks_document_upload_id_fkey
|
||||
FOREIGN KEY (document_upload_id) REFERENCES document_uploads(id)
|
||||
""")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# 1. 删除外键约束和字段
|
||||
op.execute("""
|
||||
ALTER TABLE processing_tasks
|
||||
DROP FOREIGN KEY processing_tasks_document_upload_id_fkey,
|
||||
DROP COLUMN document_upload_id
|
||||
""")
|
||||
@@ -0,0 +1,102 @@
|
||||
"""add tool jobs and srs tables
|
||||
|
||||
Revision ID: a4f9c89b7d11
|
||||
Revises: 3580c0dcd005
|
||||
Create Date: 2026-04-12 18:00:00.000000
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects import mysql
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = "a4f9c89b7d11"
|
||||
down_revision: Union[str, None] = "3580c0dcd005"
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
"tool_jobs",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("user_id", sa.Integer(), nullable=False),
|
||||
sa.Column("tool_name", sa.String(length=128), nullable=False),
|
||||
sa.Column("status", sa.String(length=32), nullable=False, server_default="pending"),
|
||||
sa.Column("input_file_name", sa.String(length=255), nullable=False),
|
||||
sa.Column("input_file_path", sa.String(length=512), nullable=False),
|
||||
sa.Column("error_message", sa.Text(), nullable=True),
|
||||
sa.Column("started_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("completed_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("output_summary", sa.JSON(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(["user_id"], ["users.id"]),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
op.create_index(op.f("ix_tool_jobs_id"), "tool_jobs", ["id"], unique=False)
|
||||
op.create_index(op.f("ix_tool_jobs_tool_name"), "tool_jobs", ["tool_name"], unique=False)
|
||||
op.create_index(op.f("ix_tool_jobs_user_id"), "tool_jobs", ["user_id"], unique=False)
|
||||
|
||||
op.create_table(
|
||||
"srs_extractions",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("job_id", sa.Integer(), nullable=False),
|
||||
sa.Column("document_name", sa.String(length=255), nullable=False),
|
||||
sa.Column("document_title", sa.String(length=255), nullable=False),
|
||||
sa.Column("generated_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("total_requirements", sa.Integer(), nullable=False),
|
||||
sa.Column("statistics", sa.JSON(), nullable=True),
|
||||
sa.Column("raw_output", sa.JSON(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(["job_id"], ["tool_jobs.id"], ondelete="CASCADE"),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("job_id"),
|
||||
)
|
||||
op.create_index(op.f("ix_srs_extractions_id"), "srs_extractions", ["id"], unique=False)
|
||||
|
||||
op.create_table(
|
||||
"srs_requirements",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("extraction_id", sa.Integer(), nullable=False),
|
||||
sa.Column("requirement_uid", sa.String(length=64), nullable=False),
|
||||
sa.Column("title", sa.String(length=255), nullable=False),
|
||||
sa.Column("description", mysql.LONGTEXT(), nullable=False),
|
||||
sa.Column("priority", sa.String(length=16), nullable=False),
|
||||
sa.Column("acceptance_criteria", sa.JSON(), nullable=False),
|
||||
sa.Column("source_field", sa.String(length=255), nullable=False),
|
||||
sa.Column("section_number", sa.String(length=64), nullable=True),
|
||||
sa.Column("section_title", sa.String(length=255), nullable=True),
|
||||
sa.Column("requirement_type", sa.String(length=64), nullable=True),
|
||||
sa.Column("sort_order", sa.Integer(), nullable=False),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(["extraction_id"], ["srs_extractions.id"], ondelete="CASCADE"),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("extraction_id", "requirement_uid", name="uq_srs_extraction_requirement_uid"),
|
||||
)
|
||||
op.create_index(op.f("ix_srs_requirements_id"), "srs_requirements", ["id"], unique=False)
|
||||
op.create_index(
|
||||
"idx_srs_requirements_extraction_sort",
|
||||
"srs_requirements",
|
||||
["extraction_id", "sort_order"],
|
||||
unique=False,
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index("idx_srs_requirements_extraction_sort", table_name="srs_requirements")
|
||||
op.drop_index(op.f("ix_srs_requirements_id"), table_name="srs_requirements")
|
||||
op.drop_table("srs_requirements")
|
||||
|
||||
op.drop_index(op.f("ix_srs_extractions_id"), table_name="srs_extractions")
|
||||
op.drop_table("srs_extractions")
|
||||
|
||||
op.drop_index(op.f("ix_tool_jobs_user_id"), table_name="tool_jobs")
|
||||
op.drop_index(op.f("ix_tool_jobs_tool_name"), table_name="tool_jobs")
|
||||
op.drop_index(op.f("ix_tool_jobs_id"), table_name="tool_jobs")
|
||||
op.drop_table("tool_jobs")
|
||||
@@ -0,0 +1,49 @@
|
||||
"""add_api_keys_table
|
||||
|
||||
Revision ID: e214adf7fb66
|
||||
Revises: 5be054bd6587
|
||||
Create Date: 2024-01-20 13:24:00.000000
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = 'e214adf7fb66'
|
||||
down_revision: Union[str, None] = '5be054bd6587'
|
||||
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(
|
||||
'api_keys',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('key', sa.String(length=64), nullable=False),
|
||||
sa.Column('name', sa.String(length=255), nullable=False),
|
||||
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||
sa.Column('is_active', sa.Boolean(), nullable=False, default=True),
|
||||
sa.Column('last_used_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False, server_default=sa.text('CURRENT_TIMESTAMP')),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=False, server_default=sa.text('CURRENT_TIMESTAMP'), onupdate=sa.text('CURRENT_TIMESTAMP')),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('key')
|
||||
)
|
||||
op.create_index(op.f('ix_api_keys_id'), 'api_keys', ['id'], unique=False)
|
||||
op.create_index(op.f('ix_api_keys_key'), 'api_keys', ['key'], unique=True)
|
||||
op.create_index(op.f('ix_api_keys_name'), 'api_keys', ['name'], unique=False)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_index(op.f('ix_api_keys_name'), table_name='api_keys')
|
||||
op.drop_index(op.f('ix_api_keys_key'), table_name='api_keys')
|
||||
op.drop_index(op.f('ix_api_keys_id'), table_name='api_keys')
|
||||
op.drop_table('api_keys')
|
||||
# ### end Alembic commands ###
|
||||
@@ -0,0 +1,44 @@
|
||||
"""add document uploads table
|
||||
|
||||
Revision ID: fd73eebc87c1
|
||||
Revises: 59cfa0f1361d
|
||||
Create Date: 2024-01-13 16:24:07.182834
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'fd73eebc87c1'
|
||||
down_revision = '59cfa0f1361d'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
'document_uploads',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('knowledge_base_id', sa.Integer(), nullable=False),
|
||||
sa.Column('file_name', sa.String(255), nullable=False),
|
||||
sa.Column('file_hash', sa.String(64), nullable=False),
|
||||
sa.Column('file_size', sa.BigInteger(), nullable=False),
|
||||
sa.Column('content_type', sa.String(100), nullable=False),
|
||||
sa.Column('temp_path', sa.String(255), nullable=False),
|
||||
sa.Column('created_at', sa.TIMESTAMP(), server_default=sa.text('now()'), nullable=False),
|
||||
sa.Column('status', sa.String(50), nullable=False, server_default='pending'),
|
||||
sa.Column('error_message', sa.Text(), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.ForeignKeyConstraint(['knowledge_base_id'], ['knowledge_bases.id'], ondelete='CASCADE')
|
||||
)
|
||||
|
||||
# 添加索引以加速查询
|
||||
op.create_index('ix_document_uploads_created_at', 'document_uploads', ['created_at'])
|
||||
op.create_index('ix_document_uploads_status', 'document_uploads', ['status'])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index('ix_document_uploads_status')
|
||||
op.drop_index('ix_document_uploads_created_at')
|
||||
op.drop_table('document_uploads')
|
||||
148
rag-web-ui/backend/alembic/versions/initial_schema.py
Normal file
148
rag-web-ui/backend/alembic/versions/initial_schema.py
Normal file
@@ -0,0 +1,148 @@
|
||||
"""initial schema
|
||||
|
||||
Revision ID: initial_schema
|
||||
Revises:
|
||||
Create Date: 2024-01-13 15:00:00.000000
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects import mysql
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = 'initial_schema'
|
||||
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:
|
||||
# Create users table
|
||||
op.create_table(
|
||||
'users',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('email', sa.String(255), nullable=False),
|
||||
sa.Column('username', sa.String(255), nullable=False),
|
||||
sa.Column('hashed_password', sa.String(255), nullable=False),
|
||||
sa.Column('is_active', sa.Boolean(), nullable=True, default=True),
|
||||
sa.Column('is_superuser', sa.Boolean(), nullable=True, default=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('email'),
|
||||
sa.UniqueConstraint('username')
|
||||
)
|
||||
|
||||
# Create knowledge_bases table
|
||||
op.create_table(
|
||||
'knowledge_bases',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('name', sa.String(255), nullable=False),
|
||||
sa.Column('description', mysql.LONGTEXT(), nullable=True),
|
||||
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
|
||||
# Create documents table
|
||||
op.create_table(
|
||||
'documents',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('file_path', sa.String(255), nullable=False),
|
||||
sa.Column('file_name', sa.String(255), nullable=False),
|
||||
sa.Column('file_size', sa.Integer(), nullable=True),
|
||||
sa.Column('content_type', sa.String(100), nullable=True),
|
||||
sa.Column('file_hash', sa.String(64), nullable=True),
|
||||
sa.Column('knowledge_base_id', sa.Integer(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['knowledge_base_id'], ['knowledge_bases.id'], ),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('knowledge_base_id', 'file_name', name='uq_kb_file_name')
|
||||
)
|
||||
|
||||
# Create document_chunks table
|
||||
op.create_table(
|
||||
'document_chunks',
|
||||
sa.Column('id', sa.String(64), nullable=False),
|
||||
sa.Column('kb_id', sa.Integer(), nullable=False),
|
||||
sa.Column('file_name', sa.String(255), nullable=False),
|
||||
sa.Column('metadata', sa.JSON(), nullable=True),
|
||||
sa.Column('hash', sa.String(64), nullable=False),
|
||||
sa.Column('created_at', sa.TIMESTAMP(), server_default=sa.text('CURRENT_TIMESTAMP')),
|
||||
sa.Column('updated_at', sa.TIMESTAMP(), server_default=sa.text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
|
||||
# Create chats table
|
||||
op.create_table(
|
||||
'chats',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('title', sa.String(255), nullable=False),
|
||||
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
|
||||
# Create chat_knowledge_bases table (association table)
|
||||
op.create_table(
|
||||
'chat_knowledge_bases',
|
||||
sa.Column('chat_id', sa.Integer(), nullable=False),
|
||||
sa.Column('knowledge_base_id', sa.Integer(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['chat_id'], ['chats.id'], ),
|
||||
sa.ForeignKeyConstraint(['knowledge_base_id'], ['knowledge_bases.id'], ),
|
||||
sa.PrimaryKeyConstraint('chat_id', 'knowledge_base_id')
|
||||
)
|
||||
|
||||
# Create messages table
|
||||
op.create_table(
|
||||
'messages',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('chat_id', sa.Integer(), nullable=False),
|
||||
sa.Column('role', sa.String(50), nullable=False),
|
||||
sa.Column('content', mysql.LONGTEXT(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['chat_id'], ['chats.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
|
||||
# Create processing_tasks table
|
||||
op.create_table(
|
||||
'processing_tasks',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('knowledge_base_id', sa.Integer(), nullable=False),
|
||||
sa.Column('document_id', sa.Integer(), nullable=False),
|
||||
sa.Column('status', sa.String(50), nullable=False, default='pending'),
|
||||
sa.Column('error_message', sa.Text(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['document_id'], ['documents.id'], ),
|
||||
sa.ForeignKeyConstraint(['knowledge_base_id'], ['knowledge_bases.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
|
||||
# Create indexes
|
||||
op.create_index('idx_kb_file_name', 'document_chunks', ['kb_id', 'file_name'])
|
||||
op.create_index('idx_hash', 'document_chunks', ['hash'])
|
||||
op.create_index('idx_file_hash', 'documents', ['file_hash'])
|
||||
|
||||
def downgrade() -> None:
|
||||
# Drop indexes
|
||||
op.drop_index('idx_hash', table_name='document_chunks')
|
||||
op.drop_index('idx_kb_file_name', table_name='document_chunks')
|
||||
op.drop_index('idx_file_hash', table_name='documents')
|
||||
|
||||
# Drop tables in reverse order
|
||||
op.drop_table('processing_tasks')
|
||||
op.drop_table('messages')
|
||||
op.drop_table('chat_knowledge_bases')
|
||||
op.drop_table('chats')
|
||||
op.drop_table('document_chunks')
|
||||
op.drop_table('documents')
|
||||
op.drop_table('knowledge_bases')
|
||||
op.drop_table('users')
|
||||
Reference in New Issue
Block a user