feat: take a backup of the sqlite db before executing schema migrations
This commit is contained in:
parent
43ac5d656f
commit
8879add39b
3 changed files with 19 additions and 9 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -9,5 +9,6 @@
|
||||||
|
|
||||||
*.sqlite3
|
*.sqlite3
|
||||||
*.db
|
*.db
|
||||||
|
*.bak
|
||||||
static/upload/
|
static/upload/
|
||||||
./matemat.conf
|
./matemat.conf
|
||||||
|
|
|
@ -60,23 +60,31 @@ class DatabaseWrapper(object):
|
||||||
|
|
||||||
def _setup(self) -> None:
|
def _setup(self) -> None:
|
||||||
# Create or update schemas if necessary
|
# Create or update schemas if necessary
|
||||||
with self.transaction() as c:
|
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
|
with self.transaction() as c:
|
||||||
for command in SCHEMAS[self.SCHEMA_VERSION]:
|
for command in SCHEMAS[self.SCHEMA_VERSION]:
|
||||||
c.execute(command)
|
c.execute(command)
|
||||||
elif version < self.SCHEMA_VERSION:
|
elif version < self.SCHEMA_VERSION:
|
||||||
self._upgrade(from_version=version, to_version=self.SCHEMA_VERSION)
|
self._upgrade(from_version=version, to_version=self.SCHEMA_VERSION)
|
||||||
elif version > self.SCHEMA_VERSION:
|
elif version > self.SCHEMA_VERSION:
|
||||||
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
|
# Enable foreign key enforcement
|
||||||
cursor = self._sqlite_db.cursor()
|
cursor = self._sqlite_db.cursor()
|
||||||
cursor.execute('PRAGMA foreign_keys=ON')
|
cursor.execute('PRAGMA foreign_keys=ON')
|
||||||
|
|
||||||
def _upgrade(self, from_version: int, to_version: int) -> None:
|
def _upgrade(self, from_version: int, to_version: int) -> None:
|
||||||
|
if from_version >= to_version:
|
||||||
|
return
|
||||||
|
if self._filename != ':memory:':
|
||||||
|
bakfile = f'{self._filename}_{from_version}_{to_version}.bak'
|
||||||
|
bak = sqlite3.connect(bakfile)
|
||||||
|
with bak:
|
||||||
|
self._sqlite_db.backup(bak, pages=1)
|
||||||
|
bak.close()
|
||||||
with self.transaction() as c:
|
with self.transaction() as c:
|
||||||
c.execute('PRAGMA foreign_keys=OFF')
|
c.execute('PRAGMA foreign_keys=OFF')
|
||||||
c.execute('PRAGMA legacy_alter_table=ON')
|
c.execute('PRAGMA legacy_alter_table=ON')
|
||||||
|
|
|
@ -16,6 +16,7 @@ if [[ "$1" == "configure" ]]; then
|
||||||
chmod 0750 /var/lib/matemat
|
chmod 0750 /var/lib/matemat
|
||||||
ln -sf /var/lib/matemat/upload /usr/lib/matemat/static/upload
|
ln -sf /var/lib/matemat/upload /usr/lib/matemat/static/upload
|
||||||
|
|
||||||
|
systemctl daemon-reload || true
|
||||||
deb-systemd-helper enable matemat.service
|
deb-systemd-helper enable matemat.service
|
||||||
deb-systemd-invoke restart matemat.service
|
deb-systemd-invoke restart matemat.service
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue