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

dgriffon pushed a commit to branch check-ES-indices-before-migration
in repository https://gitbox.apache.org/repos/asf/unomi.git

commit 5a672747f645278ce5f9b1b43d79dd9176307e75
Author: David Griffon <dgrif...@jahia.com>
AuthorDate: Fri Mar 17 13:04:48 2023 +0100

    UNOMI-752 : ensure target indices and alias does not exist before 
performing migration
---
 .../unomi/shell/migration/MigrationException.java  | 26 ++++++++++++++++++++++
 .../shell/migration/service/MigrationContext.java  | 13 +++++++++++
 .../migration/service/MigrationServiceImpl.java    |  4 ++++
 .../migrate-2.2.0-05-indicesReduction.groovy       | 15 ++++++++++++-
 4 files changed, 57 insertions(+), 1 deletion(-)

diff --git 
a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/MigrationException.java
 
b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/MigrationException.java
new file mode 100644
index 000000000..2fdf7c2a7
--- /dev/null
+++ 
b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/MigrationException.java
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.unomi.shell.migration;
+
+/**
+ * Simple exception to handle errors in migration
+ */
+public class MigrationException extends RuntimeException {
+    public MigrationException(String message) {
+        super(message);
+    }
+}
diff --git 
a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/service/MigrationContext.java
 
b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/service/MigrationContext.java
index 21835c31e..7f64b594b 100644
--- 
a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/service/MigrationContext.java
+++ 
b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/service/MigrationContext.java
@@ -198,6 +198,19 @@ public class MigrationContext {
         }
     }
 
+    /**
+     * Same as above without stacktrace
+     * @param msg the message to print out with a newline
+     */
+    public void printException(String msg) {
+        if (session == null) {
+            logger.error(msg);
+        } else {
+            PrintStream writer = session.getConsole();
+            writer.println(msg);
+        }
+    }
+
     /**
      * Get config for property name, in case the property doesn't exist on 
file system config file
      * Best effort will be made to prompt question in karaf shell to get the 
needed information
diff --git 
a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/service/MigrationServiceImpl.java
 
b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/service/MigrationServiceImpl.java
index 9722bb3fd..f0159d947 100644
--- 
a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/service/MigrationServiceImpl.java
+++ 
b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/service/MigrationServiceImpl.java
@@ -26,6 +26,7 @@ import org.apache.http.client.CredentialsProvider;
 import org.apache.http.impl.client.BasicCredentialsProvider;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.karaf.shell.api.console.Session;
+import org.apache.unomi.shell.migration.MigrationException;
 import org.apache.unomi.shell.migration.MigrationService;
 import org.apache.unomi.shell.migration.utils.HttpUtils;
 import org.osgi.framework.Bundle;
@@ -128,6 +129,9 @@ public class MigrationServiceImpl implements 
MigrationService {
                 context.printMessage("Starting execution of: " + 
migrateScript);
                 try {
                     migrateScript.getCompiledScript().run();
+                } catch (MigrationException e) {
+                    context.printException("Error executing: " + 
migrateScript);
+                    throw e;
                 } catch (Exception e) {
                     context.printException("Error executing: " + 
migrateScript, e);
                     throw e;
diff --git 
a/tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.2.0-05-indicesReduction.groovy
 
b/tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.2.0-05-indicesReduction.groovy
index 631f0bced..f410bf00d 100644
--- 
a/tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.2.0-05-indicesReduction.groovy
+++ 
b/tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.2.0-05-indicesReduction.groovy
@@ -1,3 +1,4 @@
+import org.apache.unomi.shell.migration.MigrationException
 import org.apache.unomi.shell.migration.service.MigrationContext
 import org.apache.unomi.shell.migration.utils.HttpUtils
 import org.apache.unomi.shell.migration.utils.MigrationUtils
@@ -42,10 +43,22 @@ def indicesToReduce = [
         exportconfig: [reduceTo: "systemitems", renameId: true],
         rulestats: [reduceTo: "systemitems", renameId: true],
         groovyaction: [reduceTo: "systemitems", renameId: true],
-
         persona: [reduceTo: "profile", renameId: false]
 ]
 
+def mewIndicesAndAliases = ["systemitems":"Index", "session-000001":"Index", 
"event-000001":"Index", "session":"Alias", "event":"Alias"]
+
+// Check env is ready for migration
+context.performMigrationStep("2.2.0-check-env-status", () -> {
+    def currentIndex = new MapEntry();
+    if (mewIndicesAndAliases.any{index -> {
+        currentIndex = index
+        return MigrationUtils.indexExists(context.getHttpClient(), esAddress, 
"${indexPrefix}-${currentIndex.key}")
+    }}) {
+        throw new MigrationException("${currentIndex.value} 
${indexPrefix}-${currentIndex.key} must be removed before perfoming migration")
+    }
+})
+
 context.performMigrationStep("2.2.0-create-systemItems-index", () -> {
     if (!MigrationUtils.indexExists(context.getHttpClient(), esAddress, 
"${indexPrefix}-systemitems")) {
         String mapping = 
MigrationUtils.extractMappingFromBundles(bundleContext, "systemItems.json")

Reply via email to