Created
December 9, 2013 10:44
-
-
Save jeje/7870383 to your computer and use it in GitHub Desktop.
Classe Spring lançant Flyway à l'initialisation du contexte.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.googlecode.flyway.core.Flyway; | |
import com.googlecode.flyway.core.metadatatable.MetaDataTableRow; | |
import com.googlecode.flyway.core.migration.SchemaVersion; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.InitializingBean; | |
import javax.sql.DataSource; | |
import java.util.List; | |
public class DatabaseInitialization implements InitializingBean { | |
private DataSource dataSource; | |
private static final Logger logger = LoggerFactory.getLogger(DatabaseInitialization.class); | |
@Override | |
public void afterPropertiesSet() throws Exception { | |
Flyway flyway = new Flyway(); | |
flyway.setDataSource(dataSource); | |
if (flyway.status() == null) | |
flyway.init(); | |
logger.info("Database analysis: in progress..."); | |
List<MetaDataTableRow> history = flyway.history(); | |
for (MetaDataTableRow version : history) { | |
logger.info("\tFound migration {}", version.getDescription()); | |
} | |
logger.info("Database analysis: done"); | |
SchemaVersion target = flyway.getTarget(); | |
SchemaVersion current = flyway.status().getVersion(); | |
if (target.compareTo(current) != 0) { | |
logger.info("Database migration: in progress..."); | |
flyway.migrate(); | |
logger.info("Database migration: done!"); | |
} else { | |
logger.info("Database up-to-date."); | |
} | |
} | |
public void setDataSource(DataSource dataSource) { | |
this.dataSource = dataSource; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment