Nazar - Melhorias
bug#12464 dev:trevezani qua: git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/BD/FlyWay/trunk@87498 d1611594-4594-4d17-8e1d-87c2c4800839master
parent
7fcf43b352
commit
7ee8bf797e
|
@ -7,6 +7,7 @@ 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.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
|
@ -19,6 +20,7 @@ import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
import org.flywaydb.core.Flyway;
|
import org.flywaydb.core.Flyway;
|
||||||
|
import org.flywaydb.core.api.callback.FlywayCallback;
|
||||||
|
|
||||||
import com.rjconsultores.ventaboletos.vo.FlyWayCustomDetail;
|
import com.rjconsultores.ventaboletos.vo.FlyWayCustomDetail;
|
||||||
|
|
||||||
|
@ -27,6 +29,9 @@ public class FlyWay {
|
||||||
|
|
||||||
private static final FlyWay INSTANCE = new FlyWay();
|
private static final FlyWay INSTANCE = new FlyWay();
|
||||||
|
|
||||||
|
final private static String TABELA_FLYWAY = "schema_version_cst";
|
||||||
|
final File JBOSS_DATA = new File(System.getProperty("jboss.server.data.dir"));
|
||||||
|
|
||||||
private DataSource dataSource = null;
|
private DataSource dataSource = null;
|
||||||
|
|
||||||
private String url = null;
|
private String url = null;
|
||||||
|
@ -99,13 +104,10 @@ public class FlyWay {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean startCustom() {
|
public boolean startCustom() {
|
||||||
final File jbossData = new File(System.getProperty("jboss.server.data.dir"));
|
|
||||||
final String table = "schema_version_cst";
|
|
||||||
|
|
||||||
String location = "db.performance";
|
String location = "db.performance";
|
||||||
|
|
||||||
if (jbossData != null) {
|
if (JBOSS_DATA != null) {
|
||||||
File data = new File(jbossData, "flyway");
|
File data = new File(JBOSS_DATA, "flyway");
|
||||||
|
|
||||||
if (!data.exists()) {
|
if (!data.exists()) {
|
||||||
data.mkdirs();
|
data.mkdirs();
|
||||||
|
@ -118,7 +120,7 @@ public class FlyWay {
|
||||||
location = "filesystem:" + data.getPath();
|
location = "filesystem:" + data.getPath();
|
||||||
}
|
}
|
||||||
|
|
||||||
return start(location, table);
|
return start(location, TABELA_FLYWAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean start(final String customLocation, final String customTable) {
|
public boolean start(final String customLocation, final String customTable) {
|
||||||
|
@ -131,9 +133,6 @@ public class FlyWay {
|
||||||
flyway.setTable(customTable);
|
flyway.setTable(customTable);
|
||||||
}
|
}
|
||||||
|
|
||||||
// FlyWayCallback fc = new FlyWayCallback();
|
|
||||||
// flyway.setCallbacks(fc);
|
|
||||||
|
|
||||||
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")) {
|
||||||
|
@ -151,7 +150,7 @@ public class FlyWay {
|
||||||
execute(flyway);
|
execute(flyway);
|
||||||
|
|
||||||
}catch(Throwable t){
|
}catch(Throwable t){
|
||||||
log.error("Erro ao executar o Flyway Custom",t);
|
log.error("Erro ao executar o Flyway",t);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -167,17 +166,56 @@ public class FlyWay {
|
||||||
flyway.setOutOfOrder(true);
|
flyway.setOutOfOrder(true);
|
||||||
flyway.setBaselineOnMigrate(true);
|
flyway.setBaselineOnMigrate(true);
|
||||||
|
|
||||||
|
if (flyway.getTable().equals(TABELA_FLYWAY)) {
|
||||||
|
FlyWayCallback errorCallback = new FlyWayCallback();
|
||||||
|
|
||||||
|
FlywayCallback[] callbacks = new FlywayCallback[]{ errorCallback };
|
||||||
|
flyway.setCallbacks(callbacks);
|
||||||
|
|
||||||
|
try {
|
||||||
flyway.migrate();
|
flyway.migrate();
|
||||||
|
} catch (final Exception e) {
|
||||||
|
if (errorCallback.getScriptError() != null) {
|
||||||
|
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");
|
||||||
|
scriptFile = new File(scriptFile, errorCallback.getScriptError());
|
||||||
|
|
||||||
|
if (scriptFile.exists()) {
|
||||||
|
log.info("[Flyway] Excluindo fisicamente o script: " + errorCallback.getScriptError() + ". " + (scriptFile.delete() ? "OK" : "ERROR"));
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Connection c = flyway.getDataSource().getConnection();
|
||||||
|
PreparedStatement ps = c.prepareStatement("update flyway_scripts set activo = 0 where nome like ?");
|
||||||
|
ps.setString(1, errorCallback.getScriptError().replace(".sql", ""));
|
||||||
|
ps.executeUpdate();
|
||||||
|
|
||||||
|
} catch (SQLException e1) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
flyway.repair();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
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();
|
||||||
|
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(" where activo = 1 and nome not in (select replace(\"script\", '.sql', '') from \"schema_version_cst\")");
|
||||||
|
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("select nome as nome_arquivo, sql as sql_arquivo, sql_erro as erro, datahora_execucao as datahora from FLYWAY_SCRIPTS where nome not in (select replace(\"script\", '.sql', '') from \"schema_version_cst\") order by nome_arquivo");
|
ResultSet rs = s.executeQuery(sb.toString());
|
||||||
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
Timestamp timestamp = rs.getTimestamp("datahora");
|
Timestamp timestamp = rs.getTimestamp("datahora");
|
||||||
|
@ -220,6 +258,10 @@ public class FlyWay {
|
||||||
|
|
||||||
File scriptFile = new File(data, entry.getKey() + ".sql");
|
File scriptFile = new File(data, entry.getKey() + ".sql");
|
||||||
|
|
||||||
|
if (scriptFile.exists()) {
|
||||||
|
scriptFile.delete();
|
||||||
|
}
|
||||||
|
|
||||||
if (!scriptFile.exists()) {
|
if (!scriptFile.exists()) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,11 @@ 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;
|
||||||
|
|
||||||
|
public String getScriptError() {
|
||||||
|
return script;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void beforeClean(Connection connection) {
|
public void beforeClean(Connection connection) {
|
||||||
|
@ -30,12 +35,12 @@ public class FlyWayCallback implements FlywayCallback {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void beforeEachMigrate(Connection connection, MigrationInfo info) {
|
public void beforeEachMigrate(Connection connection, MigrationInfo info) {
|
||||||
|
this.script = info.getScript();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterEachMigrate(Connection connection, MigrationInfo info) {
|
public void afterEachMigrate(Connection connection, MigrationInfo info) {
|
||||||
|
this.script = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -77,5 +82,4 @@ public class FlyWayCallback implements FlywayCallback {
|
||||||
public void afterInfo(Connection connection) {
|
public void afterInfo(Connection connection) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
declare
|
||||||
|
column_exists exception;
|
||||||
|
pragma exception_init (column_exists , -01430);
|
||||||
|
begin
|
||||||
|
execute immediate 'ALTER TABLE FLYWAY_SCRIPTS ADD (ACTIVO NUMBER(1,0))';
|
||||||
|
exception when column_exists then null;
|
||||||
|
end;
|
Loading…
Reference in New Issue