Commit bf5bef03 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

allow to inject custom data script in flyway migration scripts

parent 75c63f8f
......@@ -30,6 +30,9 @@ public class ConfigurationHolder {
@Value("${database.connectionQuery:select 1}")
private String dbConnectionQuery;
@Value("${database.pathToDataScripts}")
private String pathToDataScripts;
public String getDbUri() {
return dbUri;
}
......@@ -77,4 +80,12 @@ public class ConfigurationHolder {
public void setDbConnectionQuery(String dbConnectionQuery) {
this.dbConnectionQuery = dbConnectionQuery;
}
public String getPathToDataScripts() {
return pathToDataScripts;
}
public void setPathToDataScripts(String pathToDataScripts) {
this.pathToDataScripts = pathToDataScripts;
}
}
package lcsb.mapviewer.persist;
import java.sql.Connection;
import java.util.*;
import javax.sql.DataSource;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.flywaydb.core.Flyway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.init.DatabasePopulator;
......@@ -13,6 +16,8 @@ import org.springframework.stereotype.Service;
@Service
public class CustomDatabasePopulator implements DatabasePopulator {
private Logger logger = LogManager.getLogger();
private ConfigurationHolder config;
private DataSource dataSource;
......@@ -29,10 +34,16 @@ public class CustomDatabasePopulator implements DatabasePopulator {
if (config.getDbDriver().equals("org.hsqldb.jdbcDriver")) {
migrationFolder = "hsql";
}
List<String> locations = new ArrayList<>(
Arrays.asList("classpath:lcsb.mapviewer.persist/migration/" + migrationFolder,
"classpath:db.migration." + migrationFolder));
if (config.getPathToDataScripts() != null) {
logger.info("Additional SQL scripts located at: " + config.getPathToDataScripts());
locations.add("filesystem:" + config.getPathToDataScripts());
}
Flyway.configure().dataSource(config.getDbUri(), config.getDbUsername(), config.getDbPassword())
.baselineVersion("12.1.0")
.locations("classpath:lcsb.mapviewer.persist/migration/" + migrationFolder,
"classpath:db.migration." + migrationFolder)
.locations(locations.toArray(new String[0]))
.outOfOrder(true)
.baselineOnMigrate(true)
.validateOnMigrate(false)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment