From adc7155fe9e7e9ef63768c0573e5bda2b779062f Mon Sep 17 00:00:00 2001 From: Akshay Joshi Date: Wed, 1 Feb 2023 17:52:22 +0530 Subject: [PATCH] Make a backup of the old SQLite database to support running older versions if necessary. #4728 --- web/pgadmin/__init__.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/web/pgadmin/__init__.py b/web/pgadmin/__init__.py index 799593757..fb94810b6 100644 --- a/web/pgadmin/__init__.py +++ b/web/pgadmin/__init__.py @@ -15,6 +15,7 @@ import sys import re import ipaddress import traceback +import shutil from types import MethodType from collections import defaultdict @@ -423,7 +424,15 @@ def create_app(app_name=None): # Run migration if current schema version is greater than the # 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() else: # check all tables are present in the db. @@ -454,12 +463,10 @@ def create_app(app_name=None): schema_version = get_version() # Run migration if current schema version is greater than - # the schema version stored in version table - if CURRENT_SCHEMA_VERSION >= schema_version: - db_upgrade(app) - - # Update schema version to the latest + # the schema version stored in version table. if CURRENT_SCHEMA_VERSION > schema_version: + db_upgrade(app) + # Update schema version to the latest set_version(CURRENT_SCHEMA_VERSION) db.session.commit() except Exception as e: