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

gongchao pushed a commit to branch flyway
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git

commit 458e359c82f4bc65b14a315be48b01c59730e1bb
Author: tomsun28 <[email protected]>
AuthorDate: Sat Apr 27 22:06:07 2024 +0800

    support flyway upgrade sql
    
    Signed-off-by: tomsun28 <[email protected]>
---
 .../manager/JsonStringListAttributeConverter.java  |  7 ++-
 .../org/apache/hertzbeat/common/util/JsonUtil.java | 12 +++++
 manager/pom.xml                                    | 14 ++++++
 .../manager/config/FlywayConfiguration.java        | 52 ++++++++++++++++++++++
 manager/src/main/resources/application.yml         |  8 ++++
 .../db/migration/h2/V160__update_column.sql        | 13 ++++++
 .../db/migration/mysql/V160__update_column.sql     | 13 ++++++
 .../migration/postgresql/V160__update_column.sql   | 13 ++++++
 material/licenses/backend/LICENSE                  |  3 ++
 pom.xml                                            | 17 +++++++
 10 files changed, 151 insertions(+), 1 deletion(-)

diff --git 
a/common/src/main/java/org/apache/hertzbeat/common/entity/manager/JsonStringListAttributeConverter.java
 
b/common/src/main/java/org/apache/hertzbeat/common/entity/manager/JsonStringListAttributeConverter.java
index c112313cd..4600b2fa5 100644
--- 
a/common/src/main/java/org/apache/hertzbeat/common/entity/manager/JsonStringListAttributeConverter.java
+++ 
b/common/src/main/java/org/apache/hertzbeat/common/entity/manager/JsonStringListAttributeConverter.java
@@ -42,11 +42,16 @@ public class JsonStringListAttributeConverter implements 
AttributeConverter<List
         if (StringUtils.isBlank(dbData)) {
             return List.of();
         }
+        if (!JsonUtil.isJsonStr(dbData)) {
+            return List.of(dbData);
+        }
         TypeReference<List<String>> typeReference = new TypeReference<>() {
         };
         List<String> stringList = JsonUtil.fromJson(dbData, typeReference);
         if (stringList == null && !dbData.isEmpty()) {
             return List.of(dbData);
-        } else return stringList;
+        } else {
+            return stringList;
+        }
     }
 }
diff --git 
a/common/src/main/java/org/apache/hertzbeat/common/util/JsonUtil.java 
b/common/src/main/java/org/apache/hertzbeat/common/util/JsonUtil.java
index f5617ddc6..461bab2f7 100644
--- a/common/src/main/java/org/apache/hertzbeat/common/util/JsonUtil.java
+++ b/common/src/main/java/org/apache/hertzbeat/common/util/JsonUtil.java
@@ -93,4 +93,16 @@ public class JsonUtil {
             return null;
         }
     }
+    
+    public static boolean isJsonStr(String jsonStr) {
+        if (!StringUtils.hasText(jsonStr)) {
+            return false;
+        }
+        try {
+            OBJECT_MAPPER.readTree(jsonStr);
+            return true;
+        } catch (Exception ignored) {
+            return false;
+        }
+    }
 }
diff --git a/manager/pom.xml b/manager/pom.xml
index 1ad254952..801c9ff3c 100644
--- a/manager/pom.xml
+++ b/manager/pom.xml
@@ -116,6 +116,19 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-jdbc</artifactId>
         </dependency>
+        <!-- database migration -->
+        <dependency>
+            <groupId>org.flywaydb</groupId>
+            <artifactId>flyway-core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.flywaydb</groupId>
+            <artifactId>flyway-mysql</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.flywaydb</groupId>
+            <artifactId>flyway-database-postgresql</artifactId>
+        </dependency>
         <!-- mysql -->
         <dependency>
             <groupId>mysql</groupId>
@@ -194,6 +207,7 @@
                     <include>*.xml</include>
                     <include>banner.txt</include>
                     <include>define/**</include>
+                    <include>db/**</include>
                     <include>templates/**</include>
                     <include>**/*.html</include>
                 </includes>
diff --git 
a/manager/src/main/java/org/apache/hertzbeat/manager/config/FlywayConfiguration.java
 
