def test_version_change_success(self): migration.perform_data_update(self.db_file) store = Store(create_database(GLSettings.db_uri)) prv = config.PrivateFactory(store) self.assertEqual(prv.get_val(u'version'), __version__) store.close()
def update_db(): """ This function handles update of an existing database """ db_version, db_file_path = get_db_file(Settings.working_path) if db_version == 0: return 0 try: with warnings.catch_warnings(): from globaleaks.db import migration warnings.simplefilter("ignore", category=sa_exc.SAWarning) log.err('Found an already initialized database version: %d', db_version) if db_version == DATABASE_VERSION: migration.perform_data_update(db_file_path) return DATABASE_VERSION log.err( 'Performing schema migration from version %d to version %d', db_version, DATABASE_VERSION) migration.perform_migration(db_version) except Exception as exception: log.err('Migration failure: %s', exception) log.err('Verbose exception traceback:') etype, value, tback = sys.exc_info() log.info('\n'.join(traceback.format_exception(etype, value, tback))) return -1 log.err('Migration completed with success!') return DATABASE_VERSION
def test_version_change_success(self): migration.perform_data_update(self.db_file) store = Store(create_database(GLSettings.db_uri)) prv = config.PrivateFactory(store) self.assertEqual(prv.get_val('version'), __version__) store.close()
def update_db(): """ This function handles update of an existing database """ try: db_version, db_file_path = get_db_file(GLSettings.db_path) if db_version == 0: return 0 from globaleaks.db import migration log.err("Found an already initialized database version: %d" % db_version) if db_version >= FIRST_DATABASE_VERSION_SUPPORTED and db_version < DATABASE_VERSION: log.err( "Performing schema migration from version %d to version %d" % (db_version, DATABASE_VERSION)) migration.perform_schema_migration(db_version) log.err("Migration completed with success!") else: log.err('Performing data update') # TODO on normal startup this line is run. We need better control flow here. migration.perform_data_update( os.path.abspath( os.path.join(GLSettings.db_path, 'glbackend-%d.db' % DATABASE_VERSION))) except Exception as exception: log.err("Migration failure: %s" % exception) log.err("Verbose exception traceback:") etype, value, tback = sys.exc_info() log.info('\n'.join(traceback.format_exception(etype, value, tback))) return -1
def test_check_unmodifiable_strings(self): # This test case asserts that data migration updates unmodifiable l10n strings self._initStartDB(34) notification_l10n = NotificationL10NFactory(self.store) t0 = notification_l10n.get_val('export_template', 'it') notification_l10n.set_val('export_template', 'it', '') t1 = notification_l10n.get_val('export_template', 'it') self.assertEqual(t1, '') self.store.commit() # place a dummy version in the current db store = Store(create_database(GLSettings.db_uri)) prv = config.PrivateFactory(store) self.dummy_ver = '2.XX.XX' prv.set_val('version', self.dummy_ver) self.assertEqual(prv.get_val('version'), self.dummy_ver) store.commit() store.close() migration.perform_data_update(self.db_file) store = Store(create_database(GLSettings.db_uri)) notification_l10n = NotificationL10NFactory(store) t2 = notification_l10n.get_val('export_template', 'it') self.assertEqual(t2, t0) store.commit() store.close() shutil.rmtree(GLSettings.db_path)
def test_check_unmodifiable_strings(self): # This test case asserts that data migration updates unmodifiable l10n strings self._initStartDB(34) notification_l10n = NotificationL10NFactory(self.store) t0 = notification_l10n.get_val("export_template", "it") notification_l10n.set_val("export_template", "it", "") t1 = notification_l10n.get_val("export_template", "it") self.assertEqual(t1, "") self.store.commit() # place a dummy version in the current db store = Store(create_database(GLSettings.db_uri)) prv = config.PrivateFactory(store) self.dummy_ver = "2.XX.XX" prv.set_val("version", self.dummy_ver) self.assertEqual(prv.get_val("version"), self.dummy_ver) store.commit() store.close() migration.perform_data_update(self.db_file) store = Store(create_database(GLSettings.db_uri)) notification_l10n = NotificationL10NFactory(store) t2 = notification_l10n.get_val("export_template", "it") self.assertEqual(t2, t0) store.commit() store.close() shutil.rmtree(GLSettings.db_path)
def update_db(): """ This function handles update of an existing database """ db_version, db_file_path = get_db_file(Settings.working_path) if db_version == 0: return 0 try: with warnings.catch_warnings(): from globaleaks.db import migration warnings.simplefilter("ignore", category=sa_exc.SAWarning) log.err('Found an already initialized database version: %d', db_version) if db_version == DATABASE_VERSION: migration.perform_data_update(db_file_path) return DATABASE_VERSION log.err('Performing schema migration from version %d to version %d', db_version, DATABASE_VERSION) migration.perform_migration(db_version) except Exception as exception: log.err('Migration failure: %s', exception) log.err('Verbose exception traceback:') etype, value, tback = sys.exc_info() log.info('\n'.join(traceback.format_exception(etype, value, tback))) return -1 log.err('Migration completed with success!') return DATABASE_VERSION
def perform_system_update(): """ This function checks the system and database versions and executes migration routines based on the system's state. After this function has completed the node is either ready for initialization (0), running a version of the DB (>1), or broken (-1). """ from globaleaks.db import migration db_files = [] max_version = 0 min_version = 0 for filename in os.listdir(GLSettings.db_path): if filename.startswith('glbackend'): filepath = os.path.join(GLSettings.db_path, filename) if filename.endswith('.db'): db_files.append(filepath) nameindex = filename.rfind('glbackend') extensindex = filename.rfind('.db') fileversion = int(filename[nameindex + len('glbackend-'):extensindex]) max_version = fileversion if fileversion > max_version else max_version min_version = fileversion if fileversion < min_version else min_version db_version = max_version if len(db_files) > 1: log.msg("Error: Cannot start the application because more than one database file are present in: %s" % GLSettings.db_path) log.msg("Manual check needed and is suggested to first make a backup of %s\n" % GLSettings.working_path) log.msg("Files found:") for f in db_files: log.msg("\t%s" % f) return -1 if len(db_files) == 1: log.msg("Found an already initialized database version: %d" % db_version) if db_version < DATABASE_VERSION: log.msg("Performing schema migration from version %d to version %d" % (db_version, DATABASE_VERSION)) try: migration.perform_schema_migration(db_version) except Exception as exception: log.msg("Migration failure: %s" % exception) log.msg("Verbose exception traceback:") etype, value, tback = sys.exc_info() log.msg('\n'.join(traceback.format_exception(etype, value, tback))) return -1 log.msg("Migration completed with success!") else: log.msg('Performing data update') migration.perform_data_update(os.path.abspath(os.path.join(GLSettings.db_path, 'glbackend-%d.db' % DATABASE_VERSION))) return db_version
def test_detect_and_fix_cfg_change(self): store = Store(create_database(GLSettings.db_uri)) ret = config.is_cfg_valid(store) self.assertFalse(ret) store.close() migration.perform_data_update(self.db_file) store = Store(create_database(GLSettings.db_uri)) prv = config.PrivateFactory(store) self.assertEqual(prv.get_val('version'), __version__) self.assertEqual(prv.get_val('xx_smtp_password'), self.dp) ret = config.is_cfg_valid(store) self.assertTrue(ret) store.close()
def test_detect_and_fix_cfg_change(self): store = Store(create_database(GLSettings.db_uri)) ret = config.is_cfg_valid(store) self.assertFalse(ret) store.close() migration.perform_data_update(self.db_file) store = Store(create_database(GLSettings.db_uri)) prv = config.PrivateFactory(store) self.assertEqual(prv.get_val(u'version'), __version__) self.assertEqual(prv.get_val(u'xx_smtp_password'), self.dp) ret = config.is_cfg_valid(store) self.assertTrue(ret) store.close()
def test_ver_change_exception(self): # Explicity throw an exception in managed_ver_update via is_cfg_valid config.is_cfg_valid = apply_gen(throw_excep) try: yield migration.perform_data_update(self.db_file) self.fail() except IOError as e: self.assertIsInstance(e, IOError) store = Store(create_database(GLSettings.db_uri)) prv = config.PrivateFactory(store) self.assertEqual(prv.get_val('version'), self.dummy_ver) store.close()
def test_version_change_not_ok(self): # Set is_config_valid to false during managed ver update config.is_cfg_valid = apply_gen(mod_bool) try: yield migration.perform_data_update(self.db_file) self.fail() except DatabaseIntegrityError as e: self.assertIsInstance(e, DatabaseIntegrityError) # Ensure the rollback has succeeded store = Store(create_database(GLSettings.db_uri)) prv = config.PrivateFactory(store) self.assertEqual(prv.get_val('version'), self.dummy_ver) store.close()
def perform_system_update(): """ This function checks the system and database versions and executes migration routines based on the system's state. After this function has completed the node is either ready for initialization (0), running a version of the DB (>1), or broken (-1). """ from globaleaks.db import migration db_files = [] max_version = 0 min_version = 0 for filename in os.listdir(GLSettings.db_path): if filename.startswith('glbackend'): filepath = os.path.join(GLSettings.db_path, filename) if filename.endswith('.db'): db_files.append(filepath) nameindex = filename.rfind('glbackend') extensindex = filename.rfind('.db') fileversion = int(filename[nameindex + len('glbackend-'):extensindex]) max_version = fileversion if fileversion > max_version else max_version min_version = fileversion if fileversion < min_version else min_version db_version = max_version if len(db_files) > 1: log.msg( "Error: Cannot start the application because more than one database file are present in: %s" % GLSettings.db_path) log.msg( "Manual check needed and is suggested to first make a backup of %s\n" % GLSettings.working_path) log.msg("Files found:") for f in db_files: log.msg("\t%s" % f) return -1 if len(db_files) == 1: log.msg("Found an already initialized database version: %d" % db_version) if db_version < DATABASE_VERSION: log.msg( "Performing schema migration from version %d to version %d" % (db_version, DATABASE_VERSION)) try: migration.perform_schema_migration(db_version) except Exception as exception: log.msg("Migration failure: %s" % exception) log.msg("Verbose exception traceback:") etype, value, tback = sys.exc_info() log.msg('\n'.join( traceback.format_exception(etype, value, tback))) return -1 log.msg("Migration completed with success!") else: log.msg('Performing data update') migration.perform_data_update( os.path.abspath( os.path.join(GLSettings.db_path, 'glbackend-%d.db' % DATABASE_VERSION))) return db_version