Include time zone in event timestamp for Spanner implementation
Google Cloud Spanner adds seven hours to java.sql.Timestamp objects
when converting to UTC, resulting in entries being timestamped as
occurring in the future.
I believe this is related to their treatment of Timestamp literals,
which assume a default time zone of America/Los_Angeles:
https://cloud.google.com/spanner/docs/reference/standard-sql/lexical#timestamp_literals
Using Instant includes the time zone in the String which keeps the
timestamp accurate in Spanner.
Change-Id: I313af4fbd7803478fb0604913ee2d781de37c895
diff --git a/src/main/java/com/ericsson/gerrit/plugins/eventslog/sql/SQLClient.java b/src/main/java/com/ericsson/gerrit/plugins/eventslog/sql/SQLClient.java
index 2056e71..30643ff 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/eventslog/sql/SQLClient.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/eventslog/sql/SQLClient.java
@@ -41,6 +41,7 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
+import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
@@ -132,9 +133,21 @@
* @throws SQLException If there was a problem with the database
*/
void storeEvent(String projectName, Timestamp timestamp, String event) throws SQLException {
+ String values;
+ switch (databaseDialect) {
+ case SPANNER:
+ // Workaround since Spanner assumes timestamps without explicit time zone to be US/LA
+ Instant instant = (timestamp != null) ? timestamp.toInstant() : null;
+ values = format("VALUES('%s', '%s', '%s')", projectName, instant, event);
+ break;
+ default:
+ values = format("VALUES('%s', '%s', '%s')", projectName, timestamp, event);
+ break;
+ }
+
execute(
format("INSERT INTO %s(%s, %s, %s) ", TABLE_NAME, PROJECT_ENTRY, DATE_ENTRY, EVENT_ENTRY)
- + format("VALUES('%s', '%s', '%s')", projectName, timestamp, event));
+ + values);
}
/**