Permalink
Browse files

use flyway to migrate database on app start

  • Loading branch information...
Pauli Kärpänoja
Pauli Kärpänoja committed Dec 8, 2015
1 parent 40b1be3 commit b62cf3680cce84086e51bfb54f967d48a3cc4c2d
View
@@ -225,6 +225,12 @@
<version>${jasypt.version}</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>3.2.1</version>
</dependency>
<!-- Querydsl -->
<dependency>
<groupId>com.mysema.querydsl</groupId>
@@ -16,6 +16,7 @@
import fi.om.initiative.dto.initiative.InitiativeState;
import fi.om.initiative.util.ReviewHistoryType;
import org.apache.commons.io.FileUtils;
import org.flywaydb.core.Flyway;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
@@ -25,11 +26,13 @@
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.transaction.PlatformTransactionManager;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.sql.DataSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
@Configuration
@@ -114,5 +117,25 @@ public Connection get() {
});
}
@PostConstruct
public void updateDatabase() throws IOException {
try {
Flyway flyway = new Flyway();
flyway.setEncoding("UTF-8");
flyway.setTable("flyway_schema");
flyway.setLocations("db/migration");
flyway.setSchemas(env.getProperty(PropertyNames.jdbcUser));
flyway.setDataSource(
env.getProperty(PropertyNames.jdbcURL),
env.getProperty(PropertyNames.flywayUser),
env.getProperty(PropertyNames.flywayPassword));
flyway.setBaselineOnMigrate(true);
flyway.migrate();
} catch (Exception e) {
log.error("FAILED TO MIGRATE DATABASE", e);
}
}
}
@@ -20,6 +20,10 @@ private PropertyNames() {}
public static final String jdbcUser = "jdbc.user";
public static final String jdbcPassword = "jdbc.password";
public static final String flywayPassword = "flyway.password";
public static final String flywayUser = "flyway.user";
public static final String vetumaURL = "vetuma.url";
@@ -13,6 +13,10 @@ jdbc.url = jdbc:postgresql://localhost:5432/initdb
jdbc.user = initiative
jdbc.password = password
# createuser -s -P flyway_user
flyway.user = flyway_user
flyway.password = password
security.registeredUserSecret = abcdEFGH1234
# Default timestamp for use without resource filtering (i.e. Eclipse)
@@ -1,14 +1,20 @@
package fi.om.initiative.conf;
import fi.om.initiative.dao.TestHelper;
import org.flywaydb.core.Flyway;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.servlet.ServletContext;
import java.io.IOException;
import static org.mockito.Mockito.mock;
@Configuration
@@ -26,5 +32,25 @@ public ServletContext servletContext() {
public TestHelper testHelper() {
return new TestHelper();
}
@Inject
private Environment env;
// Tests are using own schema. When migrations have run to test-schema, we have to grant some rights to them for test-user.
@PostConstruct
public void updateDatabase() throws IOException {
Flyway flyway = new Flyway();
flyway.setEncoding("UTF-8");
flyway.setTable("flyway_schema");
flyway.setLocations("testmigration");
flyway.setSchemas(env.getProperty(PropertyNames.jdbcUser));
flyway.setDataSource(
env.getProperty(PropertyNames.jdbcURL),
env.getProperty(PropertyNames.flywayUser),
env.getProperty(PropertyNames.flywayPassword));
flyway.setBaselineOnMigrate(true);
flyway.migrate();
}
}
@@ -0,0 +1,4 @@
GRANT CONNECT, TEMP ON DATABASE initdb TO initest;
GRANT USAGE ON SCHEMA initest TO initest;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA initest TO initest;
GRANT ALL ON ALL SEQUENCES IN SCHEMA initest TO initest;

0 comments on commit b62cf36

Please sign in to comment.