# Copyright (C) 2020 Bloomberg LP
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#  <http://www.apache.org/licenses/LICENSE-2.0>
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Add client_identities table

Revision ID: b57c30a687fa
Revises: 53e8b8b5bed6
Create Date: 2023-04-11 14:22:08.511246

"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "b57c30a687fa"
down_revision = "53e8b8b5bed6"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "client_identities",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("instance", sa.String(), nullable=False),
        sa.Column("workflow", sa.String(), nullable=False),
        sa.Column("actor", sa.String(), nullable=False),
        sa.Column("subject", sa.String(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("instance", "workflow", "actor", "subject"),
    )

    # Adjusted by using batch_op and supplying a constraint name to be compatible with SQLight
    with op.batch_alter_table("operations") as batch_op:
        batch_op.add_column(sa.Column("client_identity_id", sa.Integer(), nullable=True))
        batch_op.create_foreign_key(
            "operation__client_identities_id", "client_identities", ["client_identity_id"], ["id"]
        )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###

    # Adjusted by using batch_op and supplying a constraint name to be compatible with SQLight
    with op.batch_alter_table("operations") as batch_op:
        batch_op.drop_constraint("operation__client_identities_id", type_="foreignkey")
        batch_op.drop_column("client_identity_id")
    op.drop_table("client_identities")
    # ### end Alembic commands ###
