Browse Source

data-upgrade 更新配置,支持初始基线版本数据

deason 3 months ago
parent
commit
1685e5d634

+ 6 - 0
data-upgrade/src/main/java/com/qmth/boot/data/upgrade/config/DataUpgradeConstants.java

@@ -8,6 +8,12 @@ public interface DataUpgradeConstants {
 
     String FIRST_INSTALL = "com.qmth.data.upgrade.first-install";
 
+    String BASELINE = "com.qmth.data.upgrade.baseline";
+
+    String BASELINE_APP_CODE = "com.qmth.data.upgrade.baseline-app-code";
+
+    String BASELINE_APP_VERSION = "com.qmth.data.upgrade.baseline-app-version";
+
     String SUPER_DB_USERNAME = "com.qmth.data.upgrade.super-username";
 
     String SUPER_DB_PASSWORD = "com.qmth.data.upgrade.super-password";

+ 7 - 14
data-upgrade/src/main/java/com/qmth/boot/data/upgrade/config/DataUpgradeListener.java

@@ -7,7 +7,6 @@ import com.qmth.boot.data.upgrade.service.DataUpgradeService;
 import com.qmth.boot.data.upgrade.utils.ClassHelper;
 import com.qmth.boot.data.upgrade.utils.VersionComparator;
 import com.zaxxer.hikari.HikariDataSource;
-import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.boot.context.event.ApplicationPreparedEvent;
@@ -100,7 +99,13 @@ public class DataUpgradeListener implements ApplicationListener<ApplicationPrepa
             TransactionTemplate transactionTemplate = new TransactionTemplate(new DataSourceTransactionManager(dataSource));
 
             if (!this.existBootAppInfo(jdbcTemplate)) {
-                throw new RuntimeException("【应用信息表】不存在!");
+                if (!properties.getBaseline()) {
+                    throw new RuntimeException("【应用信息表】不存在!");
+                }
+
+                // 初始应用信息表和基线数据
+                log.warn("初始基线应用码:{} 初始基线应用版本号:{}", properties.getBaselineAppCode(), properties.getBaselineAppVersion());
+                this.initBootAppInfo(jdbcTemplate, properties.getBaselineAppCode(), properties.getBaselineAppVersion());
             }
 
             BootAppInfo appInfo = this.queryBootAppInfo(jdbcTemplate, properties.getAppCode());
@@ -198,12 +203,6 @@ public class DataUpgradeListener implements ApplicationListener<ApplicationPrepa
     }
 
     private void updateBootAppInfo(JdbcTemplate jdbcTemplate, String appCode, String appVersion) {
-        if (StringUtils.isBlank(appCode)) {
-            throw new RuntimeException("appCode的值不能为空!");
-        }
-        if (StringUtils.isBlank(appVersion)) {
-            throw new RuntimeException("appVersion的值不能为空!");
-        }
         try {
             jdbcTemplate.update("update boot_app_info set app_version = ? where app_code = ?", appVersion, appCode);
         } catch (Exception e) {
@@ -222,12 +221,6 @@ public class DataUpgradeListener implements ApplicationListener<ApplicationPrepa
     }
 
     private void initBootAppInfo(JdbcTemplate jdbcTemplate, String appCode, String appVersion) {
-        if (StringUtils.isBlank(appCode)) {
-            throw new RuntimeException("appCode的值不能为空!");
-        }
-        if (StringUtils.isBlank(appVersion)) {
-            throw new RuntimeException("appVersion的值不能为空!");
-        }
         try {
             String createTableSql = "create table if not exists boot_app_info (" +
                     " app_code varchar(20) not null," +

+ 45 - 2
data-upgrade/src/main/java/com/qmth/boot/data/upgrade/config/DataUpgradeProperties.java

@@ -1,5 +1,6 @@
 package com.qmth.boot.data.upgrade.config;
 
+import com.qmth.boot.data.upgrade.utils.VersionComparator;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.boot.jdbc.DatabaseDriver;
 import org.springframework.core.env.ConfigurableEnvironment;
@@ -21,6 +22,21 @@ public class DataUpgradeProperties implements DataUpgradeConstants {
      */
     private Boolean firstInstall;
 
+    /**
+     * “应用信息表”不存在时,是否指定基线数据,默认值为false
+     */
+    private Boolean baseline;
+
+    /**
+     * 指定基线数据时,基线-应用码
+     */
+    private String baselineAppCode;
+
+    /**
+     * 指定基线数据时,基线-应用版本号
+     */
+    private String baselineAppVersion;
+
     /**
      * DB用户名
      */
@@ -59,16 +75,31 @@ public class DataUpgradeProperties implements DataUpgradeConstants {
         }
 
         this.basePackage = environment.getProperty(BASE_PACKAGE, String.class, "com.qmth");
+
         this.firstInstall = environment.getProperty(FIRST_INSTALL, Boolean.class, false);
 
+        this.baseline = environment.getProperty(BASELINE, Boolean.class, false);
+        this.baselineAppCode = environment.getProperty(BASELINE_APP_CODE, String.class);
+        this.baselineAppVersion = environment.getProperty(BASELINE_APP_VERSION, String.class);
+
+        if (baseline) {
+            if (StringUtils.isBlank(baselineAppCode)) {
+                throw new RuntimeException(BASELINE_APP_CODE + " 配置值不能为空!");
+            }
+
+            if (!VersionComparator.isValidVersion(baselineAppVersion)) {
+                throw new RuntimeException(BASELINE_APP_VERSION + "=" + baselineAppVersion + " 配置格式不正确!要求格式如:数字.数字.数字");
+            }
+        }
+
         this.appCode = environment.getProperty(APP_CODE, String.class);
         if (StringUtils.isBlank(appCode)) {
             throw new RuntimeException(APP_CODE + " 配置值不能为空!");
         }
 
         this.appVersion = environment.getProperty(APP_VERSION, String.class);
-        if (StringUtils.isBlank(appVersion)) {
-            throw new RuntimeException(APP_VERSION + " 配置值不能为空!");
+        if (!VersionComparator.isValidVersion(appVersion)) {
+            throw new RuntimeException(APP_VERSION + "=" + appVersion + " 配置格式不正确!要求格式如:数字.数字.数字");
         }
 
         this.dbUsername = environment.getProperty(DB_USERNAME, String.class);
@@ -117,6 +148,18 @@ public class DataUpgradeProperties implements DataUpgradeConstants {
         return firstInstall;
     }
 
+    public Boolean getBaseline() {
+        return baseline;
+    }
+
+    public String getBaselineAppCode() {
+        return baselineAppCode;
+    }
+
+    public String getBaselineAppVersion() {
+        return baselineAppVersion;
+    }
+
     public String getDbUsername() {
         return dbUsername;
     }

+ 2 - 2
data-upgrade/src/main/java/com/qmth/boot/data/upgrade/utils/VersionComparator.java

@@ -9,10 +9,10 @@ public class VersionComparator implements Comparator<String> {
     @Override
     public int compare(String v1, String v2) {
         if (!VersionComparator.isValidVersion(v1)) {
-            throw new IllegalArgumentException("版本号" + v1 + "格式不正确,格式如:数字.数字.数字!");
+            throw new IllegalArgumentException("版本号" + v1 + "格式不正确!要求格式如:数字.数字.数字");
         }
         if (!VersionComparator.isValidVersion(v2)) {
-            throw new IllegalArgumentException("版本号" + v2 + "格式不正确,格式如:数字.数字.数字!");
+            throw new IllegalArgumentException("版本号" + v2 + "格式不正确!要求格式如:数字.数字.数字");
         }
 
         // 分割版本号为数字段