join flyway
parent
3edd62d6f5
commit
9895e463bf
|
@ -1,82 +1,82 @@
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>br.com.rjconsultores</groupId>
|
<groupId>br.com.rjconsultores</groupId>
|
||||||
<artifactId>Flyway</artifactId>
|
<artifactId>Flyway</artifactId>
|
||||||
<version>1.106.0</version>
|
<version>1.106.0</version>
|
||||||
|
|
||||||
<distributionManagement>
|
<distributionManagement>
|
||||||
<repository>
|
<repository>
|
||||||
<id>rj-releases</id>
|
<id>rj-releases</id>
|
||||||
<url>http://52.5.53.15:8081/nexus/content/repositories/releases/</url>
|
<url>http://52.5.53.15:8081/nexus/content/repositories/releases/</url>
|
||||||
</repository>
|
</repository>
|
||||||
</distributionManagement>
|
</distributionManagement>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<sourceDirectory>src</sourceDirectory>
|
<sourceDirectory>src</sourceDirectory>
|
||||||
|
|
||||||
<resources>
|
<resources>
|
||||||
<resource>
|
<resource>
|
||||||
<directory>${basedir}/src</directory>
|
<directory>${basedir}/src</directory>
|
||||||
<includes>
|
<includes>
|
||||||
<include>*.*</include>
|
<include>*.*</include>
|
||||||
<include>db/**</include>
|
<include>db/**</include>
|
||||||
</includes>
|
</includes>
|
||||||
</resource>
|
</resource>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>3.8.1</version>
|
<version>3.8.1</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>1.8</source>
|
<source>1.8</source>
|
||||||
<target>1.8</target>
|
<target>1.8</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.flywaydb</groupId>
|
<groupId>org.flywaydb</groupId>
|
||||||
<artifactId>flyway-maven-plugin</artifactId>
|
<artifactId>flyway-maven-plugin</artifactId>
|
||||||
<version>3.2.1</version>
|
<version>3.2.1</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<validateMigrationNaming>true</validateMigrationNaming>
|
<validateMigrationNaming>true</validateMigrationNaming>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
<finalName>${project.artifactId}</finalName>
|
<finalName>${project.artifactId}</finalName>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.logging.log4j</groupId>
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
<artifactId>log4j-core</artifactId>
|
<artifactId>log4j-core</artifactId>
|
||||||
<version>2.17.1</version>
|
<version>2.17.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.flywaydb</groupId>
|
<groupId>org.flywaydb</groupId>
|
||||||
<artifactId>flyway-core</artifactId>
|
<artifactId>flyway-core</artifactId>
|
||||||
<version>4.2.0</version>
|
<version>4.2.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax.servlet</groupId>
|
<groupId>javax.servlet</groupId>
|
||||||
<artifactId>javax.servlet-api</artifactId>
|
<artifactId>javax.servlet-api</artifactId>
|
||||||
<version>4.0.0</version>
|
<version>4.0.0</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.oracle.database.jdbc</groupId>
|
<groupId>com.oracle.database.jdbc</groupId>
|
||||||
<artifactId>ojdbc8</artifactId>
|
<artifactId>ojdbc8</artifactId>
|
||||||
<version>21.7.0.0</version>
|
<version>21.7.0.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
|
@ -1,3 +1,3 @@
|
||||||
Manifest-Version: 1.0
|
Manifest-Version: 1.0
|
||||||
Class-Path:
|
Class-Path:
|
||||||
|
|
|
@ -1,430 +1,430 @@
|
||||||
package com.rjconsultores.ventaboletos;
|
package com.rjconsultores.ventaboletos;
|
||||||
|
|
||||||
import java.io.BufferedWriter;
|
import java.io.BufferedWriter;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.flywaydb.core.Flyway;
|
import org.flywaydb.core.Flyway;
|
||||||
import org.flywaydb.core.api.callback.FlywayCallback;
|
import org.flywaydb.core.api.callback.FlywayCallback;
|
||||||
|
|
||||||
import com.rjconsultores.ventaboletos.vo.FlyWayCustomDetail;
|
import com.rjconsultores.ventaboletos.vo.FlyWayCustomDetail;
|
||||||
|
|
||||||
public class FlyWay {
|
public class FlyWay {
|
||||||
private static final Logger log = LogManager.getLogger(FlyWay.class);
|
private static final Logger log = LogManager.getLogger(FlyWay.class);
|
||||||
|
|
||||||
private static final FlyWay INSTANCE = new FlyWay();
|
private static final FlyWay INSTANCE = new FlyWay();
|
||||||
|
|
||||||
final private static String TABELA_FLYWAY = "schema_version_cst";
|
final private static String TABELA_FLYWAY = "schema_version_cst";
|
||||||
final private static String AMBIENTE_DEV = "dev";
|
final private static String AMBIENTE_DEV = "dev";
|
||||||
final private static String AMBIENTE_CONSULTA = "consulta";
|
final private static String AMBIENTE_CONSULTA = "consulta";
|
||||||
final File JBOSS_DATA = getJBossDataDirectory();
|
final File JBOSS_DATA = getJBossDataDirectory();
|
||||||
|
|
||||||
private DataSource dataSource = null;
|
private DataSource dataSource = null;
|
||||||
|
|
||||||
private String url = null;
|
private String url = null;
|
||||||
private String user = null;
|
private String user = null;
|
||||||
private String password = null;
|
private String password = null;
|
||||||
|
|
||||||
private String location = "db.migration";
|
private String location = "db.migration";
|
||||||
private String oldies = "db.backup.oracle";
|
private String oldies = "db.backup.oracle";
|
||||||
|
|
||||||
private FlyWay() {
|
private FlyWay() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static FlyWay getInstance() {
|
public static FlyWay getInstance() {
|
||||||
return INSTANCE;
|
return INSTANCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
private File getJBossDataDirectory() {
|
private File getJBossDataDirectory() {
|
||||||
try {
|
try {
|
||||||
return new File(System.getProperty("jboss.server.data.dir"));
|
return new File(System.getProperty("jboss.server.data.dir"));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void defineLocation(final String location) {
|
public void defineLocation(final String location) {
|
||||||
this.location = location;
|
this.location = location;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void defineDataSource(final DataSource dataSource) {
|
public void defineDataSource(final DataSource dataSource) {
|
||||||
this.dataSource = dataSource;
|
this.dataSource = dataSource;
|
||||||
|
|
||||||
fixFiles(dataSource);
|
fixFiles(dataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void defineProperties(final String url, final String user, final String password) {
|
public void defineProperties(final String url, final String user, final String password) {
|
||||||
this.url = url;
|
this.url = url;
|
||||||
this.user = user;
|
this.user = user;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void fixFiles(final DataSource ds) {
|
private void fixFiles(final DataSource ds) {
|
||||||
try {
|
try {
|
||||||
Connection c = dataSource.getConnection();
|
Connection c = dataSource.getConnection();
|
||||||
Statement s = c.createStatement();
|
Statement s = c.createStatement();
|
||||||
|
|
||||||
fixVersion(s, "20161710.1833", "20161017.1833", "V20161017_1833__mantis8112.sql");
|
fixVersion(s, "20161710.1833", "20161017.1833", "V20161017_1833__mantis8112.sql");
|
||||||
fixVersion(s, "20161910.0934", "20161019.0934", "V20161019_0934__mantis7907.sql");
|
fixVersion(s, "20161910.0934", "20161019.0934", "V20161019_0934__mantis7907.sql");
|
||||||
fixVersion(s, "20162410.1119", "20161024.1119", "V20161024_1119__mantis7904.sql");
|
fixVersion(s, "20162410.1119", "20161024.1119", "V20161024_1119__mantis7904.sql");
|
||||||
|
|
||||||
s.close();
|
s.close();
|
||||||
c.close();
|
c.close();
|
||||||
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void fixVersion(final Statement s, final String oldVersion, final String newVersion, final String newScript) throws SQLException {
|
private void fixVersion(final Statement s, final String oldVersion, final String newVersion, final String newScript) throws SQLException {
|
||||||
ResultSet rs = s.executeQuery("select count(*) as total from \"schema_version\" where \"version\" like '" + oldVersion + "'");
|
ResultSet rs = s.executeQuery("select count(*) as total from \"schema_version\" where \"version\" like '" + oldVersion + "'");
|
||||||
|
|
||||||
int total = 0;
|
int total = 0;
|
||||||
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
total = rs.getInt("total");
|
total = rs.getInt("total");
|
||||||
}
|
}
|
||||||
|
|
||||||
rs.close();
|
rs.close();
|
||||||
|
|
||||||
if (total > 0) {
|
if (total > 0) {
|
||||||
log.info("Fix version " + oldVersion + " to new version " + newVersion + " and new script " + newScript);
|
log.info("Fix version " + oldVersion + " to new version " + newVersion + " and new script " + newScript);
|
||||||
|
|
||||||
StringBuilder sql = new StringBuilder("");
|
StringBuilder sql = new StringBuilder("");
|
||||||
sql.append(" update \"schema_version\"");
|
sql.append(" update \"schema_version\"");
|
||||||
sql.append(" set \"version\" = '" + newVersion + "', \"script\" = '" + newScript + "'");
|
sql.append(" set \"version\" = '" + newVersion + "', \"script\" = '" + newScript + "'");
|
||||||
sql.append(" where \"version\" like '" + oldVersion + "'");
|
sql.append(" where \"version\" like '" + oldVersion + "'");
|
||||||
|
|
||||||
s.executeUpdate(sql.toString());
|
s.executeUpdate(sql.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean start() {
|
public boolean start() {
|
||||||
return start(null, null, false);
|
return start(null, null, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean start(Boolean isReloadFyway) {
|
public boolean start(Boolean isReloadFyway) {
|
||||||
return start(null, null, isReloadFyway);
|
return start(null, null, isReloadFyway);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean startCustom() {
|
public boolean startCustom() {
|
||||||
String location = "db.performance";
|
String location = "db.performance";
|
||||||
|
|
||||||
if (JBOSS_DATA != null) {
|
if (JBOSS_DATA != null) {
|
||||||
File data = new File(JBOSS_DATA, "flyway");
|
File data = new File(JBOSS_DATA, "flyway");
|
||||||
|
|
||||||
if (!data.exists()) {
|
if (!data.exists()) {
|
||||||
data.mkdirs();
|
data.mkdirs();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!validarScripts(data)) {
|
if (!validarScripts(data)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
location = "filesystem:" + data.getPath();
|
location = "filesystem:" + data.getPath();
|
||||||
}
|
}
|
||||||
|
|
||||||
return start(location, TABELA_FLYWAY, false);
|
return start(location, TABELA_FLYWAY, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean start(final String customLocation, final String customTable, Boolean isReloadFlyway) {
|
public boolean start(final String customLocation, final String customTable, Boolean isReloadFlyway) {
|
||||||
log.info("Executando Flyway...");
|
log.info("Executando Flyway...");
|
||||||
|
|
||||||
try{
|
try{
|
||||||
String ambiente = System.getProperty("ambiente") ==null || System.getProperty("ambiente").isEmpty() ? "" : System.getProperty("ambiente");
|
String ambiente = System.getProperty("ambiente") ==null || System.getProperty("ambiente").isEmpty() ? "" : System.getProperty("ambiente");
|
||||||
if( (AMBIENTE_DEV.equalsIgnoreCase(ambiente) && AMBIENTE_CONSULTA.equalsIgnoreCase(ambiente)) && Boolean.FALSE.equals(isReloadFlyway) ) {
|
if( (AMBIENTE_DEV.equalsIgnoreCase(ambiente) && AMBIENTE_CONSULTA.equalsIgnoreCase(ambiente)) && Boolean.FALSE.equals(isReloadFlyway) ) {
|
||||||
log.warn("Flyway não executado. Ambiente = dev ou consulta");
|
log.warn("Flyway não executado. Ambiente = dev ou consulta");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
final Flyway flyway = new Flyway();
|
final Flyway flyway = new Flyway();
|
||||||
|
|
||||||
if (customTable != null) {
|
if (customTable != null) {
|
||||||
flyway.setTable(customTable);
|
flyway.setTable(customTable);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (customLocation != null) {
|
if (customLocation != null) {
|
||||||
flyway.setLocations(customLocation);
|
flyway.setLocations(customLocation);
|
||||||
} else if (this.location != null && !this.location.equals("db.migration")) {
|
} else if (this.location != null && !this.location.equals("db.migration")) {
|
||||||
flyway.setLocations(this.location);
|
flyway.setLocations(this.location);
|
||||||
}else {
|
}else {
|
||||||
flyway.setLocations(this.oldies, this.location);
|
flyway.setLocations(this.oldies, this.location);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dataSource != null) {
|
if (dataSource != null) {
|
||||||
flyway.setDataSource(dataSource);
|
flyway.setDataSource(dataSource);
|
||||||
} else {
|
} else {
|
||||||
flyway.setDataSource(url, user, password);
|
flyway.setDataSource(url, user, password);
|
||||||
|
|
||||||
dataSource = flyway.getDataSource();
|
dataSource = flyway.getDataSource();
|
||||||
}
|
}
|
||||||
|
|
||||||
execute(flyway);
|
execute(flyway);
|
||||||
log.info("Flyway executado.");
|
log.info("Flyway executado.");
|
||||||
|
|
||||||
}catch(Throwable t){
|
}catch(Throwable t){
|
||||||
log.error("Erro ao executar o Flyway",t);
|
log.error("Erro ao executar o Flyway",t);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void execute(final Flyway flyway) {
|
public void execute(final Flyway flyway) {
|
||||||
flyway.setValidateOnMigrate(false);
|
flyway.setValidateOnMigrate(false);
|
||||||
flyway.setIgnoreFutureMigrations(true);
|
flyway.setIgnoreFutureMigrations(true);
|
||||||
flyway.setOutOfOrder(true);
|
flyway.setOutOfOrder(true);
|
||||||
flyway.setBaselineOnMigrate(true);
|
flyway.setBaselineOnMigrate(true);
|
||||||
|
|
||||||
if (flyway.getTable().equals(TABELA_FLYWAY)) {
|
if (flyway.getTable().equals(TABELA_FLYWAY)) {
|
||||||
FlyWayCallback errorCallback = new FlyWayCallback();
|
FlyWayCallback errorCallback = new FlyWayCallback();
|
||||||
|
|
||||||
FlywayCallback[] callbacks = new FlywayCallback[]{ errorCallback };
|
FlywayCallback[] callbacks = new FlywayCallback[]{ errorCallback };
|
||||||
flyway.setCallbacks(callbacks);
|
flyway.setCallbacks(callbacks);
|
||||||
|
|
||||||
boolean reexecute = false;
|
boolean reexecute = false;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
reexecute = false;
|
reexecute = false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
flyway.migrate();
|
flyway.migrate();
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
if (errorCallback.getScriptError() != null && JBOSS_DATA != null) {
|
if (errorCallback.getScriptError() != null && JBOSS_DATA != null) {
|
||||||
reexecute = true;
|
reexecute = true;
|
||||||
|
|
||||||
log.error("[Flyway] Erro ao executar o script: " + errorCallback.getScriptError() + ". Este script sera desabilitado na tabela FLYWAY_SCRIPTS.");
|
log.error("[Flyway] Erro ao executar o script: " + errorCallback.getScriptError() + ". Este script sera desabilitado na tabela FLYWAY_SCRIPTS.");
|
||||||
|
|
||||||
File scriptFile = new File(JBOSS_DATA, "flyway");
|
File scriptFile = new File(JBOSS_DATA, "flyway");
|
||||||
scriptFile = new File(scriptFile, errorCallback.getScriptError());
|
scriptFile = new File(scriptFile, errorCallback.getScriptError());
|
||||||
|
|
||||||
if (scriptFile.exists()) {
|
if (scriptFile.exists()) {
|
||||||
boolean success = scriptFile.delete();
|
boolean success = scriptFile.delete();
|
||||||
|
|
||||||
log.info("[Flyway] Excluindo fisicamente o script: " + errorCallback.getScriptError() + ". " + (success ? "OK" : "ERROR"));
|
log.info("[Flyway] Excluindo fisicamente o script: " + errorCallback.getScriptError() + ". " + (success ? "OK" : "ERROR"));
|
||||||
|
|
||||||
if (!success) {
|
if (!success) {
|
||||||
reexecute = false;
|
reexecute = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Connection c = null;
|
Connection c = null;
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
c = flyway.getDataSource().getConnection();
|
c = flyway.getDataSource().getConnection();
|
||||||
ps = c.prepareStatement("update flyway_scripts set activo = 0 where nome like ?");
|
ps = c.prepareStatement("update flyway_scripts set activo = 0 where nome like ?");
|
||||||
ps.setString(1, errorCallback.getScriptError().replace(".sql", ""));
|
ps.setString(1, errorCallback.getScriptError().replace(".sql", ""));
|
||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
|
|
||||||
} catch (SQLException e1) {
|
} catch (SQLException e1) {
|
||||||
} finally {
|
} finally {
|
||||||
if (ps != null) {
|
if (ps != null) {
|
||||||
try { ps.close(); } catch (SQLException e1) {}
|
try { ps.close(); } catch (SQLException e1) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c != null) {
|
if (c != null) {
|
||||||
try { c.close(); } catch (SQLException e1) {}
|
try { c.close(); } catch (SQLException e1) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
flyway.repair();
|
flyway.repair();
|
||||||
}
|
}
|
||||||
} while (reexecute);
|
} while (reexecute);
|
||||||
} else {
|
} else {
|
||||||
flyway.migrate();
|
flyway.migrate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean validarScripts(File data) {
|
private boolean validarScripts(File data) {
|
||||||
Map<String, FlyWayCustomDetail> scripts = new HashMap<String, FlyWayCustomDetail>(0);
|
Map<String, FlyWayCustomDetail> scripts = new HashMap<String, FlyWayCustomDetail>(0);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
boolean existe = existeSchema(dataSource.getConnection());
|
boolean existe = existeSchema(dataSource.getConnection());
|
||||||
|
|
||||||
if (existe) {
|
if (existe) {
|
||||||
sb.append(" select nome as nome_arquivo, sql as sql_arquivo, sql_erro as erro, datahora_execucao as datahora");
|
sb.append(" select nome as nome_arquivo, sql as sql_arquivo, sql_erro as erro, datahora_execucao as datahora");
|
||||||
sb.append(" from flyway_scripts");
|
sb.append(" from flyway_scripts");
|
||||||
sb.append(" where activo = 1 and nome not in (select replace(\"script\", '.sql', '') from \"schema_version_cst\")");
|
sb.append(" where activo = 1 and nome not in (select replace(\"script\", '.sql', '') from \"schema_version_cst\")");
|
||||||
sb.append(" order by nome_arquivo");
|
sb.append(" order by nome_arquivo");
|
||||||
} else {
|
} else {
|
||||||
sb.append(" select nome as nome_arquivo, sql as sql_arquivo, sql_erro as erro, datahora_execucao as datahora");
|
sb.append(" select nome as nome_arquivo, sql as sql_arquivo, sql_erro as erro, datahora_execucao as datahora");
|
||||||
sb.append(" from flyway_scripts");
|
sb.append(" from flyway_scripts");
|
||||||
sb.append(" where activo = 1");
|
sb.append(" where activo = 1");
|
||||||
sb.append(" order by nome_arquivo");
|
sb.append(" order by nome_arquivo");
|
||||||
}
|
}
|
||||||
|
|
||||||
Connection c = dataSource.getConnection();
|
Connection c = dataSource.getConnection();
|
||||||
Statement s = c.createStatement();
|
Statement s = c.createStatement();
|
||||||
|
|
||||||
ResultSet rs = s.executeQuery(sb.toString());
|
ResultSet rs = s.executeQuery(sb.toString());
|
||||||
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
if (!isRegistroValido(rs.getString("nome_arquivo"), rs.getString("sql_arquivo"))) {
|
if (!isRegistroValido(rs.getString("nome_arquivo"), rs.getString("sql_arquivo"))) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
Timestamp timestamp = rs.getTimestamp("datahora");
|
Timestamp timestamp = rs.getTimestamp("datahora");
|
||||||
|
|
||||||
FlyWayCustomDetail detail = new FlyWayCustomDetail();
|
FlyWayCustomDetail detail = new FlyWayCustomDetail();
|
||||||
detail.setVersion(rs.getString("nome_arquivo"));
|
detail.setVersion(rs.getString("nome_arquivo"));
|
||||||
detail.setSql(rs.getString("sql_arquivo"));
|
detail.setSql(rs.getString("sql_arquivo"));
|
||||||
detail.setErrorCode(rs.getString("erro"));
|
detail.setErrorCode(rs.getString("erro"));
|
||||||
|
|
||||||
if (timestamp != null) {
|
if (timestamp != null) {
|
||||||
detail.setDatetimeExecute(new java.util.Date(timestamp.getTime()));
|
detail.setDatetimeExecute(new java.util.Date(timestamp.getTime()));
|
||||||
}
|
}
|
||||||
|
|
||||||
scripts.put(rs.getString("nome_arquivo"), detail);
|
scripts.put(rs.getString("nome_arquivo"), detail);
|
||||||
}
|
}
|
||||||
|
|
||||||
rs.close();
|
rs.close();
|
||||||
s.close();
|
s.close();
|
||||||
c.close();
|
c.close();
|
||||||
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (scripts.isEmpty()) {
|
if (scripts.isEmpty()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean retorno = false;
|
boolean retorno = false;
|
||||||
|
|
||||||
Writer writer = null;
|
Writer writer = null;
|
||||||
|
|
||||||
for (Map.Entry<String, FlyWayCustomDetail> entry : scripts.entrySet()) {
|
for (Map.Entry<String, FlyWayCustomDetail> entry : scripts.entrySet()) {
|
||||||
if (entry.getValue().getDatetimeExecute() != null) {
|
if (entry.getValue().getDatetimeExecute() != null) {
|
||||||
Calendar atual = Calendar.getInstance();
|
Calendar atual = Calendar.getInstance();
|
||||||
|
|
||||||
if (atual.getTime().before(entry.getValue().getDatetimeExecute())) {
|
if (atual.getTime().before(entry.getValue().getDatetimeExecute())) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
File scriptFile = new File(data, entry.getKey() + ".sql");
|
File scriptFile = new File(data, entry.getKey() + ".sql");
|
||||||
|
|
||||||
if (scriptFile.exists()) {
|
if (scriptFile.exists()) {
|
||||||
scriptFile.delete();
|
scriptFile.delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!scriptFile.exists()) {
|
if (!scriptFile.exists()) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
if (entry.getValue().getErrorCode() != null) {
|
if (entry.getValue().getErrorCode() != null) {
|
||||||
sb.append("declare").append("\n");
|
sb.append("declare").append("\n");
|
||||||
sb.append(" object_exists exception;").append("\n").append("\n");
|
sb.append(" object_exists exception;").append("\n").append("\n");
|
||||||
sb.append(" pragma exception_init (object_exists , ").append(entry.getValue().getErrorCode()).append(");").append("\n");
|
sb.append(" pragma exception_init (object_exists , ").append(entry.getValue().getErrorCode()).append(");").append("\n");
|
||||||
sb.append("begin").append("\n");
|
sb.append("begin").append("\n");
|
||||||
sb.append(" execute immediate '").append(entry.getValue().getSql()).append("';").append("\n");
|
sb.append(" execute immediate '").append(entry.getValue().getSql()).append("';").append("\n");
|
||||||
sb.append(" exception when object_exists then null;").append("\n");
|
sb.append(" exception when object_exists then null;").append("\n");
|
||||||
sb.append("end;");
|
sb.append("end;");
|
||||||
} else {
|
} else {
|
||||||
sb.append(entry.getValue().getSql());
|
sb.append(entry.getValue().getSql());
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(scriptFile), "utf-8"));
|
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(scriptFile), "utf-8"));
|
||||||
writer.write(sb.toString());
|
writer.write(sb.toString());
|
||||||
|
|
||||||
log.info("[Flyway] Script gerado: " + scriptFile.toString());
|
log.info("[Flyway] Script gerado: " + scriptFile.toString());
|
||||||
|
|
||||||
retorno = true;
|
retorno = true;
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
} finally {
|
} finally {
|
||||||
try {writer.close();} catch (Exception ex) {log.info(ex.getMessage(), ex);}
|
try {writer.close();} catch (Exception ex) {log.info(ex.getMessage(), ex);}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return retorno;
|
return retorno;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isRegistroValido(String versao, String sql) {
|
private boolean isRegistroValido(String versao, String sql) {
|
||||||
boolean valido = true;
|
boolean valido = true;
|
||||||
|
|
||||||
String v[] = versao.split("_");
|
String v[] = versao.split("_");
|
||||||
|
|
||||||
if (!versao.startsWith("V")) {
|
if (!versao.startsWith("V")) {
|
||||||
valido = false;
|
valido = false;
|
||||||
} else {
|
} else {
|
||||||
if (!(v.length == 4)) {
|
if (!(v.length == 4)) {
|
||||||
valido = false;
|
valido = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!valido) {
|
if (!valido) {
|
||||||
Connection c = null;
|
Connection c = null;
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
c = dataSource.getConnection();
|
c = dataSource.getConnection();
|
||||||
ps = c.prepareStatement("update flyway_scripts set activo = 0 where nome like ?");
|
ps = c.prepareStatement("update flyway_scripts set activo = 0 where nome like ?");
|
||||||
ps.setString(1, versao);
|
ps.setString(1, versao);
|
||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
|
|
||||||
} catch (SQLException e1) {
|
} catch (SQLException e1) {
|
||||||
} finally {
|
} finally {
|
||||||
if (ps != null) {
|
if (ps != null) {
|
||||||
try { ps.close(); } catch (SQLException e1) {}
|
try { ps.close(); } catch (SQLException e1) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c != null) {
|
if (c != null) {
|
||||||
try { c.close(); } catch (SQLException e1) {}
|
try { c.close(); } catch (SQLException e1) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return valido;
|
return valido;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean existeSchema(Connection c) {
|
private boolean existeSchema(Connection c) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("SELECT count(table_name) as existe FROM user_tables WHERE table_name = 'schema_version_cst'");
|
sb.append("SELECT count(table_name) as existe FROM user_tables WHERE table_name = 'schema_version_cst'");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Statement s = c.createStatement();
|
Statement s = c.createStatement();
|
||||||
ResultSet rs = s.executeQuery(sb.toString());
|
ResultSet rs = s.executeQuery(sb.toString());
|
||||||
|
|
||||||
int existe = 0;
|
int existe = 0;
|
||||||
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
existe = rs.getInt("existe");
|
existe = rs.getInt("existe");
|
||||||
}
|
}
|
||||||
|
|
||||||
rs.close();
|
rs.close();
|
||||||
s.close();
|
s.close();
|
||||||
c.close();
|
c.close();
|
||||||
|
|
||||||
if (existe > 0) {
|
if (existe > 0) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,85 +1,85 @@
|
||||||
package com.rjconsultores.ventaboletos;
|
package com.rjconsultores.ventaboletos;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
|
|
||||||
import org.flywaydb.core.api.MigrationInfo;
|
import org.flywaydb.core.api.MigrationInfo;
|
||||||
import org.flywaydb.core.api.callback.FlywayCallback;
|
import org.flywaydb.core.api.callback.FlywayCallback;
|
||||||
|
|
||||||
/* https://flywaydb.org/documentation/callbacks.html */
|
/* https://flywaydb.org/documentation/callbacks.html */
|
||||||
public class FlyWayCallback implements FlywayCallback {
|
public class FlyWayCallback implements FlywayCallback {
|
||||||
private String script = null;
|
private String script = null;
|
||||||
|
|
||||||
public String getScriptError() {
|
public String getScriptError() {
|
||||||
return script;
|
return script;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void beforeClean(Connection connection) {
|
public void beforeClean(Connection connection) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterClean(Connection connection) {
|
public void afterClean(Connection connection) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void beforeMigrate(Connection connection) {
|
public void beforeMigrate(Connection connection) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterMigrate(Connection connection) {
|
public void afterMigrate(Connection connection) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void beforeEachMigrate(Connection connection, MigrationInfo info) {
|
public void beforeEachMigrate(Connection connection, MigrationInfo info) {
|
||||||
this.script = info.getScript();
|
this.script = info.getScript();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterEachMigrate(Connection connection, MigrationInfo info) {
|
public void afterEachMigrate(Connection connection, MigrationInfo info) {
|
||||||
this.script = null;
|
this.script = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void beforeValidate(Connection connection) {
|
public void beforeValidate(Connection connection) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterValidate(Connection connection) {
|
public void afterValidate(Connection connection) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void beforeBaseline(Connection connection) {
|
public void beforeBaseline(Connection connection) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterBaseline(Connection connection) {
|
public void afterBaseline(Connection connection) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void beforeRepair(Connection connection) {
|
public void beforeRepair(Connection connection) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterRepair(Connection connection) {
|
public void afterRepair(Connection connection) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void beforeInfo(Connection connection) {
|
public void beforeInfo(Connection connection) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterInfo(Connection connection) {
|
public void afterInfo(Connection connection) {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,38 +1,38 @@
|
||||||
package com.rjconsultores.ventaboletos.servlet;
|
package com.rjconsultores.ventaboletos.servlet;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.http.HttpServlet;
|
import javax.servlet.http.HttpServlet;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import com.rjconsultores.ventaboletos.FlyWay;
|
import com.rjconsultores.ventaboletos.FlyWay;
|
||||||
|
|
||||||
public class ReloadFlyway extends HttpServlet {
|
public class ReloadFlyway extends HttpServlet {
|
||||||
private static final long serialVersionUID = -2076848353707789950L;
|
private static final long serialVersionUID = -2076848353707789950L;
|
||||||
|
|
||||||
private PrintWriter out = null;
|
private PrintWriter out = null;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
out = resp.getWriter();
|
out = resp.getWriter();
|
||||||
out.println("<h2>Reload Flyway</h2>");
|
out.println("<h2>Reload Flyway</h2>");
|
||||||
|
|
||||||
boolean retorno = FlyWay.getInstance().start(true);
|
boolean retorno = FlyWay.getInstance().start(true);
|
||||||
|
|
||||||
if (retorno) {
|
if (retorno) {
|
||||||
out.print("Processo realizado com sucesso.");
|
out.print("Processo realizado com sucesso.");
|
||||||
} else {
|
} else {
|
||||||
out.print("Erro ao executar o flyway. Verifique o log do servidor.");
|
out.print("Erro ao executar o flyway. Verifique o log do servidor.");
|
||||||
}
|
}
|
||||||
|
|
||||||
out.close();
|
out.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
doGet(req, resp);
|
doGet(req, resp);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,60 +1,60 @@
|
||||||
package com.rjconsultores.ventaboletos.vo;
|
package com.rjconsultores.ventaboletos.vo;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
public class FlyWayCustomDetail {
|
public class FlyWayCustomDetail {
|
||||||
private String version;
|
private String version;
|
||||||
private String sql;
|
private String sql;
|
||||||
private String errorCode;
|
private String errorCode;
|
||||||
private Date datetimeExecute;
|
private Date datetimeExecute;
|
||||||
|
|
||||||
public FlyWayCustomDetail() {
|
public FlyWayCustomDetail() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
public FlyWayCustomDetail(String version, String sql, String errorCode, Date datetimeExecute) {
|
public FlyWayCustomDetail(String version, String sql, String errorCode, Date datetimeExecute) {
|
||||||
super();
|
super();
|
||||||
this.version = version;
|
this.version = version;
|
||||||
this.sql = sql;
|
this.sql = sql;
|
||||||
this.errorCode = errorCode;
|
this.errorCode = errorCode;
|
||||||
this.datetimeExecute = datetimeExecute;
|
this.datetimeExecute = datetimeExecute;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getVersion() {
|
public String getVersion() {
|
||||||
return version;
|
return version;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setVersion(String version) {
|
public void setVersion(String version) {
|
||||||
this.version = version;
|
this.version = version;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSql() {
|
public String getSql() {
|
||||||
return sql;
|
return sql;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSql(String sql) {
|
public void setSql(String sql) {
|
||||||
this.sql = sql;
|
this.sql = sql;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getErrorCode() {
|
public String getErrorCode() {
|
||||||
return errorCode;
|
return errorCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setErrorCode(String errorCode) {
|
public void setErrorCode(String errorCode) {
|
||||||
this.errorCode = errorCode;
|
this.errorCode = errorCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Date getDatetimeExecute() {
|
public Date getDatetimeExecute() {
|
||||||
return datetimeExecute;
|
return datetimeExecute;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDatetimeExecute(Date datetimeExecute) {
|
public void setDatetimeExecute(Date datetimeExecute) {
|
||||||
this.datetimeExecute = datetimeExecute;
|
this.datetimeExecute = datetimeExecute;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "FlyWayCustomDetail [version=" + version + ", sql=" + sql + ", errorCode=" + errorCode
|
return "FlyWayCustomDetail [version=" + version + ", sql=" + sql + ", errorCode=" + errorCode
|
||||||
+ ", datetimeExecute=" + datetimeExecute + "]";
|
+ ", datetimeExecute=" + datetimeExecute + "]";
|
||||||
}
|
}
|
||||||
}
|
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue