Make a backup of the old SQLite database to support running older versions if necessary. #4728

This commit is contained in:
Akshay Joshi
2023-02-01 17:52:22 +05:30
parent e247867abf
commit adc7155fe9

View File

@@ -15,6 +15,7 @@ import sys
import re import re
import ipaddress import ipaddress
import traceback import traceback
import shutil
from types import MethodType from types import MethodType
from collections import defaultdict from collections import defaultdict
@@ -423,7 +424,15 @@ def create_app(app_name=None):
# Run migration if current schema version is greater than the # Run migration if current schema version is greater than the
# schema version stored in version table # schema version stored in version table
if CURRENT_SCHEMA_VERSION >= schema_version: if CURRENT_SCHEMA_VERSION > schema_version:
# Take a backup of the old database file.
try:
prev_database_file_name = \
"{0}.prev.bak".format(SQLITE_PATH)
shutil.copyfile(SQLITE_PATH, prev_database_file_name)
except Exception as e:
app.logger.error(e)
upgrade_db() upgrade_db()
else: else:
# check all tables are present in the db. # check all tables are present in the db.
@@ -454,12 +463,10 @@ def create_app(app_name=None):
schema_version = get_version() schema_version = get_version()
# Run migration if current schema version is greater than # Run migration if current schema version is greater than
# the schema version stored in version table # the schema version stored in version table.
if CURRENT_SCHEMA_VERSION >= schema_version:
db_upgrade(app)
# Update schema version to the latest
if CURRENT_SCHEMA_VERSION > schema_version: if CURRENT_SCHEMA_VERSION > schema_version:
db_upgrade(app)
# Update schema version to the latest
set_version(CURRENT_SCHEMA_VERSION) set_version(CURRENT_SCHEMA_VERSION)
db.session.commit() db.session.commit()
except Exception as e: except Exception as e: