From 7835da267b390ce8fc97c68ca735e0e0c8bde94a Mon Sep 17 00:00:00 2001 From: Surinder Kumar Date: Tue, 29 Aug 2017 15:03:02 +0100 Subject: [PATCH] Allow pgAdmin to run with config database versions from the future. Fixes #2664 --- web/pgadmin/__init__.py | 24 ++++++++++++++++++++++-- web/setup.py | 20 +++++++++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/web/pgadmin/__init__.py b/web/pgadmin/__init__.py index cd18b1f85..e66ac59f7 100644 --- a/web/pgadmin/__init__.py +++ b/web/pgadmin/__init__.py @@ -32,7 +32,9 @@ from werkzeug.utils import find_modules from pgadmin.utils.preferences import Preferences -from pgadmin.model import db, Role, Server, ServerGroup, User, Keys +from pgadmin.model import db, Role, Server, ServerGroup, \ + User, Keys, Version, SCHEMA_VERSION as CURRENT_SCHEMA_VERSION + # If script is running under python3, it will not have the xrange function # defined @@ -283,7 +285,25 @@ def create_app(app_name=None): ########################################################################## # Upgrade the schema (if required) ########################################################################## - db_upgrade(app) + with app.app_context(): + # Run migration for the first time i.e. create database + from config import SQLITE_PATH + if not os.path.exists(SQLITE_PATH): + db_upgrade(app) + else: + version = Version.query.filter_by(name='ConfigDB').first() + schema_version = version.value + + # 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 + if CURRENT_SCHEMA_VERSION > schema_version: + version = Version.query.filter_by(name='ConfigDB').first() + version.value = CURRENT_SCHEMA_VERSION + db.session.commit() Mail(app) diff --git a/web/setup.py b/web/setup.py index 20d693702..30c77f06a 100644 --- a/web/setup.py +++ b/web/setup.py @@ -12,6 +12,7 @@ and settings database.""" import os import sys +from pgadmin.model import db, Version, SCHEMA_VERSION as CURRENT_SCHEMA_VERSION if sys.version_info[0] >= 3: import builtins @@ -50,4 +51,21 @@ if __name__ == '__main__': print(u"======================================\n") with app.app_context(): - db_upgrade(app) + # Run migration for the first time i.e. create database + from config import SQLITE_PATH + if not os.path.exists(SQLITE_PATH): + db_upgrade(app) + else: + version = Version.query.filter_by(name='ConfigDB').first() + schema_version = version.value + + # 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 + if CURRENT_SCHEMA_VERSION > schema_version: + version = Version.query.filter_by(name='ConfigDB').first() + version.value = CURRENT_SCHEMA_VERSION + db.session.commit()