"""Api key could have mutliple roles

Revision ID: 68c62e902853
Revises: 82e280cdac06
Create Date: 2024-10-23 10:29:50.715009

"""

import sqlalchemy as sa
from alembic import op
from db_models.custom_types import RoleType
from utils import constants

# revision identifiers, used by Alembic.
revision = "68c62e902853"
down_revision = "fb76ac2d7a14"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "api_keys",
        sa.Column(
            "roles",
            RoleType(),
        ),
    )
    op.execute(
        f"UPDATE api_keys SET roles = CAST('[\"{constants.ORG_ADMIN}\"]' AS BYTEA) WHERE roles IS NULL",
    )
    op.alter_column(
        "api_keys",
        "roles",
        nullable=False,
        server_default=sa.text(f"'[\"{constants.VALIDATION_USER}\"]'"),
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column("api_keys", "role")
    # ### end Alembic commands ###
