Skip to content

Commit

Permalink
Revert "Fix: update method in unskript_ctl_db" (#993)
Browse files Browse the repository at this point in the history
  • Loading branch information
jayasimha-raghavan-unskript authored Feb 16, 2024
1 parent 6006cde commit 1e81014
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ def k8s_get_memory_utilization_of_services_printer(output):
def convert_memory_to_bytes(memory_value) -> int:
if not memory_value:
return 0

units = {
'K': 1000,
'M': 1000 * 1000,
Expand Down Expand Up @@ -157,4 +156,4 @@ def k8s_get_memory_utilization_of_services(handle, namespace: str = "", threshol
except Exception as e:
raise e

return (False, exceeding_services) if exceeding_services else (True, [])
return (False, exceeding_services) if exceeding_services else (True, [])
40 changes: 4 additions & 36 deletions unskript-ctl/unskript_ctl_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import zlib
import sqlite3
import json
import zlib
import ZODB.FileStorage

from unskript_ctl_factory import DatabaseFactory, UnskriptFactory
Expand Down Expand Up @@ -107,43 +106,12 @@ def update(self, **kwargs):
self.collection_name = kwargs.get('collection_name')
if 'data' in kwargs:
data = kwargs.get('data')

with self.db.transaction() as connection:
root = connection.root()
stored_data = root.get(self.collection_name, {})
migrated_to_new_format = False
if isinstance(stored_data, bytes):
self.logger.info("Found database to be using old schema, will migrate to new format")
try:
decompressed_data = zlib.decompress(stored_data)
old_data = json.loads(decompressed_data.decode('utf-8'))
# Make sure the DB has the schema version to indicate the migration
root['schema_version'] = SCHEMA_VERSION
migrated_to_new_format = True
except zlib.error as e:
self.logger.error(f"Error decompressing data: {e}")
return False
except json.JSONDecodeError as e:
self.logger.error(f"Error decoding JSON data: {e}")
return False
elif isinstance(stored_data, dict):
if 'schema_version' not in root.keys():
# In case we dont find it, lets add the schema version
root['schema_version'] = SCHEMA_VERSION
old_data = stored_data
else:
self.logger.error("Unsupported data type in database")
return False
try:
old_data.update(data)
root[self.collection_name] = old_data
connection.transaction_manager.commit()
if migrated_to_new_format:
self.logger.info("DB migrated successfully")
except Exception as e:
self.logger.debug("Exception Occurred when updating ")
return False

old_data = root[self.collection_name]
old_data.update(data)
root[self.collection_name] = old_data
connection.transaction_manager.commit()
connection.close()

del root
Expand Down

0 comments on commit 1e81014

Please sign in to comment.