From 9b7f9756d89f0a7908d9b7323f682f34b37d200e Mon Sep 17 00:00:00 2001 From: Patrick Uiterwijk Date: Fri, 8 May 2015 16:56:36 +0200 Subject: [PATCH] Add database schema versioning With this skeleton code we can add upgrade code if we ever change the database schema. https://fedorahosted.org/ipsilon/ticket/56 Signed-off-by: Patrick Uiterwijk Reviewed-by: Rob Crittenden --- ipsilon/util/data.py | 28 ++++++++++++++++++++++++++++ tests/pgdb.py | 1 + 2 files changed, 29 insertions(+) diff --git a/ipsilon/util/data.py b/ipsilon/util/data.py index 7fdc508..b7fde31 100644 --- a/ipsilon/util/data.py +++ b/ipsilon/util/data.py @@ -12,6 +12,7 @@ import uuid import logging +CURRENT_SCHEMA_VERSION = 1 OPTIONS_COLUMNS = ['name', 'option', 'value'] UNIQUE_DATA_COLUMNS = ['uuid', 'name', 'value'] @@ -267,6 +268,33 @@ class Store(Log): else: self._db = SqlStore.get_connection(name) self._query = SqlQuery + self._upgrade_database() + + def _upgrade_database(self): + if self.is_readonly: + # If the database is readonly, we cannot do anything to the + # schema. Let's just return, and assume people checked the + # upgrade notes + return + current_version = self.load_options('dbinfo').get('scheme', None) + if current_version is None or 'version' not in current_version: + # No version stored, storing current version + self.save_options('dbinfo', 'scheme', + {'version': CURRENT_SCHEMA_VERSION}) + current_version = CURRENT_SCHEMA_VERSION + else: + current_version = int(current_version['version']) + if current_version != CURRENT_SCHEMA_VERSION: + self.debug('Upgrading database schema from %i to %i' % ( + current_version, CURRENT_SCHEMA_VERSION)) + self._upgrade_database_from(current_version) + + def _upgrade_database_from(self, old_schema_version): + # Insert code here to upgrade from old_schema_version to + # CURRENT_SCHEMA_VERSION + raise Exception('Unable to upgrade database to current schema' + ' version: version %i is unknown!' % + old_schema_version) @property def is_readonly(self): diff --git a/tests/pgdb.py b/tests/pgdb.py index 78800ff..f9be5f0 100755 --- a/tests/pgdb.py +++ b/tests/pgdb.py @@ -27,6 +27,7 @@ idp_a = {'hostname': '${ADDRESS}:${PORT}', 'admin_user': '${TEST_USER}', 'system_user': '${TEST_USER}', 'instance': '${NAME}', + 'openid': 'False', 'secure': 'no', 'testauth': 'yes', 'pam': 'no', -- 2.20.1