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.Writer;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
@ -19,6 +20,7 @@ import javax.sql.DataSource;
|
|||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.flywaydb.core.Flyway;
|
||||
import org.flywaydb.core.api.callback.FlywayCallback;
|
||||
|
||||
import com.rjconsultores.ventaboletos.vo.FlyWayCustomDetail;
|
||||
|
||||
|
@ -27,6 +29,9 @@ public class 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 String url = null;
|
||||
|
@ -99,13 +104,10 @@ public class FlyWay {
|
|||
}
|
||||
|
||||
public boolean startCustom() {
|
||||
final File jbossData = new File(System.getProperty("jboss.server.data.dir"));
|
||||
final String table = "schema_version_cst";
|
||||
|
||||
String location = "db.performance";
|
||||
|
||||
if (jbossData != null) {
|
||||
File data = new File(jbossData, "flyway");
|
||||
if (JBOSS_DATA != null) {
|
||||
File data = new File(JBOSS_DATA, "flyway");
|
||||
|
||||
if (!data.exists()) {
|
||||
data.mkdirs();
|
||||
|
@ -118,7 +120,7 @@ public class FlyWay {
|
|||
location = "filesystem:" + data.getPath();
|
||||
}
|
||||
|
||||
return start(location, table);
|
||||
return start(location, TABELA_FLYWAY);
|
||||
}
|
||||
|
||||
public boolean start(final String customLocation, final String customTable) {
|
||||
|
@ -131,9 +133,6 @@ public class FlyWay {
|
|||
flyway.setTable(customTable);
|
||||
}
|
||||
|
||||
// FlyWayCallback fc = new FlyWayCallback();
|
||||
// flyway.setCallbacks(fc);
|
||||
|
||||
if (customLocation != null) {
|
||||
flyway.setLocations(customLocation);
|
||||
} else if (this.location != null && !this.location.equals("db.migration")) {
|
||||
|
@ -151,7 +150,7 @@ public class FlyWay {
|
|||
execute(flyway);
|
||||
|
||||
}catch(Throwable t){
|
||||
log.error("Erro ao executar o Flyway Custom",t);
|
||||
log.error("Erro ao executar o Flyway",t);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
@ -167,17 +166,56 @@ public class FlyWay {
|
|||
flyway.setOutOfOrder(true);
|
||||
flyway.setBaselineOnMigrate(true);
|
||||
|
||||
flyway.migrate();
|
||||
if (flyway.getTable().equals(TABELA_FLYWAY)) {
|
||||
FlyWayCallback errorCallback = new FlyWayCallback();
|
||||
|
||||
FlywayCallback[] callbacks = new FlywayCallback[]{ errorCallback };
|
||||
flyway.setCallbacks(callbacks);
|
||||
|
||||
try {
|
||||
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) {
|
||||
Map<String, FlyWayCustomDetail> scripts = new HashMap<String, FlyWayCustomDetail>(0);
|
||||
|
||||
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();
|
||||
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()) {
|
||||
Timestamp timestamp = rs.getTimestamp("datahora");
|
||||
|
@ -220,6 +258,10 @@ public class FlyWay {
|
|||
|
||||
File scriptFile = new File(data, entry.getKey() + ".sql");
|
||||
|
||||
if (scriptFile.exists()) {
|
||||
scriptFile.delete();
|
||||
}
|
||||
|
||||
if (!scriptFile.exists()) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
|
|
|
@ -7,7 +7,12 @@ import org.flywaydb.core.api.callback.FlywayCallback;
|
|||
|
||||
/* https://flywaydb.org/documentation/callbacks.html */
|
||||
public class FlyWayCallback implements FlywayCallback {
|
||||
|
||||
private String script = null;
|
||||
|
||||
public String getScriptError() {
|
||||
return script;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeClean(Connection connection) {
|
||||
|
||||
|
@ -30,12 +35,12 @@ public class FlyWayCallback implements FlywayCallback {
|
|||
|
||||
@Override
|
||||
public void beforeEachMigrate(Connection connection, MigrationInfo info) {
|
||||
|
||||
this.script = info.getScript();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterEachMigrate(Connection connection, MigrationInfo info) {
|
||||
|
||||
this.script = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -77,5 +82,4 @@ public class FlyWayCallback implements FlywayCallback {
|
|||
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