Nazar - Melhorias
bug#12464 dev:trevezani qua: git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/BD/FlyWay/trunk@86511 d1611594-4594-4d17-8e1d-87c2c4800839master
parent
1138f0eb02
commit
9068c94fc0
|
@ -1,21 +1,34 @@
|
||||||
package com.rjconsultores.ventaboletos;
|
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.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.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
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 javax.sql.DataSource;
|
|
||||||
|
|
||||||
public class FlyWay {
|
public class FlyWay {
|
||||||
private static Logger log = Logger.getLogger(FlyWay.class);
|
private static Logger log = Logger.getLogger(FlyWay.class);
|
||||||
|
|
||||||
private static final FlyWay INSTANCE = new FlyWay();
|
private static final FlyWay INSTANCE = new FlyWay();
|
||||||
|
|
||||||
private DataSource dataSource = null;
|
private DataSource dataSource = null;
|
||||||
|
|
||||||
|
private String url = null;
|
||||||
|
private String user = null;
|
||||||
|
private String password = null;
|
||||||
|
|
||||||
private String location = "db.migration";
|
private String location = "db.migration";
|
||||||
|
|
||||||
public static FlyWay getInstance() {
|
public static FlyWay getInstance() {
|
||||||
|
@ -32,6 +45,12 @@ public class FlyWay {
|
||||||
fixFiles(dataSource);
|
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) {
|
private void fixFiles(final DataSource ds) {
|
||||||
try {
|
try {
|
||||||
Connection c = dataSource.getConnection();
|
Connection c = dataSource.getConnection();
|
||||||
|
@ -72,55 +91,68 @@ public class FlyWay {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean start() {
|
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{
|
try{
|
||||||
final Flyway flyway = new Flyway();
|
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.setLocations(this.location);
|
||||||
}
|
}
|
||||||
|
|
||||||
flyway.setDataSource(dataSource);
|
if (dataSource != null) {
|
||||||
|
flyway.setDataSource(dataSource);
|
||||||
execute(flyway);
|
} else {
|
||||||
|
flyway.setDataSource(url, user, password);
|
||||||
}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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
flyway.setDataSource(url, user, password);
|
|
||||||
|
|
||||||
execute(flyway);
|
execute(flyway);
|
||||||
|
|
||||||
}catch(Throwable t){
|
}catch(Throwable t){
|
||||||
log.error("Erro ao executar o flyway",t);
|
log.error("Erro ao executar o Flyway Custom",t);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
log.info("Flyway executado.");
|
log.info("Flyway Custom executado.");
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void execute(final Flyway flyway) {
|
public void execute(final Flyway flyway) {
|
||||||
flyway.setValidateOnMigrate(false);
|
flyway.setValidateOnMigrate(false);
|
||||||
flyway.setIgnoreFutureMigrations(true);
|
flyway.setIgnoreFutureMigrations(true);
|
||||||
|
@ -129,4 +161,41 @@ public class FlyWay {
|
||||||
|
|
||||||
flyway.migrate();
|
flyway.migrate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void validarScripts(File data) {
|
||||||
|
Map<String, String> scripts = new HashMap<String, String>(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<String, String> 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);}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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;
|
Loading…
Reference in New Issue