# Copyright (C) 2023 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 cascades for job_platforms table

Revision ID: bc324dfd3610
Revises: afa46425330d
Create Date: 2023-12-01 11:30:54.380837

"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "bc324dfd3610"
down_revision = "afa46425330d"
branch_labels = None
depends_on = None

naming_convention = {
    "fk": "job_platforms_job_name_fkey",
}


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    bind = op.get_context().bind
    assert bind is not None, "Alembic database connection is missing"

    with op.batch_alter_table("job_platforms", naming_convention=naming_convention) as batch_op:
        batch_op.drop_constraint("job_platforms_job_name_fkey", type_="foreignkey")
        batch_op.create_foreign_key(
            "job_platforms_job_name_fkey", "jobs", ["job_name"], ["name"], onupdate="CASCADE", ondelete="CASCADE"
        )

    # ### end Alembic commands ###


def downgrade() -> None:
    bind = op.get_context().bind
    assert bind is not None, "Alembic database connection is missing"

    with op.batch_alter_table("job_platforms", naming_convention=naming_convention) as batch_op:
        batch_op.drop_constraint("job_platforms_job_name_fkey", type_="foreignkey")
        batch_op.create_foreign_key("job_platforms_job_name_fkey", "jobs", ["job_name"], ["name"])
