Skip to content

Instantly share code, notes, and snippets.

@jeje
Created December 9, 2013 10:44
Show Gist options
  • Save jeje/7870383 to your computer and use it in GitHub Desktop.
Save jeje/7870383 to your computer and use it in GitHub Desktop.
Classe Spring lançant Flyway à l'initialisation du contexte.
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