2017-01-11 225 views
1

當沒有要遷移的遷移時可以防止beforeMigrate回調腳本運行,因爲該模式已經是最新的嗎?在遷移之前防止遷移到最新模式

下面的代碼(在應用程序啓動時執行): -

Flyway flyway = new Flyway(); 

flyway.setDataSource(url, user, password); 

flyway.setLocations(scriptsLocations); 
flyway.setPlaceholders(placeHolders); 

flyway.setBaselineVersionAsString("7.0"); 
flyway.setBaselineOnMigrate(true); 

flyway.migrate(); 

根據日誌遷徙路線運行beforeMigrate回調決定架構之前是最新的,並且沒有遷移到運行。

INFO: Flyway 4.0.3 by Boxfuse 
INFO: Database: jdbc:oracle:thin:... (Oracle 11.2) 
INFO: Successfully validated 8 migrations (execution time 00:00.023s) 
INFO: Executing SQL callback: beforeMigrate 
INFO: Current version of schema "...": 7.0.7 
INFO: Schema "..." is up to date. No migration necessary. 

想要beforeMigrate回調僅在需要遷移時運行。

回答

1

找到了一個簡單的解決方案;使用info來確定是否存在未決遷移並根據結果進行有條件遷移的調用: -

boolean pending = flyway.info().pending().length > 0; 

if (pending) { 
    flyway.migrate(); 
}