"""Rename Organization.pretty_name to name

Revision ID: f8c558160179
Revises: b0c7409f8489
Create Date: 2024-07-23 08:13:12.829022

"""

import sqlalchemy as sa
from alembic import op

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "f8c558160179"
down_revision = "b0c7409f8489"
branch_labels: tuple[str] | None = None
depends_on: tuple[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column(
        "organizations",
        "pretty_name",
        new_column_name="name",
        existing_type=sa.String(),
        existing_nullable=True,
    )

    op.execute(
        """
        UPDATE organizations
        SET name = slug
        WHERE name IS NULL
        """
    )

    op.alter_column(
        "organizations",
        "name",
        nullable=False,
        existing_nullable=True,
        existing_type=sa.String(),
    )

    op.create_index(
        op.f("ix_organizations_name"), "organizations", ["name"], unique=False
    )

    op.alter_column(
        "organizations", "avatar_url", existing_type=sa.VARCHAR(), nullable=True
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column(
        "organizations",
        "name",
        new_column_name="pretty_name",
        nullable=True,
        existing_type=sa.String(),
        existing_nullable=False,
    )
    op.drop_index(op.f("ix_organizations_name"), table_name="organizations")

    op.alter_column(
        "organizations", "avatar_url", existing_type=sa.VARCHAR(), nullable=False
    )
    # ### end Alembic commands ###
