"""add user token field

Revision ID: b360a1f1b06e
Revises: 1138d225c857
Create Date: 2024-12-03 13:10:28.637848

"""

from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op
from sqlmodel.sql.sqltypes import AutoString

# revision identifiers, used by Alembic.
revision: str = "b360a1f1b06e"
down_revision: Union[str, None] = "1138d225c857"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # Add token column as nullable initially
    op.add_column("user", sa.Column("token", AutoString(), nullable=True))

    # Set default values
    op.execute(
        """
        UPDATE "user" 
        SET token = CASE 
            WHEN id = 1 THEN 'DEFAULT'
            ELSE 'USER_' || id::text
        END
    """
    )

    # Make column non-nullable
    op.alter_column("user", "token", nullable=False)


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