From 7ee8bf797ecece7e505dd73b53ff10a84531f394 Mon Sep 17 00:00:00 2001 From: alberto Date: Wed, 28 Nov 2018 20:31:20 +0000 Subject: [PATCH] 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-87c2c4800839 --- .../rjconsultores/ventaboletos/FlyWay.java | 66 +++++++++++++++---- .../ventaboletos/FlyWayCallback.java | 12 ++-- .../migration/V20181128_1140__mantis12465.sql | 7 ++ 3 files changed, 69 insertions(+), 16 deletions(-) create mode 100644 src/db/migration/V20181128_1140__mantis12465.sql diff --git a/src/com/rjconsultores/ventaboletos/FlyWay.java b/src/com/rjconsultores/ventaboletos/FlyWay.java index 8b18d35b9..d1b577c80 100644 --- a/src/com/rjconsultores/ventaboletos/FlyWay.java +++ b/src/com/rjconsultores/ventaboletos/FlyWay.java @@ -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 scripts = new HashMap(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(); diff --git a/src/com/rjconsultores/ventaboletos/FlyWayCallback.java b/src/com/rjconsultores/ventaboletos/FlyWayCallback.java index d5d4157c6..65a7e4ed9 100644 --- a/src/com/rjconsultores/ventaboletos/FlyWayCallback.java +++ b/src/com/rjconsultores/ventaboletos/FlyWayCallback.java @@ -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) { } - } diff --git a/src/db/migration/V20181128_1140__mantis12465.sql b/src/db/migration/V20181128_1140__mantis12465.sql new file mode 100644 index 000000000..136a73c10 --- /dev/null +++ b/src/db/migration/V20181128_1140__mantis12465.sql @@ -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; \ No newline at end of file