From 0b553a98a4741d4e61d0fd0d239a03ce1a47013a Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Thu, 4 Aug 2022 16:10:11 +0200 Subject: [PATCH] =?UTF-8?q?Contrainte=20unicit=C3=A9=20dept=5Facronym?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/departements.py | 5 +-- .../814e8e37d082_unicite_dept_acronym.py | 32 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 migrations/versions/814e8e37d082_unicite_dept_acronym.py diff --git a/app/models/departements.py b/app/models/departements.py index 416c5177b7..6786bf9fd0 100644 --- a/app/models/departements.py +++ b/app/models/departements.py @@ -6,6 +6,7 @@ from typing import Any from app import db from app.models import SHORT_STR_LEN +from app.scodoc.sco_exceptions import ScoValueError class Departement(db.Model): @@ -13,7 +14,7 @@ class Departement(db.Model): id = db.Column(db.Integer, primary_key=True) acronym = db.Column( - db.String(SHORT_STR_LEN), nullable=False, index=True + db.String(SHORT_STR_LEN), nullable=False, index=True, unique=True ) # ne change jamais, voir la pref. DeptName description = db.Column(db.Text()) # pas utilisé par ScoDoc : voir DeptFullName date_creation = db.Column(db.DateTime(timezone=True), server_default=db.func.now()) @@ -60,7 +61,7 @@ def create_dept(acronym: str, visible=True) -> Departement: existing = Departement.query.filter_by(acronym=acronym).count() if existing: - raise ValueError(f"acronyme {acronym} déjà existant") + raise ScoValueError(f"acronyme {acronym} déjà existant") departement = Departement(acronym=acronym, visible=visible) p1 = ScoPreference(name="DeptName", value=acronym, departement=departement) db.session.add(p1) diff --git a/migrations/versions/814e8e37d082_unicite_dept_acronym.py b/migrations/versions/814e8e37d082_unicite_dept_acronym.py new file mode 100644 index 0000000000..f035225a42 --- /dev/null +++ b/migrations/versions/814e8e37d082_unicite_dept_acronym.py @@ -0,0 +1,32 @@ +"""unicite dept_acronym + +Revision ID: 814e8e37d082 +Revises: 57179ae34069 +Create Date: 2022-08-04 16:07:59.710487 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = "814e8e37d082" +down_revision = "57179ae34069" +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index("ix_departement_acronym", table_name="departement") + op.create_index( + op.f("ix_departement_acronym"), "departement", ["acronym"], unique=True + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index(op.f("ix_departement_acronym"), table_name="departement") + op.create_index("ix_departement_acronym", "departement", ["acronym"], unique=False) + # ### end Alembic commands ###