MonScoDocEssai/migrations/versions/497ba81343f7_identite_admission.py

126 lines
4.1 KiB
Python

"""identite_admission
Revision ID: 497ba81343f7
Revises: 5c44d0d215ca
Create Date: 2023-10-14 10:09:02.330634
Diverses amlioration du modèle Identite:
- boursier non null
- departement non null
- admission : 1 seule admission (one-to-one)
- adresse: etudid non null.
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.orm import sessionmaker # added by ev
# revision identifiers, used by Alembic.
revision = "497ba81343f7"
down_revision = "5c44d0d215ca"
branch_labels = None
depends_on = None
Session = sessionmaker()
def upgrade():
bind = op.get_bind()
session = Session(bind=bind)
# Enleve les éventuels nulls de boursier
session.execute(
sa.text(
"""
UPDATE identite SET boursier = false WHERE boursier IS NULL;
"""
)
)
# Enleve les éventuelles adresses orphelines:
session.execute(
sa.text(
"""
DELETE FROM adresse WHERE etudid IS NULL;
"""
)
)
# Affecte arbitrairement les éventuels étudiants sans département au 1er
# (il ne devrait pas y en avoir, sauf essais manuels ou bugs)
session.execute(
sa.text(
"""
UPDATE identite SET dept_id = (
SELECT MIN(id)
FROM departement
) WHERE dept_id IS NULL;
"""
)
)
with op.batch_alter_table("identite", schema=None) as batch_op:
batch_op.add_column(sa.Column("admission_id", sa.Integer(), nullable=True))
batch_op.alter_column("boursier", existing_type=sa.BOOLEAN(), nullable=False)
batch_op.alter_column("dept_id", existing_type=sa.Integer(), nullable=False)
batch_op.create_foreign_key(
"admissions_etudid_fkey", "admissions", ["admission_id"], ["id"]
)
batch_op.drop_constraint("identite_dept_id_code_ine_key", type_="unique")
batch_op.drop_constraint("identite_dept_id_code_nip_key", type_="unique")
with op.batch_alter_table("adresse", schema=None) as batch_op:
batch_op.alter_column("etudid", existing_type=sa.Integer(), nullable=False)
batch_op.drop_constraint("adresse_etudid_fkey", type_="foreignkey")
batch_op.create_foreign_key(
"adresse_etudid_fkey", "identite", ["etudid"], ["id"]
)
# Elimine eventuels duplicats dans Admission
session.execute(
sa.text(
"""
DELETE FROM admissions
WHERE id NOT IN (
SELECT MIN(id)
FROM admissions
GROUP BY etudid
);
"""
)
)
# Copie id
session.execute(
sa.text(
"""
UPDATE identite SET admission_id = admissions.id
FROM admissions WHERE admissions.etudid = identite.id;
"""
)
)
with op.batch_alter_table("admissions", schema=None) as batch_op:
batch_op.drop_constraint("admissions_etudid_fkey", type_="foreignkey")
# laisse l'ancienne colonne pour downgrade (tests)
# batch_op.drop_column('etudid')
def downgrade():
with op.batch_alter_table("identite", schema=None) as batch_op:
batch_op.drop_constraint("admissions_etudid_fkey", type_="foreignkey")
batch_op.alter_column("boursier", existing_type=sa.BOOLEAN(), nullable=True)
batch_op.alter_column("dept_id", existing_type=sa.Integer(), nullable=True)
batch_op.drop_column("admission_id")
with op.batch_alter_table("admissions", schema=None) as batch_op:
# batch_op.add_column(
# sa.Column("etudid", sa.INTEGER(), autoincrement=False, nullable=True)
# )
batch_op.create_foreign_key(
"admissions_etudid_fkey", "identite", ["etudid"], ["id"], ondelete="CASCADE"
)
with op.batch_alter_table("adresse", schema=None) as batch_op:
batch_op.drop_constraint("adresse_etudid_fkey", type_="foreignkey")
batch_op.create_foreign_key(
"adresse_etudid_fkey", "identite", ["etudid"], ["id"], ondelete="CASCADE"
)
batch_op.alter_column("etudid", existing_type=sa.INTEGER(), nullable=True)