feat(admin): 部门管理改造为组织树形结构

This commit is contained in:
HTEWJ\26070045 committed 2026-10-08 09:38:36 +08:00
1 parent c5b7cc662b
commit 48fe19ed71
21 files changed
+1086 -234

No files matched your search

@@ -0,0 +1,100 @@
"""Add department tree columns
Revision ID: dp1a2b3c4d5e6
Revises: 9e5045511198
Create Date: 2026-10-08 10:00:00.000000
用途:departments 表升级为树形结构(parent_id 邻接表 + 同级排序 + 隐藏标记),
并放开 name 全局唯一约束(多公司允许同名部门,改由 service 层做同级查重)。
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'dp1a2b3c4d5e6'
down_revision = '9e5045511198'
branch_labels = None
depends_on = None
def upgrade():
conn = op.get_bind()
insp = sa.inspect(conn)
tables = set(insp.get_table_names())
if 'departments' not in tables:
# 防御:空库直接按新结构建表
op.create_table(
'departments',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=100), nullable=False),
sa.Column('code', sa.String(length=50), nullable=True),
sa.Column('company', sa.String(length=100), nullable=True),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('parent_id', sa.Integer(), nullable=True),
sa.Column('sort_order', sa.Integer(), nullable=False, server_default='0'),
sa.Column('is_hidden', sa.Boolean(), nullable=False, server_default=sa.false()),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('code'),
sa.ForeignKeyConstraint(
['parent_id'], ['departments.id'], name='fk_departments_parent_id_departments'
),
)
return
cols = {c['name'] for c in insp.get_columns('departments')}
with op.batch_alter_table('departments', schema=None) as batch_op:
if 'parent_id' not in cols:
batch_op.add_column(sa.Column('parent_id', sa.Integer(), nullable=True))
if 'sort_order' not in cols:
batch_op.add_column(sa.Column('sort_order', sa.Integer(), nullable=False, server_default='0'))
if 'is_hidden' not in cols:
batch_op.add_column(sa.Column('is_hidden', sa.Boolean(), nullable=False, server_default=sa.false()))
# 重新内省后再补自关联外键(列可能由上一步刚添加)
insp2 = sa.inspect(conn)
fks = insp2.get_foreign_keys('departments')
has_parent_fk = any(
'parent_id' in (fk.get('constrained_columns') or []) for fk in fks
)
if not has_parent_fk:
op.create_foreign_key(
'fk_departments_parent_id_departments', 'departments', 'departments',
['parent_id'], ['id'],
)
# 放开 name 全局唯一:删除覆盖 name 列的 unique 约束(同名约束名在不同库不一致,按列名探测)
name_uq_names = [
uq['name'] for uq in insp2.get_unique_constraints('departments')
if set(uq.get('column_names') or []) == {'name'} and uq.get('name')
]
if name_uq_names:
with op.batch_alter_table('departments', schema=None) as batch_op:
for uq_name in name_uq_names:
batch_op.drop_constraint(uq_name, type_='unique')
def downgrade():
conn = op.get_bind()
insp = sa.inspect(conn)
if 'departments' not in set(insp.get_table_names()):
return
cols = {c['name'] for c in insp.get_columns('departments')}
with op.batch_alter_table('departments', schema=None) as batch_op:
if 'is_hidden' in cols:
batch_op.drop_column('is_hidden')
if 'sort_order' in cols:
batch_op.drop_column('sort_order')
if 'parent_id' in cols:
try:
batch_op.drop_constraint('fk_departments_parent_id_departments', type_='foreignkey')
except Exception:
pass
batch_op.drop_column('parent_id')
# 恢复 name 全局唯一;若历史数据存在重名会失败,需先清洗数据
try:
batch_op.create_unique_constraint('uq_departments_name', ['name'])
except Exception:
pass