From 042949f1c135ee6049f73f75bb149fe30ad8d9a3 Mon Sep 17 00:00:00 2001 From: abraunegg Date: Fri, 24 Jun 2022 14:54:32 +1000 Subject: [PATCH] Fix 'foreign key constraint failed' when using OneDrive Business Shared Folders (#2017) In some OneDrive Business scenarios, the shared folder /delta response lacks the 'root' drive details. When this occurs, this creates the following error: A database statement execution error occurred: foreign key constraint failed. Ensure we query independently the root details for this shared folder and ensure that it is added before we process the /delta response --- src/sync.d | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/sync.d b/src/sync.d index 06c78047..103f367b 100644 --- a/src/sync.d +++ b/src/sync.d @@ -1776,6 +1776,44 @@ final class SyncEngine } } + // In some OneDrive Business scenarios, the shared folder /delta response lacks the 'root' drive details + // When this occurs, this creates the following error: A database statement execution error occurred: foreign key constraint failed + // Ensure we query independently the root details for this shared folder and ensure that it is added before we process the /delta response + if ((!nationalCloudDeployment) && (driveId!= defaultDriveId) && (syncBusinessFolders)) { + // fetch this driveId root details to ensure we add this to the database for this remote drive + JSONValue rootData; + + try { + rootData = onedrive.getDriveIdRoot(driveId); + } catch (OneDriveException e) { + log.vdebug("rootData = onedrive.getDriveIdRoot(driveId) generated a OneDriveException"); + // HTTP request returned status code 504 (Gateway Timeout) or 429 retry + if ((e.httpStatusCode == 429) || (e.httpStatusCode == 504)) { + // HTTP request returned status code 429 (Too Many Requests). We need to leverage the response Retry-After HTTP header to ensure minimum delay until the throttle is removed. + if (e.httpStatusCode == 429) { + log.vdebug("Retrying original request that generated the OneDrive HTTP 429 Response Code (Too Many Requests) - retrying applicable request"); + handleOneDriveThrottleRequest(); + } + if (e.httpStatusCode == 504) { + log.vdebug("Retrying original request that generated the HTTP 504 (Gateway Timeout) - retrying applicable request"); + Thread.sleep(dur!"seconds"(30)); + } + // Retry original request by calling function again to avoid replicating any further error handling + rootData = onedrive.getDriveIdRoot(driveId); + + } else { + // There was a HTTP 5xx Server Side Error + displayOneDriveErrorMessage(e.msg, getFunctionName!({})); + // Must exit here + exit(-1); + } + } + + // apply this root drive data + applyDifference(rootData, driveId, true); + } + + // Process /delta response from OneDrive // is changes a valid JSON response if (changes.type() == JSONType.object) { // Are there any changes to process?