b/manager/src/main/java/org/apache/hertzbeat/manager/config/FlywayConfiguration.java
new file mode 100644
index 000000000..b3437e3bd
--- /dev/null
+++ 
b/manager/src/main/java/org/apache/hertzbeat/manager/config/FlywayConfiguration.java
@@ -0,0 +1,52 @@
+/*
+ * 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.hertzbeat.manager.config;
+
+import org.flywaydb.core.Flyway;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import 
org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer;
+import org.springframework.boot.autoconfigure.flyway.FlywayProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.DependsOn;
+
+/**
+ * flyway database migration config
+ */
+@Configuration
+@ConditionalOnProperty(prefix = "spring.flyway", name = "enabled", havingValue 
= "true")
+public class FlywayConfiguration {
+
+    @Bean
+    public FlywayMigrationInitializer flywayInitializer(Flyway flyway) {
+        return new FlywayMigrationInitializer(flyway, (f) -> {
+        });
+    }
+
+    static class Dummy {
+    }
+
+    @Bean
+    @DependsOn("entityManagerFactory")
+    Dummy delayedFlywayInitializer(Flyway flyway, FlywayProperties 
flywayProperties) {
+        if (flywayProperties.isEnabled()) {
+            flyway.migrate();
+        }
+        return new Dummy();
+    }
+}
diff --git a/manager/src/main/resources/application.yml 
b/manager/src/main/resources/application.yml
index b568f9f82..e390e66bc 100644
--- a/manager/src/main/resources/application.yml
+++ b/manager/src/main/resources/application.yml
@@ -82,6 +82,14 @@ spring:
         logging:
           level: SEVERE
           
+  flyway:
+    enabled: true
+    clean-disabled: true
+    baseline-on-migrate: true
+    baseline-version: 1
+    locations:
+      - classpath:db/migration/{vendor}
+    
   mail:
     # Attention: this is mail server address. 
     # 请注意此为邮件服务器地址:qq邮箱为 smtp.qq.com qq 企业邮箱为 smtp.exmail.qq.com
