From a44e59f0c7aca46768ee33b3be957210403dbeee Mon Sep 17 00:00:00 2001
From: Dominik George <nik@naturalnet.de>
Date: Wed, 2 Jun 2021 11:03:19 +0200
Subject: [PATCH] Make unique constraints on break and lessonsubstitution less
 tight

Together with 18544e06971167185ae19c3ebf499c502160c4c8, updating the existing
migration is safe, because

 a) if the migration failed somewhere due to the multiple leaf nodes, it was
    never applied, and changing it is safe;
 b) if it succeeded, it will be renamed to 0008_ later, and thus
    re-applied
---
 CHANGELOG.rst                                             | 8 ++++++++
 .../apps/chronos/migrations/0007_unique_constraints.py    | 4 ++--
 aleksis/apps/chronos/models.py                            | 4 ++--
 3 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 4fe7c5c0..4de936f7 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -1,6 +1,14 @@
 Changelog
 =========
 
+Unreleased
+----------
+
+Fixed
+~~~~~
+
+* Unique constraints for breaks and substitutions were too tight
+
 `2.0b0`_ - 2021-05-21
 ---------------------
 
diff --git a/aleksis/apps/chronos/migrations/0007_unique_constraints.py b/aleksis/apps/chronos/migrations/0007_unique_constraints.py
index 754c3ae1..048da7fd 100644
--- a/aleksis/apps/chronos/migrations/0007_unique_constraints.py
+++ b/aleksis/apps/chronos/migrations/0007_unique_constraints.py
@@ -52,11 +52,11 @@ class Migration(migrations.Migration):
         ),
         migrations.AddConstraint(
             model_name='break',
-            constraint=models.UniqueConstraint(fields=('site_id', 'short_name'), name='unique_short_name_per_site_break'),
+            constraint=models.UniqueConstraint(fields=('validity', 'short_name'), name='unique_short_name_per_site_break'),
         ),
         migrations.AddConstraint(
             model_name='lessonsubstitution',
-            constraint=models.UniqueConstraint(fields=('lesson_period', 'week'), name='unique_period_per_week'),
+            constraint=models.UniqueConstraint(fields=('lesson_period', 'week', 'year'), name='unique_period_per_week'),
         ),
         migrations.AddConstraint(
             model_name='room',
diff --git a/aleksis/apps/chronos/models.py b/aleksis/apps/chronos/models.py
index 97f701ee..07dc3fda 100644
--- a/aleksis/apps/chronos/models.py
+++ b/aleksis/apps/chronos/models.py
@@ -451,7 +451,7 @@ class LessonSubstitution(ExtensibleModel, TeacherPropertiesMixin, WeekRelatedMix
             ),
             # Heads up: Link to period implies uniqueness per site
             models.UniqueConstraint(
-                fields=["lesson_period", "week"], name="unique_period_per_week"
+                fields=["lesson_period", "week", "year"], name="unique_period_per_week"
             ),
         ]
         indexes = [
@@ -866,7 +866,7 @@ class Break(ValidityRangeRelatedExtensibleModel):
         verbose_name_plural = _("Breaks")
         constraints = [
             models.UniqueConstraint(
-                fields=["site_id", "short_name"], name="unique_short_name_per_site_break"
+                fields=["validity", "short_name"], name="unique_short_name_per_site_break"
             ),
         ]
 
-- 
GitLab