"""add core_database_connection table

Revision ID: d4e5f6g7h8i9
Revises: c3d4e5f6g7h8
Create Date: 2026-05-29 10:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

revision: str = 'd4e5f6g7h8i9'
down_revision: Union[str, None] = 'c3d4e5f6g7h8'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None

_BASE_COLS = [
    sa.Column('id', sa.String(length=21), nullable=False, comment='主键ID(NanoId)'),
    sa.Column('sort', sa.Integer(), nullable=True, comment='排序'),
    sa.Column('is_deleted', sa.Boolean(), nullable=True, comment='是否删除'),
    sa.Column('sys_create_datetime', sa.DateTime(), server_default=sa.text('now()'), nullable=True, comment='创建时间'),
    sa.Column('sys_update_datetime', sa.DateTime(), server_default=sa.text('now()'), nullable=True, comment='更新时间'),
    sa.Column('sys_creator_id', sa.String(length=21), nullable=True, comment='创建人ID'),
    sa.Column('sys_modifier_id', sa.String(length=21), nullable=True, comment='修改人ID'),
    sa.Column('sys_dept_id', sa.String(length=21), nullable=True, comment='部门ID'),
]


def upgrade() -> None:
    op.create_table(
        'core_database_connection',
        sa.Column('application_id', sa.String(length=21), nullable=True, comment='所属应用ID'),
        sa.Column('code', sa.String(length=50), nullable=False, comment='连接编码'),
        sa.Column('name', sa.String(length=100), nullable=False, comment='连接名称'),
        sa.Column('db_type', sa.String(length=20), nullable=False, comment='数据库类型'),
        sa.Column('host', sa.String(length=255), nullable=False, comment='主机'),
        sa.Column('port', sa.Integer(), nullable=False, comment='端口'),
        sa.Column('user', sa.String(length=100), nullable=True, comment='用户名'),
        sa.Column('password_enc', sa.Text(), nullable=True, comment='加密密码'),
        sa.Column('default_database', sa.String(length=100), nullable=True, comment='默认数据库名'),
        sa.Column('description', sa.Text(), nullable=True, comment='描述'),
        sa.Column('status', sa.Boolean(), nullable=True, server_default='true', comment='是否启用'),
        sa.Column('is_system', sa.Boolean(), nullable=True, server_default='false', comment='是否系统内置'),
        sa.Column('extra_options', sa.JSON(), nullable=True, comment='扩展选项'),
        *_BASE_COLS,
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('code'),
    )
    op.create_index('ix_core_database_connection_application_id', 'core_database_connection', ['application_id'])
    op.create_index('ix_core_database_connection_code', 'core_database_connection', ['code'])
    op.create_index('ix_core_database_connection_status', 'core_database_connection', ['status'])
    op.create_index('ix_core_database_connection_is_deleted', 'core_database_connection', ['is_deleted'])
    op.create_index(
        'ix_core_database_connection_app_status',
        'core_database_connection',
        ['application_id', 'status'],
    )


def downgrade() -> None:
    op.drop_index('ix_core_database_connection_app_status', table_name='core_database_connection')
    op.drop_index('ix_core_database_connection_is_deleted', table_name='core_database_connection')
    op.drop_index('ix_core_database_connection_status', table_name='core_database_connection')
    op.drop_index('ix_core_database_connection_code', table_name='core_database_connection')
    op.drop_index('ix_core_database_connection_application_id', table_name='core_database_connection')
    op.drop_table('core_database_connection')
