"""Formsemestre / parcours, Inscriptions aux parcours + cascades sur Identite

Revision ID: a2771105c21c
Revises: 6002d7d366e5
Create Date: 2022-05-25 20:32:06.868296

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "a2771105c21c"
down_revision = "6002d7d366e5"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "parcours_formsemestre",
        sa.Column("parcours_id", sa.Integer(), nullable=False),
        sa.Column("formsemestre_id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["formsemestre_id"], ["notes_formsemestre.id"], ondelete="CASCADE"
        ),
        sa.ForeignKeyConstraint(
            ["parcours_id"],
            ["apc_parcours.id"],
        ),
        sa.PrimaryKeyConstraint("parcours_id", "formsemestre_id"),
    )
    op.drop_constraint("absences_etudid_fkey", "absences", type_="foreignkey")
    op.create_foreign_key(
        "absences_etudid_fkey",
        "absences",
        "identite",
        ["etudid"],
        ["id"],
        ondelete="CASCADE",
    )
    op.drop_constraint(
        "absences_notifications_etudid_fkey",
        "absences_notifications",
        type_="foreignkey",
    )
    op.create_foreign_key(
        "absences_notifications_etudid_fkey",
        "absences_notifications",
        "identite",
        ["etudid"],
        ["id"],
        ondelete="CASCADE",
    )
    op.drop_constraint("admissions_etudid_fkey", "admissions", type_="foreignkey")
    op.create_foreign_key(
        "admissions_etudid_fkey",
        "admissions",
        "identite",
        ["etudid"],
        ["id"],
        ondelete="CASCADE",
    )
    op.drop_constraint("adresse_etudid_fkey", "adresse", type_="foreignkey")
    op.create_foreign_key(
        "adresse_etudid_fkey",
        "adresse",
        "identite",
        ["etudid"],
        ["id"],
        ondelete="CASCADE",
    )
    op.drop_constraint(
        "group_membership_etudid_fkey", "group_membership", type_="foreignkey"
    )
    op.create_foreign_key(
        "group_membership_etudid_fkey",
        "group_membership",
        "identite",
        ["etudid"],
        ["id"],
        ondelete="CASCADE",
    )
    op.drop_constraint("itemsuivi_etudid_fkey", "itemsuivi", type_="foreignkey")
    op.create_foreign_key(
        "itemsuivi_etudid_fkey",
        "itemsuivi",
        "identite",
        ["etudid"],
        ["id"],
        ondelete="CASCADE",
    )
    op.drop_constraint(
        "notes_appreciations_etudid_fkey", "notes_appreciations", type_="foreignkey"
    )
    op.create_foreign_key(
        "notes_appreciations_etudid_fkey",
        "notes_appreciations",
        "identite",
        ["etudid"],
        ["id"],
        ondelete="CASCADE",
    )
    # GROUPES
    op.add_column(
        "partition",
        sa.Column(
            "groups_editable", sa.Boolean(), server_default="true", nullable=False
        ),
    )
    # INSCRIPTIONS
    op.drop_constraint(
        "notes_formsemestre_inscription_etudid_fkey",
        "notes_formsemestre_inscription",
        type_="foreignkey",
    )
    op.create_foreign_key(
        "notes_formsemestre_inscription_etudid_fkey",
        "notes_formsemestre_inscription",
        "identite",
        ["etudid"],
        ["id"],
        ondelete="CASCADE",
    )
    op.add_column(
        "notes_formsemestre_inscription",
        sa.Column("parcour_id", sa.Integer(), nullable=True),
    )
    op.create_index(
        op.f("ix_notes_formsemestre_inscription_parcour_id"),
        "notes_formsemestre_inscription",
        ["parcour_id"],
        unique=False,
    )
    op.create_foreign_key(
        "notes_formsemestre_inscription_parcour_id_fkey",
        "notes_formsemestre_inscription",
        "apc_parcours",
        ["parcour_id"],
        ["id"],
    )
    # ---
    op.drop_constraint("notes_notes_etudid_fkey", "notes_notes", type_="foreignkey")
    op.create_foreign_key(
        "notes_notes_etudid_fkey",
        "notes_notes",
        "identite",
        ["etudid"],
        ["id"],
        ondelete="CASCADE",
    )
    op.drop_constraint(
        "notes_notes_log_etudid_fkey", "notes_notes_log", type_="foreignkey"
    )
    op.create_foreign_key(
        "notes_notes_log_etudid_fkey",
        "notes_notes_log",
        "identite",
        ["etudid"],
        ["id"],
        ondelete="CASCADE",
    )
    op.drop_constraint(
        "scolar_autorisation_inscription_etudid_fkey",
        "scolar_autorisation_inscription",
        type_="foreignkey",
    )
    op.create_foreign_key(
        "scolar_autorisation_inscription_etudid_fkey",
        "scolar_autorisation_inscription",
        "identite",
        ["etudid"],
        ["id"],
        ondelete="CASCADE",
    )
    op.drop_constraint("scolar_events_etudid_fkey", "scolar_events", type_="foreignkey")
    op.create_foreign_key(
        "scolar_events_etudid_fkey",
        "scolar_events",
        "identite",
        ["etudid"],
        ["id"],
        ondelete="CASCADE",
    )
    op.drop_constraint(
        "scolar_formsemestre_validation_etudid_fkey",
        "scolar_formsemestre_validation",
        type_="foreignkey",
    )
    op.create_foreign_key(
        "scolar_formsemestre_validation_etudid_fkey",
        "scolar_formsemestre_validation",
        "identite",
        ["etudid"],
        ["id"],
        ondelete="CASCADE",
    )
    # ### end Alembic commands ###


# --------------------------------------------------------------
def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(
        "scolar_formsemestre_validation_etudid_fkey",
        "scolar_formsemestre_validation",
        type_="foreignkey",
    )
    op.create_foreign_key(
        "scolar_formsemestre_validation_etudid_fkey",
        "scolar_formsemestre_validation",
        "identite",
        ["etudid"],
        ["id"],
    )
    op.drop_constraint("scolar_events_etudid_fkey", "scolar_events", type_="foreignkey")
    op.create_foreign_key(
        "scolar_events_etudid_fkey", "scolar_events", "identite", ["etudid"], ["id"]
    )
    op.drop_constraint(
        "scolar_autorisation_inscription_etudid_fkey",
        "scolar_autorisation_inscription",
        type_="foreignkey",
    )
    op.create_foreign_key(
        "scolar_autorisation_inscription_etudid_fkey",
        "scolar_autorisation_inscription",
        "identite",
        ["etudid"],
        ["id"],
    )
    op.drop_constraint(
        "notes_notes_log_etudid_fkey", "notes_notes_log", type_="foreignkey"
    )
    op.create_foreign_key(
        "notes_notes_log_etudid_fkey", "notes_notes_log", "identite", ["etudid"], ["id"]
    )
    op.drop_constraint("notes_notes_etudid_fkey", "notes_notes", type_="foreignkey")
    op.create_foreign_key(
        "notes_notes_etudid_fkey", "notes_notes", "identite", ["etudid"], ["id"]
    )
    # GROUPES
    op.drop_column("partition", "groups_editable")
    # INSCRIPTIONS
    op.drop_constraint(
        "notes_formsemestre_inscription_etudid_fkey",
        "notes_formsemestre_inscription",
        type_="foreignkey",
    )
    op.create_foreign_key(
        "notes_formsemestre_inscription_etudid_fkey",
        "notes_formsemestre_inscription",
        "identite",
        ["etudid"],
        ["id"],
    )
    op.drop_constraint(
        "notes_formsemestre_inscription_parcour_id_fkey",
        "notes_formsemestre_inscription",
        type_="foreignkey",
    )
    op.drop_index(
        op.f("ix_notes_formsemestre_inscription_parcour_id"),
        table_name="notes_formsemestre_inscription",
    )
    op.drop_column("notes_formsemestre_inscription", "parcour_id")
    # --
    op.drop_constraint(
        "notes_appreciations_etudid_fkey", "notes_appreciations", type_="foreignkey"
    )
    op.create_foreign_key(
        "notes_appreciations_etudid_fkey",
        "notes_appreciations",
        "identite",
        ["etudid"],
        ["id"],
    )
    op.drop_constraint("itemsuivi_etudid_fkey", "itemsuivi", type_="foreignkey")
    op.create_foreign_key(
        "itemsuivi_etudid_fkey", "itemsuivi", "identite", ["etudid"], ["id"]
    )
    op.drop_constraint(
        "group_membership_etudid_fkey", "group_membership", type_="foreignkey"
    )
    op.create_foreign_key(
        "group_membership_etudid_fkey",
        "group_membership",
        "identite",
        ["etudid"],
        ["id"],
    )
    op.drop_constraint("adresse_etudid_fkey", "adresse", type_="foreignkey")
    op.create_foreign_key(
        "adresse_etudid_fkey", "adresse", "identite", ["etudid"], ["id"]
    )
    op.drop_constraint("admissions_etudid_fkey", "admissions", type_="foreignkey")
    op.create_foreign_key(
        "admissions_etudid_fkey", "admissions", "identite", ["etudid"], ["id"]
    )
    op.drop_constraint(
        "absences_notifications_etudid_fkey",
        "absences_notifications",
        type_="foreignkey",
    )
    op.create_foreign_key(
        "absences_notifications_etudid_fkey",
        "absences_notifications",
        "identite",
        ["etudid"],
        ["id"],
    )
    op.drop_constraint("absences_etudid_fkey", "absences", type_="foreignkey")
    op.create_foreign_key(
        "absences_etudid_fkey", "absences", "identite", ["etudid"], ["id"]
    )
    op.drop_table("parcours_formsemestre")
    # ### end Alembic commands ###
