Skip to content

Commit

Permalink
execute flyway migrations at start-up
Browse files Browse the repository at this point in the history
  • Loading branch information
goekay committed Aug 8, 2021
1 parent ade6f60 commit d5a6617
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<flyway.version>7.11.3</flyway.version>
<jooq.version>3.15.1</jooq.version>
<cxf.version>3.4.4</cxf.version>
<spring.version>5.3.9</spring.version>
Expand Down Expand Up @@ -81,7 +82,7 @@
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>7.11.3</version>
<version>${flyway.version}</version>

<!-- Must be in the same phase as Jooq -->
<executions>
Expand Down Expand Up @@ -242,6 +243,7 @@
<includes>
<include>webapp/static/**</include>
<include>webapp/WEB-INF/web.xml</include>
<include>db/**</include>
</includes>
</resource>
</resources>
Expand Down Expand Up @@ -671,6 +673,11 @@
<artifactId>jooq</artifactId>
<version>${jooq.version}</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>${flyway.version}</version>
</dependency>

<!-- Test -->
<dependency>
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/de/rwth/idsg/steve/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package de.rwth.idsg.steve;

import de.rwth.idsg.steve.utils.FlywayMigrationRunner;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
Expand All @@ -44,6 +45,8 @@ public Application() {
DateTimeZone.setDefault(DateTimeZone.forID(sc.getTimeZoneId()));
log.info("Date/time zone of the application is set to {}. Current date/time: {}", sc.getTimeZoneId(), DateTime.now());

FlywayMigrationRunner.run(sc);

switch (sc.getProfile()) {
case DEV:
delegate = new SteveDevStarter();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/de/rwth/idsg/steve/SteveConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ public static class DB {
private final String userName;
private final String password;
private final boolean sqlLogging;

public String getJdbcUrl() {
return "jdbc:mysql://" + ip + ":" + port + "/" + schema;
}
}

// Credentials for Web interface access
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private void initDataSource() {
HikariConfig hc = new HikariConfig();

// set standard params
hc.setJdbcUrl("jdbc:mysql://" + dbConfig.getIp() + ":" + dbConfig.getPort() + "/" + dbConfig.getSchema());
hc.setJdbcUrl(dbConfig.getJdbcUrl());
hc.setUsername(dbConfig.getUserName());
hc.setPassword(dbConfig.getPassword());

Expand Down
43 changes: 43 additions & 0 deletions src/main/java/de/rwth/idsg/steve/utils/FlywayMigrationRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package de.rwth.idsg.steve.utils;

import de.rwth.idsg.steve.SteveConfiguration;
import org.flywaydb.core.api.configuration.FluentConfiguration;

/**
* @author Sevket Goekay <sevketgokay@gmail.com>
* @since 08.08.2021
*/
public class FlywayMigrationRunner {

private static final String INIT_SQL = "SET default_storage_engine=InnoDB;";
private static final String BOOKKEEPING_TABLE = "schema_version";
private static final String LOCATION_OF_MIGRATIONS = "db/migration";

public static void run(SteveConfiguration sc) {
try {
getConfig(sc.getDb()).load().migrate();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

/**
* This configuration should be kept in-sync with the one of the flyway-maven-plugin in pom.xml
*/
private static FluentConfiguration getConfig(SteveConfiguration.DB dbConfig) {
FluentConfiguration c = new FluentConfiguration();

c.initSql(INIT_SQL);
c.outOfOrder(true);
c.table(BOOKKEEPING_TABLE);
c.cleanDisabled(true);
c.schemas(dbConfig.getSchema());
c.defaultSchema(dbConfig.getSchema());
c.locations(LOCATION_OF_MIGRATIONS);
c.dataSource(dbConfig.getJdbcUrl(), dbConfig.getUserName(), dbConfig.getPassword());

return c;
}
}

0 comments on commit d5a6617

Please sign in to comment.