From 51adba2e255fcf982c7656e47ff6ff97a942150b Mon Sep 17 00:00:00 2001
From: s3lph <s3lph@kabelsalat.ch>
Date: Sat, 7 Dec 2024 21:15:08 +0100
Subject: [PATCH] fix: sqlite constraint migration

---
 matemat/db/wrapper.py | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/matemat/db/wrapper.py b/matemat/db/wrapper.py
index 30fd6e5..bea257e 100644
--- a/matemat/db/wrapper.py
+++ b/matemat/db/wrapper.py
@@ -74,10 +74,12 @@ class DatabaseWrapper(object):
 
         # Enable foreign key enforcement
         cursor = self._sqlite_db.cursor()
-        cursor.execute('PRAGMA foreign_keys = 1')
+        cursor.execute('PRAGMA foreign_keys=ON')
 
     def _upgrade(self, from_version: int, to_version: int) -> None:
         with self.transaction() as c:
+            c.execute('PRAGMA foreign_keys=OFF')
+            c.execute('PRAGMA legacy_alter_table=ON')
             if from_version <= 1 and to_version >= 2:
                 migrate_schema_1_to_2(c)
             if from_version <= 2 and to_version >= 3:
@@ -98,6 +100,8 @@ class DatabaseWrapper(object):
                 migrate_schema_8_to_9(c)
             if from_version <= 9 and to_version >= 10:
                 migrate_schema_9_to_10(c)
+            c.execute('PRAGMA foreign_key_check')
+            c.execute('PRAGMA foreign_keys=ON')
 
     def connect(self) -> None:
         if self.is_connected():