diff --git a/manager/src/main/resources/db/migration/h2/V160__update_column.sql 
b/manager/src/main/resources/db/migration/h2/V160__update_column.sql
new file mode 100644
index 000000000..5cad3e02f
--- /dev/null
+++ b/manager/src/main/resources/db/migration/h2/V160__update_column.sql
@@ -0,0 +1,13 @@
+-- ensure every sql can rerun without error
+
+ALTER TABLE HZB_PARAM ADD COLUMN IF NOT EXISTS "value" VARCHAR(255);
+UPDATE HZB_PARAM SET param_value = "value" WHERE "value" IS NOT NULL AND 
param_value IS NULL;
+ALTER TABLE HZB_PARAM DROP COLUMN "value";
+
+ALTER TABLE HZB_TAG ADD COLUMN IF NOT EXISTS "value" VARCHAR(255);
+UPDATE HZB_TAG SET tag_value = "value" WHERE "value" IS NOT NULL AND tag_value 
IS NULL;
+ALTER TABLE HZB_TAG DROP COLUMN "value";
+
+ALTER TABLE HZB_STATUS_PAGE_HISTORY ADD COLUMN IF NOT EXISTS "unknown" 
VARCHAR(255);
+UPDATE HZB_STATUS_PAGE_HISTORY SET unknowing = "unknown" WHERE "unknown" IS 
NOT NULL AND unknowing IS NULL;
+ALTER TABLE HZB_STATUS_PAGE_HISTORY DROP COLUMN "unknown";
diff --git 
a/manager/src/main/resources/db/migration/mysql/V160__update_column.sql 
b/manager/src/main/resources/db/migration/mysql/V160__update_column.sql
new file mode 100644
index 000000000..2211317eb
--- /dev/null
+++ b/manager/src/main/resources/db/migration/mysql/V160__update_column.sql
@@ -0,0 +1,13 @@
+-- ensure every sql can rerun without error
+
+ALTER TABLE HZB_PARAM ADD COLUMN IF NOT EXISTS `value` VARCHAR(255);
+UPDATE HZB_PARAM SET param_value = `value` WHERE `value` IS NOT NULL AND 
param_value IS NULL;
+ALTER TABLE HZB_PARAM DROP COLUMN `value`;
+
+ALTER TABLE HZB_TAG ADD COLUMN IF NOT EXISTS `value` VARCHAR(255);
+UPDATE HZB_TAG SET tag_value = `value` WHERE `value` IS NOT NULL AND tag_value 
IS NULL;
+ALTER TABLE HZB_TAG DROP COLUMN `value`;
+
+ALTER TABLE HZB_STATUS_PAGE_HISTORY ADD COLUMN IF NOT EXISTS `unknown` 
VARCHAR(255);
+UPDATE HZB_STATUS_PAGE_HISTORY SET unknowing = `unknown` WHERE `unknown` IS 
NOT NULL AND unknowing IS NULL;
+ALTER TABLE HZB_STATUS_PAGE_HISTORY DROP COLUMN `unknown`;
diff --git 
a/manager/src/main/resources/db/migration/postgresql/V160__update_column.sql 
b/manager/src/main/resources/db/migration/postgresql/V160__update_column.sql
new file mode 100644
index 000000000..2211317eb
--- /dev/null
+++ b/manager/src/main/resources/db/migration/postgresql/V160__update_column.sql
@@ -0,0 +1,13 @@
+-- ensure every sql can rerun without error
+
+ALTER TABLE HZB_PARAM ADD COLUMN IF NOT EXISTS `value` VARCHAR(255);
+UPDATE HZB_PARAM SET param_value = `value` WHERE `value` IS NOT NULL AND 
param_value IS NULL;
+ALTER TABLE HZB_PARAM DROP COLUMN `value`;
+
+ALTER TABLE HZB_TAG ADD COLUMN IF NOT EXISTS `value` VARCHAR(255);
+UPDATE HZB_TAG SET tag_value = `value` WHERE `value` IS NOT NULL AND tag_value 
IS NULL;
+ALTER TABLE HZB_TAG DROP COLUMN `value`;
+
+ALTER TABLE HZB_STATUS_PAGE_HISTORY ADD COLUMN IF NOT EXISTS `unknown` 
VARCHAR(255);
+UPDATE HZB_STATUS_PAGE_HISTORY SET unknowing = `unknown` WHERE `unknown` IS 
NOT NULL AND unknowing IS NULL;
+ALTER TABLE HZB_STATUS_PAGE_HISTORY DROP COLUMN `unknown`;
diff --git a/material/licenses/backend/LICENSE 
b/material/licenses/backend/LICENSE
index 963e21187..94e02c951 100644
--- a/material/licenses/backend/LICENSE
+++ b/material/licenses/backend/LICENSE
@@ -410,6 +410,9 @@ The text of each license is the standard Apache 2.0 license.
     https://mvnrepository.com/artifact/org.webjars/swagger-ui/5.10.3
     
https://mvnrepository.com/artifact/com.google.flatbuffers/flatbuffers-java/1.12.0
     https://mvnrepository.com/artifact/org.apache.commons/commons-jexl3/3.2.1
+    https://mvnrepository.com/artifact/org.flywaydb/flyway-core/10.11.1
+    https://mvnrepository.com/artifact/org.flywaydb/flyway-mysql/10.11.1
+    
https://mvnrepository.com/artifact/org.flywaydb/flyway-database-postgresql/10.11.1
 
 
 ========================================================================
diff --git a/pom.xml b/pom.xml
index cfb442ab2..c20bad2ab 100644
--- a/pom.xml
+++ b/pom.xml
@@ -132,6 +132,7 @@
         <h2.version>2.2.224</h2.version>
         <taos-jdbcdriver.version>3.0.0</taos-jdbcdriver.version>
         <iotdb-session.version>0.13.3</iotdb-session.version>
+        <flyway.version>10.11.1</flyway.version>
         <commons-collections4.version>4.4</commons-collections4.version>
         <commons-jexl3>3.2.1</commons-jexl3>
         <commons-net>3.10.0</commons-net>
@@ -234,6 +235,22 @@
                 <artifactId>commons-jexl3</artifactId>
                 <version>${commons-jexl3}</version>
             </dependency>
+            <!-- database migration -->
+            <dependency>
+                <groupId>org.flywaydb</groupId>
+                <artifactId>flyway-core</artifactId>
+                <version>${flyway.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>org.flywaydb</groupId>
+                <artifactId>flyway-mysql</artifactId>
+                <version>${flyway.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>org.flywaydb</groupId>
+                <artifactId>flyway-database-postgresql</artifactId>
+                <version>${flyway.version}</version>
+            </dependency>
             <!-- mysql -->
             <dependency>
                 <groupId>mysql</groupId>


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to