diff --git a/src/com/rjconsultores/ventaboletos/FlyWay.java b/src/com/rjconsultores/ventaboletos/FlyWay.java index 14ad8e25d..e698da719 100644 --- a/src/com/rjconsultores/ventaboletos/FlyWay.java +++ b/src/com/rjconsultores/ventaboletos/FlyWay.java @@ -1,21 +1,34 @@ package com.rjconsultores.ventaboletos; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.util.HashMap; +import java.util.Map; + +import javax.sql.DataSource; import org.apache.log4j.Logger; import org.flywaydb.core.Flyway; -import javax.sql.DataSource; - public class FlyWay { private static Logger log = Logger.getLogger(FlyWay.class); private static final FlyWay INSTANCE = new FlyWay(); private DataSource dataSource = null; + + private String url = null; + private String user = null; + private String password = null; + private String location = "db.migration"; public static FlyWay getInstance() { @@ -32,6 +45,12 @@ public class FlyWay { fixFiles(dataSource); } + public void defineProperties(final String url, final String user, final String password) { + this.url = url; + this.user = user; + this.password = password; + } + private void fixFiles(final DataSource ds) { try { Connection c = dataSource.getConnection(); @@ -72,55 +91,68 @@ public class FlyWay { } public boolean start() { - log.info("Executando Flyway..."); + return start(null, null); + } + + public boolean startCustom() { + final File jbossData = new File(System.getProperty("jboss.server.data.dir")); + final String table = "schema_version_perf"; + + String location = "db.performance"; + + if (jbossData != null) { + File data = new File(jbossData, "flyway"); + + if (!data.exists()) { + data.mkdirs(); + } + + validarScripts(data); + + location = "filesystem:" + data.getPath(); + } + + return start(location, table); + } + + public boolean start(final String customLocation, final String customTable) { + log.info("Executando Flyway Custom..."); try{ final Flyway flyway = new Flyway(); - - if (this.location != null && !this.location.equals("db.migration")) { + + if (customTable != null) { + 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")) { flyway.setLocations(this.location); } - flyway.setDataSource(dataSource); - - execute(flyway); - - }catch(Throwable t){ - log.error("Erro ao executar o flyway",t); - - return false; - } - - log.info("Flyway executado."); - - return true; - } - - public boolean start(String url, String user, String password) { - log.info("Executando Flyway..."); - - try{ - final Flyway flyway = new Flyway(); - - if (this.location != null && !this.location.equals("db.migration")) { - flyway.setLocations("classpath: " + this.location); + if (dataSource != null) { + flyway.setDataSource(dataSource); + } else { + flyway.setDataSource(url, user, password); } - flyway.setDataSource(url, user, password); - execute(flyway); }catch(Throwable t){ - log.error("Erro ao executar o flyway",t); + log.error("Erro ao executar o Flyway Custom",t); return false; } - log.info("Flyway executado."); + log.info("Flyway Custom executado."); return true; } - + public void execute(final Flyway flyway) { flyway.setValidateOnMigrate(false); flyway.setIgnoreFutureMigrations(true); @@ -129,4 +161,41 @@ public class FlyWay { flyway.migrate(); } + + private void 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"); + + while (rs.next()) { + scripts.put(rs.getString("nome_arquivo"), rs.getString("sql_arquivo")); + } + + rs.close(); + s.close(); + + } catch (SQLException e) { + log.error(e.getMessage(), e); + } + + Writer writer = null; + + for (Map.Entry entry : scripts.entrySet()) { + File scriptFile = new File(data, entry.getKey() + ".sql"); + + if (!scriptFile.exists()) { + try { + writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(scriptFile), "utf-8")); + writer.write(entry.getValue()); + } catch (IOException ex) { + } finally { + try {writer.close();} catch (Exception ex) {log.info(ex.getMessage(), ex);} + } + } + } + } } diff --git a/src/com/rjconsultores/ventaboletos/FlyWayCallback.java b/src/com/rjconsultores/ventaboletos/FlyWayCallback.java new file mode 100644 index 000000000..d5d4157c6 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/FlyWayCallback.java @@ -0,0 +1,81 @@ +package com.rjconsultores.ventaboletos; + +import java.sql.Connection; + +import org.flywaydb.core.api.MigrationInfo; +import org.flywaydb.core.api.callback.FlywayCallback; + +/* https://flywaydb.org/documentation/callbacks.html */ +public class FlyWayCallback implements FlywayCallback { + + @Override + public void beforeClean(Connection connection) { + + } + + @Override + public void afterClean(Connection connection) { + + } + + @Override + public void beforeMigrate(Connection connection) { + + } + + @Override + public void afterMigrate(Connection connection) { + + } + + @Override + public void beforeEachMigrate(Connection connection, MigrationInfo info) { + + } + + @Override + public void afterEachMigrate(Connection connection, MigrationInfo info) { + + } + + @Override + public void beforeValidate(Connection connection) { + + } + + @Override + public void afterValidate(Connection connection) { + + } + + @Override + public void beforeBaseline(Connection connection) { + + } + + @Override + public void afterBaseline(Connection connection) { + + } + + @Override + public void beforeRepair(Connection connection) { + + } + + @Override + public void afterRepair(Connection connection) { + + } + + @Override + public void beforeInfo(Connection connection) { + + } + + @Override + public void afterInfo(Connection connection) { + + } + +} diff --git a/src/db/migration/V20181025_1840__mantis12464.sql b/src/db/migration/V20181025_1840__mantis12464.sql new file mode 100644 index 000000000..811cc10a9 --- /dev/null +++ b/src/db/migration/V20181025_1840__mantis12464.sql @@ -0,0 +1,15 @@ +declare + object_exists exception; + pragma exception_init (object_exists , -00955); +begin + execute immediate + 'CREATE TABLE FLYWAY_SCRIPTS ( + NOME VARCHAR2(100) NOT NULL, + SQL CLOB NOT NULL, + OBSERVACAO VARCHAR2(500), + FECMODIF DATE, + PRIMARY KEY (NOME) + )'; + + exception when object_exists then null; +end;