From 4e1f1f9684b8139fbe20a37e8c7fa5099a0a186e Mon Sep 17 00:00:00 2001 From: alberto Date: Tue, 27 Nov 2018 20:07:47 +0000 Subject: [PATCH] Nazar - Melhorias bug#12464 dev:trevezani qua: git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/BD/FlyWay/trunk@87457 d1611594-4594-4d17-8e1d-87c2c4800839 --- .../rjconsultores/ventaboletos/FlyWay.java | 73 ++++++++++++++++--- .../ventaboletos/vo/FlyWayCustomDetail.java | 60 +++++++++++++++ .../migration/V20181127_1520__mantis12464.sql | 7 ++ 3 files changed, 130 insertions(+), 10 deletions(-) create mode 100644 src/com/rjconsultores/ventaboletos/vo/FlyWayCustomDetail.java create mode 100644 src/db/migration/V20181127_1520__mantis12464.sql diff --git a/src/com/rjconsultores/ventaboletos/FlyWay.java b/src/com/rjconsultores/ventaboletos/FlyWay.java index e698da719..8b18d35b9 100644 --- a/src/com/rjconsultores/ventaboletos/FlyWay.java +++ b/src/com/rjconsultores/ventaboletos/FlyWay.java @@ -10,6 +10,8 @@ import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.sql.Timestamp; +import java.util.Calendar; import java.util.HashMap; import java.util.Map; @@ -18,6 +20,8 @@ import javax.sql.DataSource; import org.apache.log4j.Logger; import org.flywaydb.core.Flyway; +import com.rjconsultores.ventaboletos.vo.FlyWayCustomDetail; + public class FlyWay { private static Logger log = Logger.getLogger(FlyWay.class); @@ -96,7 +100,7 @@ public class FlyWay { public boolean startCustom() { final File jbossData = new File(System.getProperty("jboss.server.data.dir")); - final String table = "schema_version_perf"; + final String table = "schema_version_cst"; String location = "db.performance"; @@ -107,7 +111,9 @@ public class FlyWay { data.mkdirs(); } - validarScripts(data); + if (!validarScripts(data)) { + return true; + } location = "filesystem:" + data.getPath(); } @@ -116,7 +122,7 @@ public class FlyWay { } public boolean start(final String customLocation, final String customTable) { - log.info("Executando Flyway Custom..."); + log.info("Executando Flyway..."); try{ final Flyway flyway = new Flyway(); @@ -138,6 +144,8 @@ public class FlyWay { flyway.setDataSource(dataSource); } else { flyway.setDataSource(url, user, password); + + dataSource = flyway.getDataSource(); } execute(flyway); @@ -148,7 +156,7 @@ public class FlyWay { return false; } - log.info("Flyway Custom executado."); + log.info("Flyway executado."); return true; } @@ -162,17 +170,28 @@ public class FlyWay { flyway.migrate(); } - private void validarScripts(File data) { - Map scripts = new HashMap(0); + private boolean validarScripts(File data) { + Map scripts = new HashMap(0); try { Connection c = dataSource.getConnection(); Statement s = c.createStatement(); - ResultSet rs = s.executeQuery("select nome as nome_arquivo, sql as sql_arquivo from flyway_scripts"); + 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"); while (rs.next()) { - scripts.put(rs.getString("nome_arquivo"), rs.getString("sql_arquivo")); + Timestamp timestamp = rs.getTimestamp("datahora"); + + FlyWayCustomDetail detail = new FlyWayCustomDetail(); + detail.setVersion(rs.getString("nome_arquivo")); + detail.setSql(rs.getString("sql_arquivo")); + detail.setErrorCode(rs.getString("erro")); + + if (timestamp != null) { + detail.setDatetimeExecute(new java.util.Date(timestamp.getTime())); + } + + scripts.put(rs.getString("nome_arquivo"), detail); } rs.close(); @@ -182,20 +201,54 @@ public class FlyWay { log.error(e.getMessage(), e); } + if (scripts.isEmpty()) { + return false; + } + + boolean retorno = false; + Writer writer = null; - for (Map.Entry entry : scripts.entrySet()) { + for (Map.Entry entry : scripts.entrySet()) { + if (entry.getValue().getDatetimeExecute() != null) { + Calendar atual = Calendar.getInstance(); + + if (atual.getTime().before(entry.getValue().getDatetimeExecute())) { + continue; + } + } + File scriptFile = new File(data, entry.getKey() + ".sql"); if (!scriptFile.exists()) { + StringBuilder sb = new StringBuilder(); + + if (entry.getValue().getErrorCode() != null) { + sb.append("declare").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("begin").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("end;"); + } else { + sb.append(entry.getValue().getSql()); + } + try { writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(scriptFile), "utf-8")); - writer.write(entry.getValue()); + writer.write(sb.toString()); + + log.info("[Flyway] Script gerado: " + scriptFile.toString()); + + retorno = true; } catch (IOException ex) { } finally { try {writer.close();} catch (Exception ex) {log.info(ex.getMessage(), ex);} } } } + + return retorno; } } diff --git a/src/com/rjconsultores/ventaboletos/vo/FlyWayCustomDetail.java b/src/com/rjconsultores/ventaboletos/vo/FlyWayCustomDetail.java new file mode 100644 index 000000000..751c39db1 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/vo/FlyWayCustomDetail.java @@ -0,0 +1,60 @@ +package com.rjconsultores.ventaboletos.vo; + +import java.util.Date; + +public class FlyWayCustomDetail { + private String version; + private String sql; + private String errorCode; + private Date datetimeExecute; + + public FlyWayCustomDetail() { + super(); + } + + public FlyWayCustomDetail(String version, String sql, String errorCode, Date datetimeExecute) { + super(); + this.version = version; + this.sql = sql; + this.errorCode = errorCode; + this.datetimeExecute = datetimeExecute; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getSql() { + return sql; + } + + public void setSql(String sql) { + this.sql = sql; + } + + public String getErrorCode() { + return errorCode; + } + + public void setErrorCode(String errorCode) { + this.errorCode = errorCode; + } + + public Date getDatetimeExecute() { + return datetimeExecute; + } + + public void setDatetimeExecute(Date datetimeExecute) { + this.datetimeExecute = datetimeExecute; + } + + @Override + public String toString() { + return "FlyWayCustomDetail [version=" + version + ", sql=" + sql + ", errorCode=" + errorCode + + ", datetimeExecute=" + datetimeExecute + "]"; + } +} diff --git a/src/db/migration/V20181127_1520__mantis12464.sql b/src/db/migration/V20181127_1520__mantis12464.sql new file mode 100644 index 000000000..0f9c22c76 --- /dev/null +++ b/src/db/migration/V20181127_1520__mantis12464.sql @@ -0,0 +1,7 @@ +declare + column_exists exception; + pragma exception_init (column_exists , -01430); +begin + execute immediate 'ALTER TABLE FLYWAY_SCRIPTS ADD (SQL_ERRO VARCHAR2(10), DATAHORA_EXECUCAO DATE)'; + exception when column_exists then null; +end; \ No newline at end of file