"""Relocate artifact revision's readonly column

Revision ID: 29bbf3830309
Revises: 902837cf18b4
Create Date: 2025-08-26 11:42:13.907375

"""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.engine.reflection import Inspector

# revision identifiers, used by Alembic.
revision = "29bbf3830309"
down_revision = "902837cf18b4"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    conn = op.get_bind()
    inspector = Inspector.from_engine(conn)

    columns = [col["name"] for col in inspector.get_columns("artifact_revisions")]
    if "readonly" in columns:
        op.drop_column("artifact_revisions", "readonly")

    columns = [col["name"] for col in inspector.get_columns("artifacts")]
    if "readonly" not in columns:
        op.add_column(
            "artifacts",
            sa.Column("readonly", sa.Boolean(), server_default=sa.false(), nullable=False),
        )

        op.alter_column(
            "artifacts",
            "readonly",
            default=False,
            server_default=None,
            nullable=False,
        )

    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    conn = op.get_bind()
    inspector = Inspector.from_engine(conn)

    columns = [col["name"] for col in inspector.get_columns("artifact_revisions")]
    if "readonly" not in columns:
        op.add_column(
            "artifact_revisions",
            sa.Column("readonly", sa.Boolean(), server_default=sa.false(), nullable=True),
        )

        op.alter_column(
            "artifact_revisions",
            "readonly",
            default=False,
            server_default=None,
            nullable=False,
        )

    columns = [col["name"] for col in inspector.get_columns("artifacts")]
    if "readonly" in columns:
        op.drop_column("artifacts", "readonly")
    # ### end Alembic commands ###
