Skip to content

Commit

Permalink
✨ 兼容捐赠版数据文件功能
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaojun1998 committed Aug 11, 2022
1 parent 5a816b1 commit edf4395
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
package im.zhaojun.zfile.common.config;import cn.hutool.core.util.StrUtil;import org.flywaydb.core.Flyway;import org.springframework.context.annotation.Configuration;import javax.annotation.PostConstruct;import javax.annotation.Resource;import javax.sql.DataSource;import java.sql.SQLException;import java.util.Locale;/** * 数据库初始化 * * @author zhaojun */@Configurationpublic class FlywayDbInitializer { public static final String[] SUPPORT_DB_TYPE = new String[]{"mysql", "sqlite"}; @Resource private DataSource dataSource; /** * 启动时根据当前数据库类型执行数据库初始化 */ @PostConstruct public void migrateFlyway() { try { String databaseProductName = dataSource.getConnection().getMetaData().getDatabaseProductName(); String dbType = databaseProductName.toLowerCase(Locale.ROOT); // 检查当前数据库类型是否支持 if (!StrUtil.equalsAnyIgnoreCase(dbType, SUPPORT_DB_TYPE)) { throw new RuntimeException("不支持的数据库类型 [" + dbType + "]"); } Flyway load = Flyway.configure().dataSource(dataSource).locations("db/migration-" + dbType).load(); load.migrate(); } catch (SQLException e) { e.printStackTrace(); } }}
package im.zhaojun.zfile.common.config;import cn.hutool.core.util.StrUtil;import org.flywaydb.core.Flyway;import org.springframework.context.annotation.Configuration;import javax.annotation.PostConstruct;import javax.annotation.Resource;import javax.sql.DataSource;import java.sql.SQLException;import java.util.Locale;/** * 数据库初始化 * * @author zhaojun */@Configurationpublic class FlywayDbInitializer { public static final String[] SUPPORT_DB_TYPE = new String[]{"mysql", "sqlite"}; @Resource private DataSource dataSource; /** * 启动时根据当前数据库类型执行数据库初始化 */ @PostConstruct public void migrateFlyway() { try { String databaseProductName = dataSource.getConnection().getMetaData().getDatabaseProductName(); String dbType = databaseProductName.toLowerCase(Locale.ROOT); // 检查当前数据库类型是否支持 if (!StrUtil.equalsAnyIgnoreCase(dbType, SUPPORT_DB_TYPE)) { throw new RuntimeException("不支持的数据库类型 [" + dbType + "]"); } Flyway load = Flyway.configure().dataSource(dataSource).outOfOrder(true).locations("db/migration-" + dbType).load(); load.migrate(); } catch (SQLException e) { e.printStackTrace(); } }}
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ spring.datasource.url=jdbc:sqlite:${zfile.db.path}

spring.profiles.active=prod


# ingroe zfile-pro upgrade sql
spring.flyway.ignore-migration-patterns=V5__.*,V6__.*

# -------------- database config end --------------

0 comments on commit edf4395

Please sign in to comment.