改用mysql

This commit is contained in:
amb
2026-09-02 11:19:49 +08:00
parent 410bd30b2e
commit 73c4d83f42
24 changed files with 831 additions and 258 deletions
@@ -20,7 +20,7 @@ depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# document_categories 新增字段
op.add_column('document_categories', sa.Column('parent_id', sa.String(length=32), nullable=True, comment='父分类 IDNULL = 顶层)'))
op.add_column('document_categories', sa.Column('path', sa.Text(), server_default='/', nullable=False, comment='物化路径'))
op.add_column('document_categories', sa.Column('path', sa.String(length=500), server_default='/', nullable=False, comment='物化路径'))
op.add_column('document_categories', sa.Column('is_folder', sa.Boolean(), server_default=sa.text('1'), nullable=False, comment='True=文件夹'))
op.create_index(op.f('ix_document_categories_parent_id'), 'document_categories', ['parent_id'], unique=False)
@@ -28,7 +28,7 @@ def upgrade() -> None:
op.add_column('documents', sa.Column('content', sa.Text(), nullable=True, comment='直接输入的文本内容'))
op.add_column('documents', sa.Column('content_format', sa.String(length=16), server_default='markdown', nullable=False, comment='内容格式'))
# SQLite 不支持 ALTER COLUMN,用 batch mode 重建表
# ALTER COLUMNMySQL 直接支持,SQLite 需要 batch mode
with op.batch_alter_table('documents', schema=None) as batch_op:
batch_op.alter_column('storage_path', existing_type=sa.String(1024), nullable=True)
batch_op.alter_column('sha256', existing_type=sa.String(64), nullable=True)
@@ -0,0 +1,125 @@
"""add_user_role
Revision ID: 4e40432cab9f
Revises: 4bd4c7f26818
Create Date: 2026-09-02 10:29:41.369850
"""
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 = '4e40432cab9f'
down_revision: Union[str, None] = '4bd4c7f26818'
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('document_categories', 'path',
existing_type=mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=500),
comment='物化路径,如 /01_公司层/04_岗位AI角色/',
existing_comment='物化路径',
existing_nullable=False,
existing_server_default=sa.text("'/'"))
op.alter_column('document_categories', 'is_folder',
existing_type=mysql.TINYINT(display_width=1),
comment='True=文件夹(可含子项),False=叶子分类',
existing_comment='True=文件夹',
existing_nullable=False,
existing_server_default=sa.text("'1'"))
op.create_foreign_key(None, 'document_categories', 'document_categories', ['parent_id'], ['id'])
op.alter_column('documents', 'original_filename',
existing_type=mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=512),
comment='原始文件名或文本标题 (仅展示)',
existing_comment='原始文件名 (仅展示)',
existing_nullable=False)
op.alter_column('documents', 'storage_path',
existing_type=mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=1024),
comment='相对于 data/ 的物理存储路径(文本内容文档为 NULL)',
existing_nullable=True)
op.alter_column('documents', 'content',
existing_type=mysql.TEXT(collation='utf8mb4_unicode_ci'),
comment='直接输入的文本内容(非文件上传时使用)',
existing_comment='直接输入的文本内容',
existing_nullable=True)
op.alter_column('documents', 'content_format',
existing_type=mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=16),
comment='内容格式:markdown / text',
existing_comment='内容格式',
existing_nullable=False,
existing_server_default=sa.text("'markdown'"))
op.alter_column('documents', 'file_size',
existing_type=mysql.INTEGER(),
comment='文件大小 (字节),文本内容文档为内容长度',
existing_comment='文件大小 (字节)',
existing_nullable=False)
op.alter_column('documents', 'file_ext',
existing_type=mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=16),
comment='文件扩展名 (.docx/.pdf/.md)',
existing_comment='文件扩展名 (.docx/.pdf)',
existing_nullable=False)
op.alter_column('documents', 'sha256',
existing_type=mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=64),
comment='文件 SHA-256 哈希(文本内容文档为 NULL)',
existing_nullable=True)
op.add_column('users', sa.Column('role', sa.String(length=16), nullable=False, comment='角色 (internal=内部员工 / customer=客户)'))
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('users', 'role')
op.alter_column('documents', 'sha256',
existing_type=mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=64),
comment=None,
existing_comment='文件 SHA-256 哈希(文本内容文档为 NULL)',
existing_nullable=True)
op.alter_column('documents', 'file_ext',
existing_type=mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=16),
comment='文件扩展名 (.docx/.pdf)',
existing_comment='文件扩展名 (.docx/.pdf/.md)',
existing_nullable=False)
op.alter_column('documents', 'file_size',
existing_type=mysql.INTEGER(),
comment='文件大小 (字节)',
existing_comment='文件大小 (字节),文本内容文档为内容长度',
existing_nullable=False)
op.alter_column('documents', 'content_format',
existing_type=mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=16),
comment='内容格式',
existing_comment='内容格式:markdown / text',
existing_nullable=False,
existing_server_default=sa.text("'markdown'"))
op.alter_column('documents', 'content',
existing_type=mysql.TEXT(collation='utf8mb4_unicode_ci'),
comment='直接输入的文本内容',
existing_comment='直接输入的文本内容(非文件上传时使用)',
existing_nullable=True)
op.alter_column('documents', 'storage_path',
existing_type=mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=1024),
comment=None,
existing_comment='相对于 data/ 的物理存储路径(文本内容文档为 NULL)',
existing_nullable=True)
op.alter_column('documents', 'original_filename',
existing_type=mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=512),
comment='原始文件名 (仅展示)',
existing_comment='原始文件名或文本标题 (仅展示)',
existing_nullable=False)
op.drop_constraint(None, 'document_categories', type_='foreignkey')
op.alter_column('document_categories', 'is_folder',
existing_type=mysql.TINYINT(display_width=1),
comment='True=文件夹',
existing_comment='True=文件夹(可含子项),False=叶子分类',
existing_nullable=False,
existing_server_default=sa.text("'1'"))
op.alter_column('document_categories', 'path',
existing_type=mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=500),
comment='物化路径',
existing_comment='物化路径,如 /01_公司层/04_岗位AI角色/',
existing_nullable=False,
existing_server_default=sa.text("'/'"))
# ### end Alembic commands ###