Skip to content

Commit

Permalink
fixed SQL update and added ALTER command to update schema
Browse files Browse the repository at this point in the history
  • Loading branch information
albogdano committed Jun 15, 2018
1 parent 0b75ca0 commit 6db4a56
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions src/main/java/com/erudika/para/persistence/H2Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ private static boolean addJsonUpdatesColumnIfMissing(String appid) {
try (ResultSet res = p.executeQuery(); Statement addColumnPS = conn.createStatement()) {
if (!res.next()) {
addColumnPS.execute(Utils.formatMessage("ALTER TABLE {0} ADD json_updates NVARCHAR", table));
logger.info("Table '{}' was altered to include 'json_updates' column.", table);
return true;
}
}
Expand All @@ -217,6 +218,29 @@ private static boolean addJsonUpdatesColumnIfMissing(String appid, String except
String table = getTableNameForAppid(appid);
try (Connection conn = getConnection(); Statement addColumnPS = conn.createStatement()) {
addColumnPS.execute(Utils.formatMessage("ALTER TABLE {0} ADD json_updates NVARCHAR", table));
logger.info("Table '{}' was altered to include 'json_updates' column.", table);
return true;
} catch (Exception e) {
logger.error(null, e);
}
}
return false;
}

/**
* @param appid app id
* @param exceptionMsg exception reason
* @return true if table was altered
*/
private static boolean removeTimestampUpdatedColumns(String appid, String exceptionMsg) {
if (StringUtils.isBlank(appid)) {
return false;
}
if (StringUtils.containsIgnoreCase(exceptionMsg, "column count")) {
String table = getTableNameForAppid(appid);
try (Connection conn = getConnection(); Statement removeColumnPS = conn.createStatement()) {
removeColumnPS.execute(Utils.formatMessage("ALTER TABLE {0} DROP COLUMN timestamp, updated", table));
logger.info("Table {} was altered - 'timestamp', 'updated' columns were removed.", table);
return true;
} catch (Exception e) {
logger.error(null, e);
Expand Down Expand Up @@ -276,9 +300,10 @@ protected static <P extends ParaObject> Map<String, P> readRows(String appid, Li
return results;
}
} catch (Exception e) {
logger.error(null, e);
if (addJsonUpdatesColumnIfMissing(appid, e.getMessage())) {
readRows(appid, ids);
} else {
logger.error(null, e);
}
}
return Collections.emptyMap();
Expand Down Expand Up @@ -319,7 +344,11 @@ protected static <P extends ParaObject> void createRows(String appid, List<P> ob
}
ps.executeBatch();
} catch (Exception e) {
logger.error(null, e);
if (removeTimestampUpdatedColumns(appid, e.getMessage())) {
createRows(appid, objects);
} else {
logger.error(null, e);
}
}
}

Expand All @@ -334,8 +363,7 @@ protected static <P extends ParaObject> void updateRows(String appid, List<P> ob
return;
}
String table = getTableNameForAppid(appid);
String sql = Utils.formatMessage("UPDATE {0} SET json_updates=? "
+ "WHERE {2} = ?", table, Config._ID);
String sql = Utils.formatMessage("UPDATE {0} SET json_updates=? WHERE {1} = ?", table, Config._ID);

try (Connection conn = getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
for (P object : objects) {
Expand All @@ -349,9 +377,10 @@ protected static <P extends ParaObject> void updateRows(String appid, List<P> ob
}
ps.executeBatch();
} catch (Exception e) {
logger.error(null, e);
if (addJsonUpdatesColumnIfMissing(appid, e.getMessage())) {
updateRows(appid, objects);
} else {
logger.error(null, e);
}
}
}
Expand Down Expand Up @@ -426,9 +455,10 @@ protected static <P extends ParaObject> List<P> scanRows(String appid, Pager pag
return results;
}
} catch (Exception e) {
logger.error(null, e);
if (addJsonUpdatesColumnIfMissing(appid, e.getMessage())) {
scanRows(appid, pager);
} else {
logger.error(null, e);
}
}
return Collections.emptyList();
Expand Down

0 comments on commit 6db4a56

Please sign in to comment.