From 97830e3a0cab6f1e3ee757e6178b99b863d161b1 Mon Sep 17 00:00:00 2001 From: jsinovassin <58434978+jsinovassin@users.noreply.github.com> Date: Thu, 31 Oct 2024 17:26:34 +0100 Subject: [PATCH] UNOMI-862: fix scope check on migration (#706) * UNOMI-862: fix scope check on migration * replace space by - in the scope name --- .../unomi/shell/migration/utils/HttpUtils.java | 2 +- .../cxs/migration/migrate-2.0.0-20-scopes.groovy | 15 +++++++++------ .../2.0.0/scope_search_by_item_id.json | 8 ++++++++ 3 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 tools/shell-commands/src/main/resources/requestBody/2.0.0/scope_search_by_item_id.json diff --git a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/HttpUtils.java b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/HttpUtils.java index 503714a18..faa341e8a 100644 --- a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/HttpUtils.java +++ b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/HttpUtils.java @@ -124,7 +124,7 @@ public static String executePostRequest(CloseableHttpClient httpClient, String u httpPost.addHeader("accept", "application/json"); if (jsonData != null) { - StringEntity input = new StringEntity(jsonData); + StringEntity input = new StringEntity(jsonData, "UTF-8"); input.setContentType("application/json"); httpPost.setEntity(input); } diff --git a/tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.0.0-20-scopes.groovy b/tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.0.0-20-scopes.groovy index 7b291abcd..053090b54 100644 --- a/tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.0.0-20-scopes.groovy +++ b/tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.0.0-20-scopes.groovy @@ -25,6 +25,7 @@ MigrationContext context = migrationContext def jsonSlurper = new JsonSlurper() String searchScopesRequest = MigrationUtils.resourceAsString(bundleContext,"requestBody/2.0.0/scope_search.json") String saveScopeRequestBulk = MigrationUtils.resourceAsString(bundleContext, "requestBody/2.0.0/scope_save_bulk.ndjson") +String searchScopeById = MigrationUtils.resourceAsString(bundleContext, "requestBody/2.0.0/scope_search_by_item_id.json") String esAddress = context.getConfigString("esAddress") String indexPrefix = context.getConfigString("indexPrefix") String scopeIndex = indexPrefix + "-scope" @@ -52,20 +53,22 @@ context.performMigrationStep("2.0.0-create-scopes-from-existing-events", () -> { bucket -> { // Filter empty scope from existing events if (bucket.key) { + def scopeKey = bucket.key.replaceAll(" ", "-") // check that the scope doesn't already exists def scopeAlreadyExists = false try { - def existingScope = jsonSlurper.parseText(HttpUtils.executeGetRequest(context.getHttpClient(), esAddress + "/" + scopeIndex + "/_doc/" + bucket.key, null)); - scopeAlreadyExists = existingScope.found + context.printMessage("Check if " + scopeKey + " exists") + def existingScope = jsonSlurper.parseText(HttpUtils.executePostRequest(context.getHttpClient(), esAddress + "/" + scopeIndex + "/_search/", searchScopeById.replace("##scope##", scopeKey), null)); + scopeAlreadyExists = existingScope.hits.total.value > 0 } catch (HttpRequestException e) { // can happen in case response code > 400 due to item not exist in ElasticSearch } if (!scopeAlreadyExists) { - context.printMessage("Scope: " + bucket.key + " will be created") - bulkSaveRequest.append(saveScopeRequestBulk.replace("##scope##", bucket.key)) + context.printMessage("Scope: " + scopeKey + " will be created") + bulkSaveRequest.append(saveScopeRequestBulk.replace("##scope##", scopeKey)) } else { - context.printMessage("Scope: " + bucket.key + " already exists, won't be created") + context.printMessage("Scope: " + scopeKey + " already exists, won't be created") } } } @@ -77,4 +80,4 @@ context.performMigrationStep("2.0.0-create-scopes-from-existing-events", () -> { HttpUtils.executeGetRequest(context.getHttpClient(), esAddress + "/" + scopeIndex + "/_refresh", null) } } -}) \ No newline at end of file +}) diff --git a/tools/shell-commands/src/main/resources/requestBody/2.0.0/scope_search_by_item_id.json b/tools/shell-commands/src/main/resources/requestBody/2.0.0/scope_search_by_item_id.json new file mode 100644 index 000000000..54362daf4 --- /dev/null +++ b/tools/shell-commands/src/main/resources/requestBody/2.0.0/scope_search_by_item_id.json @@ -0,0 +1,8 @@ +{ + "query": { + "size": 1, + "term": { + "itemId": "##scope##" + } + } +}