diff --git a/src/com/rjconsultores/ventaboletos/FlyWay.java b/src/com/rjconsultores/ventaboletos/FlyWay.java index 480ab672f..be87c008d 100644 --- a/src/com/rjconsultores/ventaboletos/FlyWay.java +++ b/src/com/rjconsultores/ventaboletos/FlyWay.java @@ -1,7 +1,13 @@ package com.rjconsultores.ventaboletos; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + import org.apache.log4j.Logger; import org.flywaydb.core.Flyway; + import javax.sql.DataSource; public class FlyWay { @@ -17,8 +23,48 @@ public class FlyWay { public void defineDataSource(final DataSource dataSource) { this.dataSource = dataSource; + + fixFiles(dataSource); } + private void fixFiles(final DataSource ds) { + try { + Connection c = dataSource.getConnection(); + Statement s = c.createStatement(); + + fixVersion(s, "20161910_0934", "20161019_0934", "V20161019_0934__mantis7907.sql"); + fixVersion(s, "20162410.1119", "20161024.1119", "V20161024.1119__mantis7904.sql"); + + s.close(); + + } catch (SQLException e) { + log.error(e.getMessage(), e); + } + } + + private void fixVersion(final Statement s, final String oldVersion, final String newVersion, final String newScript) throws SQLException { + ResultSet rs = s.executeQuery("select count(*) as total from \"schema_version\" where \"version\" like '" + oldVersion + "'"); + + int total = 0; + + while (rs.next()) { + total = rs.getInt("total"); + } + + rs.close(); + + if (total > 0) { + log.info("Fix version " + oldVersion + " to new version " + newVersion + " and new script " + newScript); + + StringBuilder sql = new StringBuilder(""); + sql.append(" update \"schema_version\""); + sql.append(" set \"version\" = '" + newVersion + "', \"script\" = '" + newScript + "'"); + sql.append(" where \"version\" like '" + oldVersion + "'"); + + s.executeUpdate(sql.toString()); + } + } + public boolean start() { log.info("Executando Flyway..."); diff --git a/src/db/migration/V20161710_1833__mantis8112.sql b/src/db/migration/V20161017_1833__mantis8112.sql similarity index 100% rename from src/db/migration/V20161710_1833__mantis8112.sql rename to src/db/migration/V20161017_1833__mantis8112.sql diff --git a/src/db/migration/V20161910_0934__mantis7907.sql b/src/db/migration/V20161019_0934__mantis7907.sql similarity index 100% rename from src/db/migration/V20161910_0934__mantis7907.sql rename to src/db/migration/V20161019_0934__mantis7907.sql diff --git a/src/db/migration/V20161024_1430__mantis8120.sql b/src/db/migration/V20161024_1430__mantis8120.sql new file mode 100644 index 000000000..b95f00f8d --- /dev/null +++ b/src/db/migration/V20161024_1430__mantis8120.sql @@ -0,0 +1,19 @@ +alter table fiscal_ml2 add (indvendamanual number(1)); + +update fiscal_ml2 set indvendamanual = 0; + +update fiscal_ml2 set indvendamanual = 1 where fiscalml2_id in ( + select ml2.fiscalml2_id + from fiscal_ml2 ml2 + join boleto b on b.boleto_id = ml2.boleto_id + where b.tipoventa_id = 3); + +alter table fiscal_r4 add (indvendamanual number(1)); + +update fiscal_r4 set indvendamanual = 0; + +update fiscal_r4 set indvendamanual = 1 where fiscalr4_id in ( + select r4.fiscalr4_id + from fiscal_r4 r4 + join boleto b on b.boleto_id = r4.boleto_id + where b.tipoventa_id = 3); \ No newline at end of file