From 9fdbef91ad6571ddb253a737a63aa57be5db9bf2 Mon Sep 17 00:00:00 2001 From: SimplifiedPrivacy Date: Sun, 15 Nov 2026 21:18:59 -0500 Subject: [PATCH] Introduced Forced all key sync for missing SQL failure options --- core/controllers/SyncController.py | 29 ++++++++++++++----- .../controllers/profile_state/data_filters.py | 28 +++++++++++++++++- core/models/manage/insert.py | 6 ++-- core/services/sync/sync_service.py | 21 +++++++++++++- 4 files changed, 73 insertions(+), 11 deletions(-) diff --git a/core/controllers/SyncController.py b/core/controllers/SyncController.py index f8bfe62..ba8e295 100644 --- a/core/controllers/SyncController.py +++ b/core/controllers/SyncController.py @@ -56,6 +56,7 @@ APP_CODES = { "librewolf": 5 } + def call_legacy_insert_function(key: str, new_data: dict): func = LEGACY_SQL_FUNCT_DICT.get(key) if func is None: @@ -73,14 +74,26 @@ def get_orm_model(key) -> bool: return ORM_TABLES[key] +def _sync_success(): + notification = "Finshed sync." + Notifications.emit(EventType.STATUS_UPDATE, notification) + Notifications.emit(EventType.SYNC_COMPLETE, notification) + global_state_marks_sync_completed() + return Result(valid=True, message=notification) -def new_sync(client_observer: ClientObserver, connection_observer: ConnectionObserver) -> Result: + +def new_sync(client_observer: ClientObserver, connection_observer: ConnectionObserver, force_all: bool = False) -> Result: client_observer.notify('synchronizing', "Fetching list of new data ..") + logger.info("Starting sync") #################################### # METADATA. Should we even sync? #################################### - metadata_result = coordinate_cache_sync(client_observer, connection_observer) + metadata_result = coordinate_cache_sync( + client_observer=client_observer, + connection_observer=connection_observer, + force_all=force_all + ) # Outright Error: if not metadata_result["success"]: @@ -182,13 +195,15 @@ def new_sync(client_observer: ClientObserver, connection_observer: ConnectionObs if total_skipped == 0: client_observer.notify('synchronized', "Fetch & Save Complete!") + + # forced_sync being true, forces this to none: + if filtered_metadata is None: + return _sync_success() + + # otherwise evaluate saving of metadata: save_successful = save_metadata(filtered_metadata) if save_successful: - notification = "Finshed sync." - Notifications.emit(EventType.STATUS_UPDATE, notification) - Notifications.emit(EventType.SYNC_COMPLETE, notification) - global_state_marks_sync_completed() - return Result(valid=True, message=notification) + return _sync_success() else: error_msg = "Finshed sync, but had issues with the saving of metadata for next time." logger.error(error_msg) diff --git a/core/controllers/profile_state/data_filters.py b/core/controllers/profile_state/data_filters.py index ee5585d..fa2a950 100644 --- a/core/controllers/profile_state/data_filters.py +++ b/core/controllers/profile_state/data_filters.py @@ -1,6 +1,8 @@ +from core.observers.ObserverStash import ObserverStash +from core.controllers.SyncController import new_sync from core.controllers.ProfileController import ProfileController from core.services.payment_phase.payment_state import PaymentData -from core.models.BaseProfile import BaseProfile as Profile +# from core.models.BaseProfile import BaseProfile as Profile from core.models.orm_models.Plan import Plan from core.models.orm_calls.model_neutral_tools import get_all_for_model from core.models.orm_calls.location_calls import get_profile_location_data, get_profile_location_data_by_id @@ -83,6 +85,30 @@ def get_plans_for_profile(profile_id: int) -> Result: billing_data.protocol_choice = chosen_protocol # Return plans for that protocol: + all_plans_result = get_all_plans_by_protocol(chosen_protocol) + + if all_plans_result.valid: + return all_plans_result + + if all_plans_result.error_type == ResultError.MISSING_DATA: + return force_sync_then_try_again(chosen_protocol) + else: + return all_plans_result + + +def force_sync_then_try_again(chosen_protocol: ProtocolChoice) -> Result: + doc_string = "Sync with ALL keys forced to download. Then try fetching the plan data again" + + logger.info(doc_string) + observer_stash = ObserverStash() + result = new_sync( + client_observer=observer_stash.client_observer, + connection_observer=observer_stash.connection_observer, + force_all=True) + + if not result.valid: + return result.wrap("couldn't force sync all keys, to get the SQL data for plans.") + return get_all_plans_by_protocol(chosen_protocol) diff --git a/core/models/manage/insert.py b/core/models/manage/insert.py index 0726a7f..9c259ff 100644 --- a/core/models/manage/insert.py +++ b/core/models/manage/insert.py @@ -84,7 +84,6 @@ def _wrapped_insert(model_class: Type, data_list: dict | list, session: Session, # Get valid field names upfront valid_fields = {col.name for col in model_class.__table__.columns} - logger.info(f"valid_fields are {valid_fields}") # Filter data proactively filtered_data_list = [] @@ -104,7 +103,10 @@ def _wrapped_insert(model_class: Type, data_list: dict | list, session: Session, try: for filtered_json in filtered_data_list: - logger.info(f"inserting {filtered_json}") + + if filtered_json is None: + logger.error(f"Critical error of filtered_json being None for {model_class}") + instance = model_class(**filtered_json) session.add(instance) session.commit() diff --git a/core/services/sync/sync_service.py b/core/services/sync/sync_service.py index 4c6d5c3..2c85354 100644 --- a/core/services/sync/sync_service.py +++ b/core/services/sync/sync_service.py @@ -144,10 +144,25 @@ def _log_what_changed(new_model_types: list, changed_tables: list): logger.info("No models changed between versions.") +def _force_all_keys(new_data: dict) -> dict: + error_msg = "Forcing a sync of all keys" + logger.info(error_msg) + changed_tables = [key for key in new_data] + return { + "success": True, + "changed_tables": changed_tables, + "new_model_types": [], + "error": error_msg, + "filtered_metadata": None + } + + + # Highest Level in this Flow: def coordinate_cache_sync( client_observer: Optional[ClientObserver] = None, - connection_observer: Optional[ConnectionObserver] = None + connection_observer: Optional[ConnectionObserver] = None, + force_all: bool = False ) -> dict: """ Fetch version metadata from API and return changed/new model keys. @@ -218,6 +233,10 @@ def coordinate_cache_sync( new_data = original_data.get('data', original_data) print(f"new_data is {new_data}") + # proceed with force all: + if force_all: + return _force_all_keys(new_data) + if "version" not in new_data: error_msg = f"API returned invalid metadata: missing 'version' key. The data is {new_data}" logger.error(error_msg)