00001 #include <iostream>
00002
00003
00004 #include <qstring.h>
00005 #include <qdir.h>
00006
00007
00008 #include <mythtv/mythcontext.h>
00009 #include <mythtv/mythdbcon.h>
00010
00011
00012 #include "dbcheck.h"
00013
00014
00015 const QString currentDatabaseVersion = "1001";
00016
00017 static bool UpdateDBVersionNumber(const QString &newnumber)
00018 {
00019
00020 if (!gContext->SaveSettingOnHost("ArchiveDBSchemaVer",newnumber,NULL))
00021 {
00022 VERBOSE(VB_IMPORTANT, QString("DB Error (Setting new DB version number): %1\n")
00023 .arg(newnumber));
00024
00025 return false;
00026 }
00027
00028 return true;
00029 }
00030
00031 static bool performActualUpdate(const QString updates[], QString version,
00032 QString &dbver)
00033 {
00034 MSqlQuery query(MSqlQuery::InitCon());
00035
00036 VERBOSE(VB_IMPORTANT, QString("Upgrading to MythArchive schema version ") +
00037 version);
00038
00039 int counter = 0;
00040 QString thequery = updates[counter];
00041
00042 while (thequery != "")
00043 {
00044 query.prepare(thequery);
00045 query.exec();
00046
00047 if (query.lastError().type() != QSqlError::None)
00048 {
00049 QString msg =
00050 QString("DB Error (Performing database upgrade): \n"
00051 "Query was: %1 \nError was: %2 \nnew version: %3")
00052 .arg(thequery)
00053 .arg(MythContext::DBErrorMessage(query.lastError()))
00054 .arg(version);
00055 VERBOSE(VB_IMPORTANT, msg);
00056 return false;
00057 }
00058
00059 counter++;
00060 thequery = updates[counter];
00061 }
00062
00063 if (!UpdateDBVersionNumber(version))
00064 return false;
00065
00066 dbver = version;
00067 return true;
00068 }
00069
00070 bool UpgradeArchiveDatabaseSchema(void)
00071 {
00072 QString dbver = gContext->GetSetting("ArchiveDBSchemaVer");
00073
00074 if (dbver == currentDatabaseVersion)
00075 return true;
00076
00077 if (dbver == "")
00078 {
00079 VERBOSE(VB_IMPORTANT, "Inserting MythArchive initial database information.");
00080
00081 const QString updates[] = {
00082 "DROP TABLE IF EXISTS archiveitems;",
00083
00084 "CREATE TABLE IF NOT EXISTS archiveitems ("
00085 " intid INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,"
00086 " type set ('Recording','Video','File'),"
00087 " title VARCHAR(128),"
00088 " subtitle VARCHAR(128),"
00089 " description TEXT,"
00090 " startdate VARCHAR(30),"
00091 " starttime VARCHAR(30),"
00092 " size INT UNSIGNED NOT NULL,"
00093 " filename TEXT NOT NULL,"
00094 " hascutlist BOOL NOT NULL DEFAULT 0,"
00095 " cutlist TEXT DEFAULT '',"
00096 " INDEX (title)"
00097 ");",
00098 ""
00099 };
00100 if (!performActualUpdate(updates, "1000", dbver))
00101 return false;
00102 }
00103
00104 if (dbver == "1000")
00105 {
00106 const QString updates[] =
00107 {
00108 "ALTER TABLE archiveitems MODIFY size BIGINT UNSIGNED NOT NULL;",
00109 ""
00110 };
00111
00112 if (!performActualUpdate(updates, "1001", dbver))
00113 return false;
00114 }
00115
00116 return true;
00117 }
00118