1
0
Fork 0
forked from s3lph/matemat

Hotfix: Database migration caused data loss.

This commit is contained in:
s3lph 2018-09-13 18:39:10 +02:00
parent dcbd093eed
commit b3a1df9e18

View file

@ -62,14 +62,8 @@ class DatabaseWrapper(object):
return DatabaseTransaction(self._sqlite_db, exclusive) return DatabaseTransaction(self._sqlite_db, exclusive)
def _setup(self) -> None: def _setup(self) -> None:
# Enable foreign key enforcement
cursor = self._sqlite_db.cursor()
cursor.execute('PRAGMA foreign_keys = 1')
# Create or update schemas if necessary # Create or update schemas if necessary
with self.transaction() as c: with self.transaction() as c:
# Defer foreign key enforcement in the setup transaction
c.execute('PRAGMA defer_foreign_keys = 1')
version: int = self._user_version version: int = self._user_version
if version < 1: if version < 1:
# Don't use executescript, as it issues a COMMIT first # Don't use executescript, as it issues a COMMIT first
@ -81,6 +75,10 @@ class DatabaseWrapper(object):
raise RuntimeError('Database schema is newer than supported by this version of Matemat.') raise RuntimeError('Database schema is newer than supported by this version of Matemat.')
self._user_version = self.SCHEMA_VERSION self._user_version = self.SCHEMA_VERSION
# Enable foreign key enforcement
cursor = self._sqlite_db.cursor()
cursor.execute('PRAGMA foreign_keys = 1')
def _upgrade(self, from_version: int, to_version: int) -> None: def _upgrade(self, from_version: int, to_version: int) -> None:
with self.transaction() as c: with self.transaction() as c:
# Note to future s3lph: If there are further migrations, also consider upgrades like 1 -> 3 # Note to future s3lph: If there are further migrations, also consider upgrades like 1 -> 3