Skip to content

Commit

Permalink
Improve registry.py logging and revised save function for consistent …
Browse files Browse the repository at this point in the history
…error handling
  • Loading branch information
Kevin Ngai committed Jul 4, 2024
1 parent 149cd9f commit 7ad3e00
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions woudc_data_registry/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ def query_distinct_by_fields(
:returns: list of distinct values
"""

LOGGER.debug(f'Querying distinct values for {domain}')

conditions = []
target_fields = values.keys()

LOGGER.debug(f'Querying distinct values by fields {target_fields} for {domain}')

for field in target_fields:
table_field = getattr(obj, field)
if case_insensitive:
Expand Down Expand Up @@ -318,32 +318,29 @@ def update_by_field(

def save(self, obj=None):
"""
helper function to save object to registry
Helper function to save object to registry.
:param obj: object to save (default None)
:returns: void
"""
if obj is None:
LOGGER.warning('obj is none while trying to save, skipping')
return

registry_config = config.EXTRAS.get('registry', {})

try:
if obj is not None:
flag_name = '_'.join([obj.__tablename__, 'enabled'])
if registry_config.get(flag_name, True):
self.session.add(obj)
# self.session.merge(obj)
else:
LOGGER.info(f'Registry persistence for model {obj.__tablename__} disabled, skipping') # noqa
return

try:
self.session.commit()
except SQLAlchemyError as err:
LOGGER.error(f'Failed to persist {obj} due to: {err}')
self.session.rollback()

LOGGER.debug(f'Saving {obj}')
except DataError as err:
flag_name = '_'.join([obj.__tablename__, 'enabled'])
if registry_config.get(flag_name, True):
self.session.add(obj) # Use merge if needed: self.session.merge(obj)
else:
LOGGER.info(f'Registry persistence for model {obj.__tablename__} disabled, skipping')
return

LOGGER.debug(f'Committing save of {obj}')
self.session.commit()

except (SQLAlchemyError, DataError) as err:
LOGGER.error(f'Failed to save to registry: {err}')
self.session.rollback()

Expand Down

0 comments on commit 7ad3e00

Please sign in to comment.