"""rename_old_table_prefix_for_update_tables

Revision ID: d8f4912d7a52
Revises: c3a0e1c71ce6
Create Date: 2026-05-20 18:02:45.186600

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'd8f4912d7a52'
down_revision: Union[str, Sequence[str], None] = 'c3a0e1c71ce6'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # 将旧环境（cbd4068 前）创建的 app_releases / release_packages 重命名为 mjk_ 前缀
    # 使用 IF EXISTS 兼容：新环境已在 initial_schema 中创建了正确前缀的表名
    op.execute(
        "ALTER TABLE IF EXISTS app_releases RENAME TO mjk_app_releases"
    )
    op.execute(
        "ALTER INDEX IF EXISTS ix_app_releases_version "
        "RENAME TO ix_mjk_app_releases_version"
    )
    op.execute(
        "ALTER TABLE IF EXISTS release_packages RENAME TO mjk_release_packages"
    )


def downgrade() -> None:
    """Downgrade schema."""
    op.execute(
        "ALTER TABLE IF EXISTS mjk_app_releases RENAME TO app_releases"
    )
    op.execute(
        "ALTER INDEX IF EXISTS ix_mjk_app_releases_version "
        "RENAME TO ix_app_releases_version"
    )
    op.execute(
        "ALTER TABLE IF EXISTS mjk_release_packages RENAME TO release_packages"
    )
