This is an automated email from the ASF dual-hosted git repository.

jsinovassinnaik pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/unomi.git


The following commit(s) were added to refs/heads/master by this push:
     new 97830e3a0 UNOMI-862: fix scope check on migration (#706)
97830e3a0 is described below

commit 97830e3a0cab6f1e3ee757e6178b99b863d161b1
Author: jsinovassin <58434978+jsinovas...@users.noreply.github.com>
AuthorDate: Thu Oct 31 17:26:34 2024 +0100

    UNOMI-862: fix scope check on migration (#706)
    
    * UNOMI-862: fix scope check on migration
    
    * replace space by - in the scope name
---
 .../org/apache/unomi/shell/migration/utils/HttpUtils.java |  2 +-
 .../META-INF/cxs/migration/migrate-2.0.0-20-scopes.groovy | 15 +++++++++------
 .../requestBody/2.0.0/scope_search_by_item_id.json        |  8 ++++++++
 3 files changed, 18 insertions(+), 7 deletions(-)

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 class HttpUtils {
         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##"
+    }
+  }
+}

Reply via email to