From d06b9685dce93192d61042a61b3d251ca12c2957 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Mon, 11 Dec 2006 21:35:05 +0000 Subject: [PATCH] Add some hackery to work around MySQL's strange habit of assigning 0 instead of the current time when adding a TIMESTAMP column to a table (even if you explicitly define the TIMESTAMP column with DEFAULT CURRENT_TIMESTAMP). --- .../samskivert/jdbc/depot/DepotMarshaller.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java b/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java index acd9cb6..645a62a 100644 --- a/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java +++ b/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java @@ -361,6 +361,21 @@ public class DepotMarshaller } finally { stmt.close(); } + + // if the column is a TIMESTAMP column, we need to run a special query to update all + // existing rows to the current time because MySQL annoyingly assigns them a default + // value of "0000-00-00 00:00:00" regardless of whether we explicitly provide a + // "DEFAULT" value for the column or not + if (coldef.toLowerCase().indexOf(" timestamp") != -1) { + query = "update " + getTableName() + " set " + fmarsh.getColumnName() + " = NOW()"; + log.info("Assigning current time to TIMESTAMP column: " + query); + stmt = conn.createStatement(); + try { + stmt.executeUpdate(query); + } finally { + stmt.close(); + } + } } // TODO: run any registered hand migrations