以代码的形式实现flyway的迁移

如果某个项目被另一个项目以jar包的形式依赖,此项目的flyway实现如下

 1 package com.enmo.dbaas.firewall.common.config;
 2 
 3 import org.flywaydb.core.Flyway;
 4 import org.flywaydb.core.api.configuration.FluentConfiguration;
 5 import org.springframework.stereotype.Component;
 6 import com.enmo.dbaas.firewall.common.utils.SpringContextProvider;
 7 import lombok.extern.slf4j.Slf4j;
 8 
 9 /**
10  * @ClassName: FlywayConfig.java
11  * @Description:
12  * @Author guo.yu
13  * @Version V1.0.0
14  * @Date 2019-05-12 19:46:10
15  */
16 @Component
17 @Slf4j
18 public class FlywayConfig {
19 
20     private static final String FLYWAY_TAB_NAME = "dbaas_firewall_flyway_schema_history";
21     
22     private static final String FLYWAY_LOCATION = "db/firewall/migration";
23 
24     public FlywayConfig(SpringContextProvider provider) {
25         Flyway defFlyway = null;
26         try {
27             defFlyway = provider.getBean(Flyway.class);
28         } catch (Exception e) {
29             log.warn("flyway is not enabled.");
30         }
31         if (defFlyway != null) {
32             firewallMigrate(defFlyway);
33         }
34 
35     }
36 
37     @SuppressWarnings("deprecation")
38     public void firewallMigrate(Flyway defFlyway) {
39         FluentConfiguration config = new FluentConfiguration();
40 
41         config.dataSource(defFlyway.getDataSource())
42                .baselineOnMigrate(defFlyway.isBaselineOnMigrate())
43                .locations(FLYWAY_LOCATION)
44                .table(FLYWAY_TAB_NAME)
45                //允许运行之前版本(相对于schema表最新版本)的schema文件
46                .outOfOrder(true)
47                .load()
48                .migrate();
49         
50         
51     }
52 
53 }

 

posted @ 2019-08-27 12:44  guoAIrong  阅读(466)  评论(0编辑  收藏  举报