MythTV  0.26-pre
dbcheck.cpp
Go to the documentation of this file.
00001 #include <QString>
00002 #include <QDir>
00003 #include <QStringList>
00004 #include <QSqlError>
00005 
00006 #include <mythcontext.h>
00007 #include <mythdb.h>
00008 
00009 #include "dbcheck.h"
00010 
00011 const QString currentDatabaseVersion = "1006";
00012 
00013 static bool UpdateDBVersionNumber(const QString &newnumber)
00014 {
00015     if (!gCoreContext->SaveSettingOnHost("WeatherDBSchemaVer",newnumber,NULL))
00016     {
00017         LOG(VB_GENERAL, LOG_ERR,
00018             QString("DB Error (Setting new DB version number): %1\n")
00019                 .arg(newnumber));
00020 
00021         return false;
00022     }
00023 
00024     return true;
00025 }
00026 
00027 static bool performActualUpdate(const QStringList updates, QString version,
00028                                 QString &dbver)
00029 {
00030     LOG(VB_GENERAL, LOG_NOTICE,
00031         "Upgrading to MythWeather schema version " + version);
00032 
00033     MSqlQuery query(MSqlQuery::InitCon());
00034 
00035     QStringList::const_iterator it = updates.begin();
00036 
00037     while (it != updates.end())
00038     {
00039         QString thequery = *it;
00040         if (!query.exec(thequery))
00041         {
00042             QString msg =
00043                 QString("DB Error (Performing database upgrade): \n"
00044                         "Query was: %1 \nError was: %2 \nnew version: %3")
00045                 .arg(thequery)
00046                 .arg(MythDB::DBErrorMessage(query.lastError()))
00047                 .arg(version);
00048             LOG(VB_GENERAL, LOG_ERR, msg);
00049             return false;
00050         }
00051         ++it;
00052     }
00053 
00054     if (!UpdateDBVersionNumber(version))
00055         return false;
00056 
00057     dbver = version;
00058     return true;
00059 }
00060 
00061 /*
00062  * TODO Probably the biggest change to simplify things would be to get rid of
00063  * the surrogate key screen_id in weatherscreens, draworder should be unique,
00064  * that way, with cascading, updating screens won't need to blow out everything
00065  * in the db everytime.
00066  */
00067 bool InitializeDatabase()
00068 {
00069     QString dbver = gCoreContext->GetSetting("WeatherDBSchemaVer");
00070 
00071     if (dbver == currentDatabaseVersion)
00072         return true;
00073 
00074     if (dbver == "")
00075     {
00076         LOG(VB_GENERAL, LOG_NOTICE,
00077             "Inserting MythWeather initial database information.");
00078         QStringList updates;
00079         updates << "CREATE TABLE IF NOT EXISTS weathersourcesettings ("
00080                         "sourceid INT UNSIGNED NOT NULL AUTO_INCREMENT,"
00081                         "source_name VARCHAR(64) NOT NULL,"
00082                         "update_timeout INT UNSIGNED NOT NULL DEFAULT '600',"
00083                         "retrieve_timeout INT UNSIGNED NOT NULL DEFAULT '60',"
00084                         "hostname VARCHAR(255) NULL,"
00085                         "path VARCHAR(255) NULL,"
00086                         "author VARCHAR(128) NULL,"
00087                         "version VARCHAR(32) NULL,"
00088                         "email VARCHAR(255) NULL,"
00089                         "types MEDIUMTEXT NULL,"
00090                         "PRIMARY KEY(sourceid)) ENGINE=InnoDB;"
00091                << "CREATE TABLE IF NOT EXISTS weatherscreens ("
00092                         "screen_id INT UNSIGNED NOT NULL AUTO_INCREMENT,"
00093                         "draworder INT UNSIGNED NOT NULL,"
00094                         "container VARCHAR(64) NOT NULL,"
00095                         "hostname VARCHAR(255) NULL,"
00096                         "units TINYINT UNSIGNED NOT NULL,"
00097                         "PRIMARY KEY(screen_id)) ENGINE=InnoDB;"
00098                << "CREATE TABLE IF NOT EXISTS weatherdatalayout ("
00099                         "location VARCHAR(64) NOT NULL,"
00100                         "dataitem VARCHAR(64) NOT NULL,"
00101                         "weatherscreens_screen_id INT UNSIGNED NOT NULL,"
00102                         "weathersourcesettings_sourceid INT UNSIGNED NOT NULL,"
00103                         "PRIMARY KEY(location, dataitem, weatherscreens_screen_id,"
00104                             "weathersourcesettings_sourceid),"
00105                         "INDEX weatherdatalayout_FKIndex1(weatherscreens_screen_id),"
00106                         "INDEX weatherdatalayout_FKIndex2(weathersourcesettings_sourceid),"
00107                         "FOREIGN KEY(weatherscreens_screen_id) "
00108                         "REFERENCES weatherscreens(screen_id) "
00109                             "ON DELETE CASCADE "
00110                             "ON UPDATE CASCADE,"
00111                         "FOREIGN KEY(weathersourcesettings_sourceid) "
00112                         "REFERENCES weathersourcesettings(sourceid) "
00113                         "ON DELETE RESTRICT "
00114                         "ON UPDATE CASCADE) ENGINE=InnoDB;";
00115         /*
00116          * TODO Possible want to delete old stuff (i.e. agressiveness, locale..)
00117          * that we don't use any more
00118          */
00119 
00120         if (!performActualUpdate(updates, "1000", dbver))
00121             return false;
00122     }
00123 
00124     if (dbver == "1000")
00125     {
00126         QStringList updates;
00127         updates << "ALTER TABLE weathersourcesettings ADD COLUMN updated "
00128                    "TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP "
00129                    "          ON UPDATE CURRENT_TIMESTAMP;";
00130 
00131         if (!performActualUpdate(updates, "1001", dbver))
00132             return false;
00133     }
00134 
00135 
00136 
00137     if (dbver == "1001")
00138     {
00139         QStringList updates;
00140         updates << QString("ALTER DATABASE %1 DEFAULT CHARACTER SET latin1;")
00141             .arg(gContext->GetDatabaseParams().dbName) <<
00142             "ALTER TABLE weatherdatalayout"
00143             "  MODIFY location varbinary(64) NOT NULL,"
00144             "  MODIFY dataitem varbinary(64) NOT NULL;" <<
00145             "ALTER TABLE weatherscreens"
00146             "  MODIFY container varbinary(64) NOT NULL,"
00147             "  MODIFY hostname varbinary(64) default NULL;" <<
00148             "ALTER TABLE weathersourcesettings"
00149             "  MODIFY source_name varbinary(64) NOT NULL,"
00150             "  MODIFY hostname varbinary(64) default NULL,"
00151             "  MODIFY path varbinary(255) default NULL,"
00152             "  MODIFY author varbinary(128) default NULL,"
00153             "  MODIFY version varbinary(32) default NULL,"
00154             "  MODIFY email varbinary(255) default NULL,"
00155             "  MODIFY types mediumblob;";
00156 
00157         if (!performActualUpdate(updates, "1002", dbver))
00158             return false;
00159     }
00160 
00161 
00162     if (dbver == "1002")
00163     {
00164         QStringList updates;
00165         updates << QString("ALTER DATABASE %1 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;")
00166                 .arg(gContext->GetDatabaseParams().dbName) <<
00167             "ALTER TABLE weatherdatalayout"
00168             "  DEFAULT CHARACTER SET default,"
00169             "  MODIFY location varchar(64) CHARACTER SET utf8 NOT NULL,"
00170             "  MODIFY dataitem varchar(64) CHARACTER SET utf8 NOT NULL;" <<
00171             "ALTER TABLE weatherscreens"
00172             "  DEFAULT CHARACTER SET default,"
00173             "  MODIFY container varchar(64) CHARACTER SET utf8 NOT NULL,"
00174             "  MODIFY hostname varchar(64) CHARACTER SET utf8 default NULL;" <<
00175             "ALTER TABLE weathersourcesettings"
00176             "  DEFAULT CHARACTER SET default,"
00177             "  MODIFY source_name varchar(64) CHARACTER SET utf8 NOT NULL,"
00178             "  MODIFY hostname varchar(64) CHARACTER SET utf8 default NULL,"
00179             "  MODIFY path varchar(255) CHARACTER SET utf8 default NULL,"
00180             "  MODIFY author varchar(128) CHARACTER SET utf8 default NULL,"
00181             "  MODIFY version varchar(32) CHARACTER SET utf8 default NULL,"
00182             "  MODIFY email varchar(255) CHARACTER SET utf8 default NULL,"
00183             "  MODIFY types mediumtext CHARACTER SET utf8;";
00184 
00185         if (!performActualUpdate(updates, "1003", dbver))
00186             return false;
00187     }
00188 
00189     if (dbver == "1003")
00190     {
00191         QStringList updates;
00192         updates << "DELETE FROM keybindings "
00193                    " WHERE action = 'DELETE' AND context = 'Weather';";
00194 
00195         if (!performActualUpdate(updates, "1004", dbver))
00196             return false;
00197     }
00198 
00199     if (dbver == "1004")
00200     {
00201         QStringList updates;
00202         updates << "ALTER TABLE weatherdatalayout"
00203                    "  MODIFY location varchar(128) CHARACTER SET utf8 NOT NULL;";
00204 
00205         if (!performActualUpdate(updates, "1005", dbver))
00206             return false;
00207     }
00208 
00209     if (dbver == "1005")
00210     {
00211         QStringList updates;
00212         updates << "ALTER TABLE weathersourcesettings MODIFY COLUMN updated "
00213                    "  TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP "
00214                    "            ON UPDATE CURRENT_TIMESTAMP;";
00215 
00216         if (!performActualUpdate(updates, "1006", dbver))
00217             return false;
00218     }
00219 
00220     return true;
00221 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends