00001 #include <iostream>
00002 using namespace std;
00003
00004
00005 #include <QString>
00006 #include <QSqlError>
00007
00008
00009 #include "mythcontext.h"
00010 #include "mythdb.h"
00011
00012
00013 #include "dbcheck.h"
00014
00015 const QString currentDatabaseVersion = "1003";
00016
00017 static bool UpdateDBVersionNumber(const QString &newnumber)
00018 {
00019
00020 if (!gCoreContext->SaveSettingOnHost("GalleryDBSchemaVer",newnumber,NULL))
00021 {
00022 LOG(VB_GENERAL, LOG_ERR,
00023 QString("DB Error (Setting new DB version number): %1\n")
00024 .arg(newnumber));
00025
00026 return false;
00027 }
00028
00029 return true;
00030 }
00031
00032 static bool performActualUpdate(const QString updates[], QString version,
00033 QString &dbver)
00034 {
00035 MSqlQuery query(MSqlQuery::InitCon());
00036
00037 LOG(VB_GENERAL, LOG_NOTICE,
00038 "Upgrading to MythGallery schema version " + version);
00039
00040 int counter = 0;
00041 QString thequery = updates[counter];
00042
00043 while (thequery != "")
00044 {
00045 if (!query.exec(thequery))
00046 {
00047 QString msg =
00048 QString("DB Error (Performing database upgrade): \n"
00049 "Query was: %1 \nError was: %2 \nnew version: %3")
00050 .arg(thequery)
00051 .arg(MythDB::DBErrorMessage(query.lastError()))
00052 .arg(version);
00053 LOG(VB_GENERAL, LOG_ERR, msg);
00054 return false;
00055 }
00056
00057 counter++;
00058 thequery = updates[counter];
00059 }
00060
00061 if (!UpdateDBVersionNumber(version))
00062 return false;
00063
00064 dbver = version;
00065 return true;
00066 }
00067
00068 bool UpgradeGalleryDatabaseSchema(void)
00069 {
00070 QString dbver = gCoreContext->GetSetting("GalleryDBSchemaVer");
00071
00072 if (dbver == currentDatabaseVersion)
00073 return true;
00074
00075 if (dbver == "")
00076 {
00077 LOG(VB_GENERAL, LOG_NOTICE,
00078 "Inserting MythGallery initial database information.");
00079
00080 const QString updates[] = {
00081 "CREATE TABLE IF NOT EXISTS gallerymetadata ("
00082 " image VARCHAR(255) NOT NULL PRIMARY KEY,"
00083 " angle INTEGER NOT NULL"
00084 ");",
00085 "INSERT INTO settings VALUES ('GalleryDBSchemaVer', 1000, NULL);",
00086 ""
00087 };
00088 if (!performActualUpdate(updates, "1000", dbver))
00089 return false;
00090 }
00091
00092
00093
00094 if (dbver == "1000")
00095 {
00096 const QString updates[] = {
00097 QString("ALTER DATABASE %1 DEFAULT CHARACTER SET latin1;")
00098 .arg(gContext->GetDatabaseParams().dbName),
00099 "ALTER TABLE gallerymetadata"
00100 " MODIFY image varbinary(255) NOT NULL;",
00101 ""
00102 };
00103
00104 if (!performActualUpdate(updates, "1001", dbver))
00105 return false;
00106 }
00107
00108
00109 if (dbver == "1001")
00110 {
00111 const QString updates[] = {
00112 QString("ALTER DATABASE %1 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;")
00113 .arg(gContext->GetDatabaseParams().dbName),
00114 "ALTER TABLE gallerymetadata"
00115 " DEFAULT CHARACTER SET default,"
00116 " MODIFY image varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL;",
00117 ""
00118 };
00119
00120 if (!performActualUpdate(updates, "1002", dbver))
00121 return false;
00122 }
00123
00124 if (dbver == "1002")
00125 {
00126 const QString updates[] = {
00127 "DELETE FROM keybindings "
00128 " WHERE action = 'DELETE' AND context = 'Gallery';",
00129 ""
00130 };
00131
00132 if (!performActualUpdate(updates, "1003", dbver))
00133 return false;
00134 }
00135
00136 return true;
00137 }