From cc0c421363fcd5e6456e171e7922745b2e3e86aa Mon Sep 17 00:00:00 2001
From: Dominik George <dominik.george@teckids.org>
Date: Thu, 1 Sep 2022 12:14:02 +0200
Subject: [PATCH] Fix guess_school_id migration

---
 CHANGELOG.rst                                         | 6 ++++++
 aleksis/apps/untis/migrations/0003_guess_school_id.py | 7 ++++---
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 1281b8f..54bde93 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -9,6 +9,12 @@ and this project adheres to `Semantic Versioning`_.
 Unreleased
 ----------
 
+Fixed
+~~~~~
+
+* Guessing school ID could fail in `aleksis-admin migrate` under some
+  version conditions
+
 `2.3.1`_ - 2022-08-13
 ---------------------
 
diff --git a/aleksis/apps/untis/migrations/0003_guess_school_id.py b/aleksis/apps/untis/migrations/0003_guess_school_id.py
index 79a2d27..090fd6d 100644
--- a/aleksis/apps/untis/migrations/0003_guess_school_id.py
+++ b/aleksis/apps/untis/migrations/0003_guess_school_id.py
@@ -13,9 +13,10 @@ def guess_school_id(apps, schema_editor):
     except ValidityRange.DoesNotExist:
         return
 
-    school_id = vr.school_id_untis
-    if school_id:
-        get_site_preferences()["untis_mysql__school_id"] = school_id
+    if not vr or not vr.school_id_untis:
+        return
+
+    get_site_preferences()["untis_mysql__school_id"] = school_id
 
 
 class Migration(migrations.Migration):
-- 
GitLab