diff --git a/src/com/rjconsultores/ventaboletos/FlyWay.java b/src/com/rjconsultores/ventaboletos/FlyWay.java index 8aacdc24c..aa0a744c0 100644 --- a/src/com/rjconsultores/ventaboletos/FlyWay.java +++ b/src/com/rjconsultores/ventaboletos/FlyWay.java @@ -221,10 +221,20 @@ public class FlyWay { try { StringBuilder sb = new StringBuilder(); - sb.append(" select nome as nome_arquivo, sql as sql_arquivo, sql_erro as erro, datahora_execucao as datahora"); - sb.append(" from flyway_scripts"); - sb.append(" where activo = 1 and nome not in (select replace(\"script\", '.sql', '') from \"schema_version_cst\")"); - sb.append(" order by nome_arquivo"); + + boolean existe = existeSchema(dataSource.getConnection()); + + if (existe) { + sb.append(" select nome as nome_arquivo, sql as sql_arquivo, sql_erro as erro, datahora_execucao as datahora"); + sb.append(" from flyway_scripts"); + sb.append(" where activo = 1 and nome not in (select replace(\"script\", '.sql', '') from \"schema_version_cst\")"); + sb.append(" order by nome_arquivo"); + } else { + sb.append(" select nome as nome_arquivo, sql as sql_arquivo, sql_erro as erro, datahora_execucao as datahora"); + sb.append(" from flyway_scripts"); + sb.append(" where activo = 1"); + sb.append(" order by nome_arquivo"); + } Connection c = dataSource.getConnection(); Statement s = c.createStatement(); @@ -307,4 +317,32 @@ public class FlyWay { return retorno; } + + private boolean existeSchema(Connection c) { + StringBuilder sb = new StringBuilder(); + sb.append("SELECT count(table_name) as existe FROM all_tables WHERE table_name = 'schema_version_cst'"); + + try { + Statement s = c.createStatement(); + ResultSet rs = s.executeQuery(sb.toString()); + + int existe = 0; + + while (rs.next()) { + existe = rs.getInt("existe"); + } + + rs.close(); + s.close(); + + if (existe > 0) { + return true; + } + + } catch (SQLException e) { + log.error(e.getMessage(), e); + } + + return false; + } }