From 8ee7388d8a34d4e8f9b537f10ff821ad538f96a6 Mon Sep 17 00:00:00 2001 From: Michael Chouinard Date: Thu, 2 May 2024 16:53:31 -0400 Subject: [PATCH] Two small bug fixes --- .../transformation/transform_oracle_data_task.py | 11 +++++++---- api/src/util/datetime_util.py | 3 +++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/api/src/data_migration/transformation/transform_oracle_data_task.py b/api/src/data_migration/transformation/transform_oracle_data_task.py index 54be6c2d1..e18a19726 100644 --- a/api/src/data_migration/transformation/transform_oracle_data_task.py +++ b/api/src/data_migration/transformation/transform_oracle_data_task.py @@ -110,7 +110,7 @@ def process_opportunity( logger.info("Transforming and upserting opportunity", extra=extra) transformed_opportunity = transform_opportunity(source_opportunity, target_opportunity) - self.db_session.add(transformed_opportunity) + self.db_session.merge(transformed_opportunity) if is_insert: self.increment(self.Metrics.TOTAL_RECORDS_INSERTED) @@ -139,13 +139,16 @@ def process_one_to_many_lookup_tables(self) -> None: def transform_opportunity( - source_opportunity: Topportunity, target_opportunity: Opportunity | None + source_opportunity: Topportunity, existing_opportunity: Opportunity | None ) -> Opportunity: log_extra = {"opportunity_id": source_opportunity.opportunity_id} - if target_opportunity is None: + if existing_opportunity is None: logger.info("Creating new opportunity record", extra=log_extra) - target_opportunity = Opportunity(opportunity_id=source_opportunity.opportunity_id) + + # We always create a new opportunity record here and merge it in the calling function + # this way if there is any error doing the transformation, we don't modify the existing one. + target_opportunity = Opportunity(opportunity_id=source_opportunity.opportunity_id) target_opportunity.opportunity_number = source_opportunity.oppnumber target_opportunity.opportunity_title = source_opportunity.opptitle diff --git a/api/src/util/datetime_util.py b/api/src/util/datetime_util.py index 264b19148..dcc152eb6 100644 --- a/api/src/util/datetime_util.py +++ b/api/src/util/datetime_util.py @@ -32,6 +32,9 @@ def adjust_timezone(timestamp: datetime, timezone_str: str) -> datetime: def make_timezone_aware(timestamp: datetime, timezone_str: str) -> datetime: + # First make certain the tzinfo is truly None otherwise pytz will error + # if it actually had a tzinfo this means we're effectively ignoring anything about it. + timestamp.replace(tzinfo=None) new_timezone = pytz.timezone(timezone_str) return new_timezone.localize(timestamp)