Skip to content

Commit

Permalink
Check for table existence instead of IF EXISTS which is not supported…
Browse files Browse the repository at this point in the history
… by all databases
  • Loading branch information
ccavanaugh committed Jul 19, 2020
1 parent 7774249 commit d737d73
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions jgnash-core/src/main/java/jgnash/engine/jpa/SqlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,21 @@ static void dropColumn(final String fileName, final char[] password, final Strin
final String url = properties.getProperty(JpaConfiguration.JAVAX_PERSISTENCE_JDBC_URL);

try (final Connection connection = DriverManager.getConnection(url)) {
for (final String column : columns) {
try (final Statement statement = connection.createStatement()) {
statement.execute("ALTER TABLE IF EXISTS " + table + " DROP COLUMN IF EXISTS " + column);

final DatabaseMetaData metaData = connection.getMetaData();

final ResultSet resultSet = metaData.getTables(null, null, table,
new String[] {"TABLE"});

while (resultSet.next()) {
final String tableName = resultSet.getString("TABLE_NAME");

if (tableName !=null && tableName.equalsIgnoreCase(table)) {
for (final String column : columns) {
try (final Statement statement = connection.createStatement()) {
statement.execute("ALTER TABLE " + table + " DROP " + column);
}
}
}
}

Expand Down

0 comments on commit d737d73

Please sign in to comment.