"""init knowledge models Revision ID: 20260425_0001 Revises: Create Date: 2026-04-25 17:10:00 """ from collections.abc import Sequence from alembic import op import sqlalchemy as sa revision: str = "20260425_0001" down_revision: str | None = None branch_labels: Sequence[str] | None = None depends_on: Sequence[str] | None = None def upgrade() -> None: op.create_table( "knowledge_base", sa.Column("code", sa.String(length=64), nullable=False), sa.Column("name", sa.String(length=128), nullable=False), sa.Column("description", sa.Text(), nullable=True), sa.Column("status", sa.String(length=32), nullable=False), sa.Column("metadata_json", sa.JSON(), nullable=True), sa.Column("id", sa.String(length=36), nullable=False), sa.Column("tenant_id", sa.String(length=36), nullable=False), sa.Column("created_by", sa.String(length=36), nullable=True), sa.Column("updated_by", sa.String(length=36), nullable=True), sa.Column("created_time", sa.DateTime(), nullable=False), sa.Column("updated_time", sa.DateTime(), nullable=False), sa.Column("deleted_time", sa.DateTime(), nullable=True), sa.Column("version", sa.Integer(), nullable=False), sa.PrimaryKeyConstraint("id"), ) op.create_index("ix_knowledge_base_code", "knowledge_base", ["code"], unique=False) op.create_index("ix_knowledge_base_status", "knowledge_base", ["status"], unique=False) op.create_index("ix_knowledge_base_tenant_id", "knowledge_base", ["tenant_id"], unique=False) op.create_table( "knowledge_document", sa.Column("knowledge_base_id", sa.String(length=36), nullable=False), sa.Column("title", sa.String(length=256), nullable=False), sa.Column("source_type", sa.String(length=32), nullable=False), sa.Column("source_uri", sa.String(length=512), nullable=True), sa.Column("status", sa.String(length=32), nullable=False), sa.Column("content_text", sa.Text(), nullable=False), sa.Column("content_hash", sa.String(length=64), nullable=True), sa.Column("metadata_json", sa.JSON(), nullable=True), sa.Column("indexed_time", sa.DateTime(), nullable=True), sa.Column("id", sa.String(length=36), nullable=False), sa.Column("tenant_id", sa.String(length=36), nullable=False), sa.Column("created_by", sa.String(length=36), nullable=True), sa.Column("updated_by", sa.String(length=36), nullable=True), sa.Column("created_time", sa.DateTime(), nullable=False), sa.Column("updated_time", sa.DateTime(), nullable=False), sa.Column("deleted_time", sa.DateTime(), nullable=True), sa.Column("version", sa.Integer(), nullable=False), sa.PrimaryKeyConstraint("id"), ) op.create_index( "ix_knowledge_document_knowledge_base_id", "knowledge_document", ["knowledge_base_id"], unique=False, ) op.create_index( "ix_knowledge_document_source_type", "knowledge_document", ["source_type"], unique=False, ) op.create_index("ix_knowledge_document_status", "knowledge_document", ["status"], unique=False) op.create_index( "ix_knowledge_document_content_hash", "knowledge_document", ["content_hash"], unique=False, ) op.create_index( "ix_knowledge_document_tenant_id", "knowledge_document", ["tenant_id"], unique=False, ) op.create_table( "knowledge_chunk", sa.Column("knowledge_base_id", sa.String(length=36), nullable=False), sa.Column("document_id", sa.String(length=36), nullable=False), sa.Column("chunk_index", sa.Integer(), nullable=False), sa.Column("content_text", sa.Text(), nullable=False), sa.Column("token_count", sa.Integer(), nullable=False), sa.Column("embedding_model", sa.String(length=64), nullable=True), sa.Column("embedding_json", sa.JSON(), nullable=True), sa.Column("metadata_json", sa.JSON(), nullable=True), sa.Column("id", sa.String(length=36), nullable=False), sa.Column("tenant_id", sa.String(length=36), nullable=False), sa.Column("created_by", sa.String(length=36), nullable=True), sa.Column("updated_by", sa.String(length=36), nullable=True), sa.Column("created_time", sa.DateTime(), nullable=False), sa.Column("updated_time", sa.DateTime(), nullable=False), sa.Column("deleted_time", sa.DateTime(), nullable=True), sa.Column("version", sa.Integer(), nullable=False), sa.PrimaryKeyConstraint("id"), ) op.create_index( "ix_knowledge_chunk_knowledge_base_id", "knowledge_chunk", ["knowledge_base_id"], unique=False, ) op.create_index( "ix_knowledge_chunk_document_id", "knowledge_chunk", ["document_id"], unique=False, ) op.create_index( "ix_knowledge_chunk_embedding_model", "knowledge_chunk", ["embedding_model"], unique=False, ) op.create_index("ix_knowledge_chunk_tenant_id", "knowledge_chunk", ["tenant_id"], unique=False) def downgrade() -> None: op.drop_index("ix_knowledge_chunk_tenant_id", table_name="knowledge_chunk") op.drop_index("ix_knowledge_chunk_embedding_model", table_name="knowledge_chunk") op.drop_index("ix_knowledge_chunk_document_id", table_name="knowledge_chunk") op.drop_index("ix_knowledge_chunk_knowledge_base_id", table_name="knowledge_chunk") op.drop_table("knowledge_chunk") op.drop_index("ix_knowledge_document_tenant_id", table_name="knowledge_document") op.drop_index("ix_knowledge_document_content_hash", table_name="knowledge_document") op.drop_index("ix_knowledge_document_status", table_name="knowledge_document") op.drop_index("ix_knowledge_document_source_type", table_name="knowledge_document") op.drop_index("ix_knowledge_document_knowledge_base_id", table_name="knowledge_document") op.drop_table("knowledge_document") op.drop_index("ix_knowledge_base_tenant_id", table_name="knowledge_base") op.drop_index("ix_knowledge_base_status", table_name="knowledge_base") op.drop_index("ix_knowledge_base_code", table_name="knowledge_base") op.drop_table("knowledge_base")