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-87c2c4800839master
parent
eae319cdc9
commit
4e1f1f9684
|
@ -10,6 +10,8 @@ import java.sql.Connection;
|
||||||
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.util.Calendar;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ -18,6 +20,8 @@ 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 com.rjconsultores.ventaboletos.vo.FlyWayCustomDetail;
|
||||||
|
|
||||||
public class FlyWay {
|
public class FlyWay {
|
||||||
private static Logger log = Logger.getLogger(FlyWay.class);
|
private static Logger log = Logger.getLogger(FlyWay.class);
|
||||||
|
|
||||||
|
@ -96,7 +100,7 @@ public class FlyWay {
|
||||||
|
|
||||||
public boolean startCustom() {
|
public boolean startCustom() {
|
||||||
final File jbossData = new File(System.getProperty("jboss.server.data.dir"));
|
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";
|
String location = "db.performance";
|
||||||
|
|
||||||
|
@ -107,7 +111,9 @@ public class FlyWay {
|
||||||
data.mkdirs();
|
data.mkdirs();
|
||||||
}
|
}
|
||||||
|
|
||||||
validarScripts(data);
|
if (!validarScripts(data)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
location = "filesystem:" + data.getPath();
|
location = "filesystem:" + data.getPath();
|
||||||
}
|
}
|
||||||
|
@ -116,7 +122,7 @@ public class FlyWay {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean start(final String customLocation, final String customTable) {
|
public boolean start(final String customLocation, final String customTable) {
|
||||||
log.info("Executando Flyway Custom...");
|
log.info("Executando Flyway...");
|
||||||
|
|
||||||
try{
|
try{
|
||||||
final Flyway flyway = new Flyway();
|
final Flyway flyway = new Flyway();
|
||||||
|
@ -138,6 +144,8 @@ public class FlyWay {
|
||||||
flyway.setDataSource(dataSource);
|
flyway.setDataSource(dataSource);
|
||||||
} else {
|
} else {
|
||||||
flyway.setDataSource(url, user, password);
|
flyway.setDataSource(url, user, password);
|
||||||
|
|
||||||
|
dataSource = flyway.getDataSource();
|
||||||
}
|
}
|
||||||
|
|
||||||
execute(flyway);
|
execute(flyway);
|
||||||
|
@ -148,7 +156,7 @@ public class FlyWay {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
log.info("Flyway Custom executado.");
|
log.info("Flyway executado.");
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -162,17 +170,28 @@ public class FlyWay {
|
||||||
flyway.migrate();
|
flyway.migrate();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void validarScripts(File data) {
|
private boolean validarScripts(File data) {
|
||||||
Map<String, String> scripts = new HashMap<String, String>(0);
|
Map<String, FlyWayCustomDetail> scripts = new HashMap<String, FlyWayCustomDetail>(0);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
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 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()) {
|
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();
|
rs.close();
|
||||||
|
@ -182,20 +201,54 @@ public class FlyWay {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (scripts.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean retorno = false;
|
||||||
|
|
||||||
Writer writer = null;
|
Writer writer = null;
|
||||||
|
|
||||||
for (Map.Entry<String, String> entry : scripts.entrySet()) {
|
for (Map.Entry<String, FlyWayCustomDetail> 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");
|
File scriptFile = new File(data, entry.getKey() + ".sql");
|
||||||
|
|
||||||
if (!scriptFile.exists()) {
|
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 {
|
try {
|
||||||
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(scriptFile), "utf-8"));
|
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) {
|
} 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 + "]";
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
Loading…
Reference in New Issue