"""Change lengths of several Char fields

Revision ID: 0002
Revises: 0001
Create Date: 2022-09-06 10:13:48.622901

"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "0002"
down_revision = "0001"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column("setting", sa.Column("plugin_id", sa.String(length=32), nullable=False))

    op.create_unique_constraint("organisation_id", "organisation", ["id"])
    op.create_unique_constraint(
        "unique_plugin_per_repo_per_org", "plugin_state", ["plugin_id", "organisation_pk", "repository_pk"]
    )
    op.create_unique_constraint("repository_id", "repository", ["id"])
    op.create_unique_constraint(
        "unique_keys_per_organisation_per_plugin", "setting", ["key", "organisation_pk", "plugin_id"]
    )

    op.alter_column(
        "setting", "key", existing_type=sa.String(length=32), type_=sa.String(length=128), existing_nullable=False
    )
    op.alter_column(
        "setting", "value", existing_type=sa.String(length=64), type_=sa.String(length=128), existing_nullable=False
    )
    op.drop_constraint("plugin_state_plugin_id_key", "plugin_state", type_="unique")
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint("unique_keys_per_organisation_per_plugin", "setting", type_="unique")

    op.alter_column(
        "setting", "value", existing_type=sa.String(length=128), type_=sa.String(length=64), existing_nullable=False
    )
    op.alter_column(
        "setting", "key", existing_type=sa.String(length=128), type_=sa.String(length=32), existing_nullable=False
    )

    op.drop_constraint("repository_id", "repository", type_="unique")
    op.drop_constraint("unique_plugin_per_repo_per_org", "plugin_state", type_="unique")
    op.drop_constraint("organisation_id", "organisation", type_="unique")
    op.create_unique_constraint("plugin_state_plugin_id_key", "plugin_state", ["plugin_id"])

    op.drop_column("setting", "plugin_id")
    # ### end Alembic commands ###
