Permalink
Browse files

Add UTC conversion DB update.

  • Loading branch information...
daniel-kristjansson committed May 5, 2012
1 parent c6f6410 commit 0d836c7357d4357451f25bf993384136b189a408
@@ -114,7 +114,7 @@ package MythTV;
# schema version supported in the main code. We need to check that the schema
# version in the database is as expected by the bindings, which are expected
# to be kept in sync with the main code.
- our $SCHEMA_VERSION = "1302";
+ our $SCHEMA_VERSION = "1303";
# NUMPROGRAMLINES is defined in mythtv/libs/libmythtv/programinfo.h and is
# the number of items in a ProgramInfo QStringList group used by
@@ -5,7 +5,7 @@
"""
OWN_VERSION = (0,26,-1,0)
-SCHEMA_VERSION = 1302
+SCHEMA_VERSION = 1303
NVSCHEMA_VERSION = 1007
MUSICSCHEMA_VERSION = 1018
PROTO_VERSION = '74'
@@ -57,7 +57,7 @@
* mythtv/bindings/php/MythBackend.php
#endif
-#define MYTH_DATABASE_VERSION "1302"
+#define MYTH_DATABASE_VERSION "1303"
MBASE_PUBLIC const char *GetMythSourceVersion();
@@ -1997,6 +1997,94 @@ NULL
return false;
}
+ if (dbver == "1302")
+ {
+ QDateTime loc = QDateTime::currentDateTime();
+ QDateTime utc = loc.toUTC();
+ loc = QDateTime(loc.date(), loc.time(), Qt::UTC);
+ int utc_offset = loc.secsTo(utc) / 60;
+
+ QList<QByteArray> updates_ba;
+
+ // Convert DATE and TIME in record into DATETIME
+ const char *pre_sql[] = {
+ "CREATE TEMPORARY TABLE recordupdate ("
+ "recid INT, starttime DATETIME, endtime DATETIME)",
+ "INSERT INTO recordupdate (recid, starttime, endtime) "
+ "SELECT recordid, "
+ " CONCAT(startdate, ' ', starttime), "
+ " CONCAT(enddate, ' ', endtime) FROM record",
+ };
+ for (uint i = 0; i < sizeof(pre_sql)/sizeof(char*); i++)
+ updates_ba.push_back(QByteArray(pre_sql[i]));
+
+ // Convert various DATETIME fields from local time to UTC
+ if (0 != utc_offset)
+ {
+ const char *with_endtime[] = {
+ "program", "recorded", "oldrecorded", "recordupdate",
+ };
+ const char *without_endtime[] = {
+ "programgenres", "programrating", "credits",
+ "jobqueue",
+ };
+ QString order = (utc_offset > 0) ? "-starttime" : "starttime";
+
+ for (uint i = 0; i < sizeof(with_endtime)/sizeof(char*); i++)
+ {
+ updates_ba.push_back(
+ QString("UPDATE %1 "
+ "SET starttime = starttime + interval %2 minute, "
+ " endtime = endtime + interval %3 minute "
+ "ORDER BY %4")
+ .arg(with_endtime[i]).arg(utc_offset).arg(utc_offset)
+ .arg(order).toLocal8Bit());
+ }
+
+ for (uint i = 0; i < sizeof(without_endtime)/sizeof(char*); i++)
+ {
+ updates_ba.push_back(
+ QString("UPDATE %1 "
+ "SET starttime = starttime + interval %2 minute "
+ "ORDER BY %3")
+ .arg(without_endtime[i]).arg(utc_offset).arg(order)
+ .toLocal8Bit());
+ }
+
+ updates_ba.push_back(
+ QString("UPDATE oldprogram "
+ "SET airdate = airdate + interval %2 minute "
+ "ORDER BY %3")
+ .arg(utc_offset).arg((utc_offset > 0) ? "-airdate" : "airdate")
+ .toLocal8Bit());
+ }
+
+ // Convert DATETIME back to seperate DATE and TIME in record table
+ const char *post_sql[] = {
+ "UPDATE record, recordupdate "
+ "SET record.startdate = DATE(recordupdate.starttime), "
+ " record.starttime = TIME(recordupdate.starttime), "
+ " record.enddate = DATE(recordupdate.endtime), "
+ " record.endtime = TIME(recordupdate.endtime) "
+ "WHERE recordid = recid",
+ "DROP TABLE recordupdate",
+ };
+
+ for (uint i = 0; i < sizeof(post_sql)/sizeof(char*); i++)
+ updates_ba.push_back(QByteArray(post_sql[i]));
+
+ // Convert update ByteArrays to NULL terminated char**
+ QList<QByteArray>::const_iterator it = updates_ba.begin();
+ vector<const char*> updates;
+ for (; it != updates_ba.end(); ++it)
+ updates.push_back((*it).constData());
+ updates.push_back(NULL);
+
+ // do the actual update
+ if (!performActualUpdate(&updates[0], "1303", dbver))
+ return false;
+ }
+
return true;
}

0 comments on commit 0d836c7

Please sign in to comment.