"""Rétablir le rattachement des tâches planifiées aux contrats.
Revision ID: 8a95b1c2d3e4
Revises: 5784fc27cb2a
"""
from alembic import op
import sqlalchemy as sa
revision = "8a95b1c2d3e4"
down_revision = "5784fc27cb2a"
branch_labels = None
depends_on = None
def upgrade():
    inspector = sa.inspect(op.get_bind())
    columns = {c["name"] for c in inspector.get_columns("scheduled_tasks")}
    if "contract_id" not in columns:
        op.add_column("scheduled_tasks", sa.Column("contract_id", sa.Integer(), nullable=True))
    names = {fk.get("name") for fk in inspector.get_foreign_keys("scheduled_tasks")}
    if "fk_scheduled_tasks_contract_id" not in names:
        op.create_foreign_key("fk_scheduled_tasks_contract_id", "scheduled_tasks", "contracts", ["contract_id"], ["id"])
def downgrade():
    inspector = sa.inspect(op.get_bind())
    names = {fk.get("name") for fk in inspector.get_foreign_keys("scheduled_tasks")}
    if "fk_scheduled_tasks_contract_id" in names:
        op.drop_constraint("fk_scheduled_tasks_contract_id", "scheduled_tasks", type_="foreignkey")
    if "contract_id" in {c["name"] for c in inspector.get_columns("scheduled_tasks")}:
        op.drop_column("scheduled_tasks", "contract_id")
