From d980c6794a31ba74ccd805a6de3a08c4e7e8736a Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Sat, 9 Oct 2021 20:20:44 +0200 Subject: [PATCH] =?UTF-8?q?Constrainte=20d'unicit=C3=A9=20sur=20les=20insc?= =?UTF-8?q?riptions=20aux=20modules?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/formsemestre.py | 1 + ...codoc_9_0_51_add_unicity_constraint_on_.py | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 migrations/versions/d74b4e16fb3c_scodoc_9_0_51_add_unicity_constraint_on_.py diff --git a/app/models/formsemestre.py b/app/models/formsemestre.py index 89468ab2..93b781b7 100644 --- a/app/models/formsemestre.py +++ b/app/models/formsemestre.py @@ -273,6 +273,7 @@ class NotesModuleImplInscription(db.Model): """Inscription à un module (etudiants,moduleimpl)""" __tablename__ = "notes_moduleimpl_inscription" + __table_args__ = (db.UniqueConstraint("moduleimpl_id", "etudid"),) id = db.Column(db.Integer, primary_key=True) moduleimpl_inscription_id = db.synonym("id") diff --git a/migrations/versions/d74b4e16fb3c_scodoc_9_0_51_add_unicity_constraint_on_.py b/migrations/versions/d74b4e16fb3c_scodoc_9_0_51_add_unicity_constraint_on_.py new file mode 100644 index 00000000..1c9da8a4 --- /dev/null +++ b/migrations/versions/d74b4e16fb3c_scodoc_9_0_51_add_unicity_constraint_on_.py @@ -0,0 +1,46 @@ +"""ScoDoc 9.0.51: add unicity constraint on notes_moduleimpl_inscription + +Revision ID: d74b4e16fb3c +Revises: f86c013c9fbd +Create Date: 2021-10-09 20:08:50.927330 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.orm import sessionmaker # added by ev + + +# revision identifiers, used by Alembic. +revision = "d74b4e16fb3c" +down_revision = "f86c013c9fbd" +branch_labels = None +depends_on = None + +Session = sessionmaker() + + +def upgrade(): + # Added by ev: remove duplicates + bind = op.get_bind() + session = Session(bind=bind) + session.execute( + """ + DELETE FROM notes_moduleimpl_inscription i1 + USING notes_moduleimpl_inscription i2 + WHERE i1.id < i2.id + AND i1.moduleimpl_id = i2.moduleimpl_id + AND i1.etudid = i2.etudid; + """ + ) + + # ### commands auto generated by Alembic - please adjust! ### + op.create_unique_constraint( + None, "notes_moduleimpl_inscription", ["moduleimpl_id", "etudid"] + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint(None, "notes_moduleimpl_inscription", type_="unique") + # ### end Alembic commands ###