#792 DataSource: when creating registrationDate column, set all rows to the current date

This commit is contained in:
ljacqu
2017-10-20 21:31:58 +02:00
parent a425eacf2d
commit b5ea48085c
2 changed files with 56 additions and 24 deletions
@@ -111,8 +111,7 @@ public class SQLite implements DataSource {
}
if (isColumnMissing(md, col.REGISTRATION_DATE)) {
st.executeUpdate("ALTER TABLE " + tableName
+ " ADD COLUMN " + col.REGISTRATION_DATE + " TIMESTAMP NOT NULL DEFAULT '0';");
addRegistrationDateColumn(st);
}
if (isColumnMissing(md, col.LASTLOC_X)) {
@@ -632,6 +631,25 @@ public class SQLite implements DataSource {
.build();
}
/**
* Creates the column for registration date and sets all entries to the current timestamp.
* We do so in order to avoid issues with purging, where entries with 0 / NULL might get
* purged immediately on startup otherwise.
*
* @param st Statement object to the database
*/
private void addRegistrationDateColumn(Statement st) throws SQLException {
st.executeUpdate("ALTER TABLE " + tableName
+ " ADD COLUMN " + col.REGISTRATION_DATE + " TIMESTAMP NOT NULL DEFAULT '0';");
// Use the timestamp from Java to avoid timezone issues in case JVM and database are out of sync
long currentTimestamp = System.currentTimeMillis();
int updatedRows = st.executeUpdate(String.format("UPDATE %s SET %s = %d;",
tableName, col.REGISTRATION_DATE, currentTimestamp));
ConsoleLogger.info("Created column '" + col.REGISTRATION_DATE + "' and set the current timestamp, "
+ currentTimestamp + ", to all " + updatedRows + " rows");
}
private static void close(Statement st) {
if (st != null) {
try {