Spring:Flyway
Aus Alexander's Wiki
Flyway in SpringBoot-Projekt
In pom.xml
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>${flyway.version}</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-mysql</artifactId>
<version>${flyway.version}</version>
</dependency>
In application.yaml
spring:
flyway:
enabled: true
baselineOnMigrate: true
check-location: true
# schemas: public
# unfortunately flyway cannot distinct between mysql and mariadb by {vendor}
locations: classpath:db/migration/{vendor},classpath:db/migration/all #, filesystem:/opt/migration/{vendor}
user: user
password: password
Flyway in maven-Skript
flyway.conf in
- installDir/conf/flyway.conf
- userhome/flyway.conf
- workingDir/flyway.conf
# Settings are simple key-value pairs
flyway.key=value
# Single line comment start with a hash
# Long properties can be split over multiple lines by ending each line with a backslash
flyway.locations=filesystem:my/really/long/path/folder1,\
filesystem:my/really/long/path/folder2,\
filesystem:my/really/long/path/folder3
# These are some example settings
flyway.url=jdbc:mydb://mydatabaseurl
flyway.schemas=schema1,schema2
flyway.placeholders.keyABC=valueXYZ
Befehle:
mvn flyway:info
mvn flyway:migrate
mvn flyway:repair
mvn compile flyway:migrate -D'flyway.configFiles'=${project.build.directory}/target/classes/configs/lea-dev-plateg.conf
Flyway callbacks
package de.itzbund.egesetz.bmi.lea.persistence;
import lombok.extern.log4j.Log4j2;
import org.flywaydb.core.api.callback.Callback;
import org.flywaydb.core.api.callback.Context;
import org.flywaydb.core.api.callback.Event;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
@Log4j2
public class FlywayCallback implements Callback
{
private static final String CLEANUP_FAILED_MIGRATIONS = "DELETE FROM flyway_schema_history WHERE success = 0;";
private static final String GET_FAILED_MIGRATIONS = "SELECT count(*) FROM flyway_schema_history WHERE success = 0;";
@Override
public boolean supports(Event event, Context context)
{
// case BEFORE_VALIDATE no more necessary
return false;
}
@Override
public boolean canHandleInTransaction(Event event, Context context)
{
return false;
}
@Override
public void handle(Event event, Context context)
{
switch (event)
{
case BEFORE_VALIDATE:
repairFailedMigrations(context.getConnection());
break;
default:
}
}
private void repairFailedMigrations(Connection connection)
{
if (connection != null)
{
if (getFailedMigrations(connection) > 0)
{
try (Statement statement = connection.createStatement())
{
statement.execute(CLEANUP_FAILED_MIGRATIONS);
} catch (SQLException e)
{
log.debug("Got Exception while cleanup flyway {}, but will continue.", e);
}
} else
{
log.debug("No failed migrations detected.");
}
} else
{
log.debug("Connection is null");
}
}
private int getFailedMigrations(Connection connection)
{
int fails = 0;
try (Statement statement = connection.createStatement())
{
ResultSet rs = statement.executeQuery(GET_FAILED_MIGRATIONS);
if (rs != null)
{
rs.next();
fails = rs.getInt(1);
log.debug("{} previous failed scripts where found. We will cleanup.", fails);
}
} catch (SQLException e)
{
log.debug("Got Exception while getting informations on flyway {}, but will continue.", e);
}
return fails;
}
@Override
public String getCallbackName()
{
return FlywayCallback.class.getSimpleName();
}
}