"""创建 Core 库基础表。"""

from alembic import op

from zbt.infrastructure.mysql.core_models import CoreBase

revision = "core_0001"
down_revision = None
branch_labels = None
depends_on = None

TABLE_NAMES = (
    "h5_users",
    "permissions",
    "roles",
    "admin_users",
    "auth_sessions",
    "products",
    "product_versions",
    "plans",
    "eligibility_rules",
    "rate_tables",
    "admin_user_roles",
    "role_permissions",
)


def upgrade() -> None:
    bind = op.get_bind()
    tables = [CoreBase.metadata.tables[name] for name in TABLE_NAMES]
    for table in CoreBase.metadata.sorted_tables:
        if table in tables:
            table.create(bind=bind)


def downgrade() -> None:
    bind = op.get_bind()
    tables = [CoreBase.metadata.tables[name] for name in TABLE_NAMES]
    for table in reversed(CoreBase.metadata.sorted_tables):
        if table in tables:
            table.drop(bind=bind)
