"""Register the original knowledge table without replacing existing data.""" import sqlalchemy as sa from alembic import op revision = "0001_baseline" down_revision = None branch_labels = None depends_on = None def upgrade(): bind = op.get_bind() op.execute("CREATE EXTENSION IF NOT EXISTS vector") inspector = sa.inspect(bind) if inspector.has_table("knowledge_items"): columns = {column["name"] for column in inspector.get_columns("knowledge_items")} expected = { "id", "title", "category", "content", "source_url", "verification_status", "created_at", } if columns != expected: raise RuntimeError("已有 knowledge_items 结构与基础版本不同,请先核对结构并备份") return op.create_table( "knowledge_items", sa.Column("id", sa.Integer(), primary_key=True), sa.Column("title", sa.String(250), nullable=False), sa.Column("category", sa.String(80), nullable=False), sa.Column("content", sa.Text(), nullable=False), sa.Column("source_url", sa.String(1000)), sa.Column("verification_status", sa.String(30), nullable=False), sa.Column( "created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False ), ) op.create_index("ix_knowledge_items_id", "knowledge_items", ["id"]) op.create_index("ix_knowledge_items_title", "knowledge_items", ["title"]) def downgrade(): op.drop_table("knowledge_items") # 扩展可能由其他项目共享,不在回滚时删除。