MythTV  master
weatherdbcheck.cpp
Go to the documentation of this file.
1 #include <QString>
2 #include <QDir>
3 #include <QStringList>
4 #include <QSqlError>
5 
6 #include <mythcontext.h>
7 #include <mythdb.h>
8 #include <mythdbcheck.h>
9 
10 #include "weatherdbcheck.h"
11 
12 const QString currentDatabaseVersion = "1006";
13 const QString MythWeatherVersionName = "WeatherDBSchemaVer";
14 
15 /*
16  * TODO Probably the biggest change to simplify things would be to get rid of
17  * the surrogate key screen_id in weatherscreens, draworder should be unique,
18  * that way, with cascading, updating screens won't need to blow out everything
19  * in the db everytime.
20  */
22 {
23  QString dbver = gCoreContext->GetSetting("WeatherDBSchemaVer");
24 
25  if (dbver == currentDatabaseVersion)
26  return true;
27 
28  if (dbver == "")
29  {
30  LOG(VB_GENERAL, LOG_NOTICE,
31  "Inserting MythWeather initial database information.");
32  DBUpdates updates {
33  "CREATE TABLE IF NOT EXISTS weathersourcesettings ("
34  "sourceid INT UNSIGNED NOT NULL AUTO_INCREMENT,"
35  "source_name VARCHAR(64) NOT NULL,"
36  "update_timeout INT UNSIGNED NOT NULL DEFAULT '600',"
37  "retrieve_timeout INT UNSIGNED NOT NULL DEFAULT '60',"
38  "hostname VARCHAR(255) NULL,"
39  "path VARCHAR(255) NULL,"
40  "author VARCHAR(128) NULL,"
41  "version VARCHAR(32) NULL,"
42  "email VARCHAR(255) NULL,"
43  "types MEDIUMTEXT NULL,"
44  "PRIMARY KEY(sourceid)) ENGINE=InnoDB;",
45  "CREATE TABLE IF NOT EXISTS weatherscreens ("
46  "screen_id INT UNSIGNED NOT NULL AUTO_INCREMENT,"
47  "draworder INT UNSIGNED NOT NULL,"
48  "container VARCHAR(64) NOT NULL,"
49  "hostname VARCHAR(255) NULL,"
50  "units TINYINT UNSIGNED NOT NULL,"
51  "PRIMARY KEY(screen_id)) ENGINE=InnoDB;",
52  "CREATE TABLE IF NOT EXISTS weatherdatalayout ("
53  "location VARCHAR(64) NOT NULL,"
54  "dataitem VARCHAR(64) NOT NULL,"
55  "weatherscreens_screen_id INT UNSIGNED NOT NULL,"
56  "weathersourcesettings_sourceid INT UNSIGNED NOT NULL,"
57  "PRIMARY KEY(location, dataitem, weatherscreens_screen_id,"
58  "weathersourcesettings_sourceid),"
59  "INDEX weatherdatalayout_FKIndex1(weatherscreens_screen_id),"
60  "INDEX weatherdatalayout_FKIndex2(weathersourcesettings_sourceid),"
61  "FOREIGN KEY(weatherscreens_screen_id) "
62  "REFERENCES weatherscreens(screen_id) "
63  "ON DELETE CASCADE "
64  "ON UPDATE CASCADE,"
65  "FOREIGN KEY(weathersourcesettings_sourceid) "
66  "REFERENCES weathersourcesettings(sourceid) "
67  "ON DELETE RESTRICT "
68  "ON UPDATE CASCADE) ENGINE=InnoDB;"
69  };
70  /*
71  * TODO Possible want to delete old stuff (i.e. agressiveness, locale..)
72  * that we don't use any more
73  */
74 
75  if (!performActualUpdate("MythWeather", MythWeatherVersionName,
76  updates, "1000", dbver))
77  return false;
78  }
79 
80  if (dbver == "1000")
81  {
82  DBUpdates updates {
83  "ALTER TABLE weathersourcesettings ADD COLUMN updated "
84  "TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP "
85  " ON UPDATE CURRENT_TIMESTAMP;"
86  };
87 
88  if (!performActualUpdate("MythWeather", MythWeatherVersionName,
89  updates, "1001", dbver))
90  return false;
91  }
92 
93 
94 
95  if (dbver == "1001")
96  {
97  DBUpdates updates {
98  qPrintable(QString("ALTER DATABASE %1 DEFAULT CHARACTER SET latin1;")
100  "ALTER TABLE weatherdatalayout"
101  " MODIFY location varbinary(64) NOT NULL,"
102  " MODIFY dataitem varbinary(64) NOT NULL;",
103  "ALTER TABLE weatherscreens"
104  " MODIFY container varbinary(64) NOT NULL,"
105  " MODIFY hostname varbinary(64) default NULL;",
106  "ALTER TABLE weathersourcesettings"
107  " MODIFY source_name varbinary(64) NOT NULL,"
108  " MODIFY hostname varbinary(64) default NULL,"
109  " MODIFY path varbinary(255) default NULL,"
110  " MODIFY author varbinary(128) default NULL,"
111  " MODIFY version varbinary(32) default NULL,"
112  " MODIFY email varbinary(255) default NULL,"
113  " MODIFY types mediumblob;"
114  };
115 
116  if (!performActualUpdate("MythWeather", MythWeatherVersionName,
117  updates, "1002", dbver))
118  return false;
119  }
120 
121 
122  if (dbver == "1002")
123  {
124  DBUpdates updates {
125  qPrintable(QString("ALTER DATABASE %1 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;")
127  "ALTER TABLE weatherdatalayout"
128  " DEFAULT CHARACTER SET default,"
129  " MODIFY location varchar(64) CHARACTER SET utf8 NOT NULL,"
130  " MODIFY dataitem varchar(64) CHARACTER SET utf8 NOT NULL;",
131  "ALTER TABLE weatherscreens"
132  " DEFAULT CHARACTER SET default,"
133  " MODIFY container varchar(64) CHARACTER SET utf8 NOT NULL,"
134  " MODIFY hostname varchar(64) CHARACTER SET utf8 default NULL;",
135  "ALTER TABLE weathersourcesettings"
136  " DEFAULT CHARACTER SET default,"
137  " MODIFY source_name varchar(64) CHARACTER SET utf8 NOT NULL,"
138  " MODIFY hostname varchar(64) CHARACTER SET utf8 default NULL,"
139  " MODIFY path varchar(255) CHARACTER SET utf8 default NULL,"
140  " MODIFY author varchar(128) CHARACTER SET utf8 default NULL,"
141  " MODIFY version varchar(32) CHARACTER SET utf8 default NULL,"
142  " MODIFY email varchar(255) CHARACTER SET utf8 default NULL,"
143  " MODIFY types mediumtext CHARACTER SET utf8;"
144  };
145 
146  if (!performActualUpdate("MythWeather", MythWeatherVersionName,
147  updates, "1003", dbver))
148  return false;
149  }
150 
151  if (dbver == "1003")
152  {
153  DBUpdates updates {
154  "DELETE FROM keybindings "
155  " WHERE action = 'DELETE' AND context = 'Weather';"
156  };
157 
158  if (!performActualUpdate("MythWeather", MythWeatherVersionName,
159  updates, "1004", dbver))
160  return false;
161  }
162 
163  if (dbver == "1004")
164  {
165  DBUpdates updates {
166  "ALTER TABLE weatherdatalayout"
167  " MODIFY location varchar(128) CHARACTER SET utf8 NOT NULL;"
168  };
169 
170  if (!performActualUpdate("MythWeather", MythWeatherVersionName,
171  updates, "1005", dbver))
172  return false;
173  }
174 
175  if (dbver == "1005")
176  {
177  DBUpdates updates {
178  "ALTER TABLE weathersourcesettings MODIFY COLUMN updated "
179  " TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP "
180  " ON UPDATE CURRENT_TIMESTAMP;"
181  };
182 
183  if (!performActualUpdate("MythWeather", MythWeatherVersionName,
184  updates, "1006", dbver))
185  return false;
186  }
187 
188  return true;
189 }
InitializeDatabase
bool InitializeDatabase()
Definition: weatherdbcheck.cpp:21
weatherdbcheck.h
mythdb.h
MythContext::GetDatabaseParams
DatabaseParams GetDatabaseParams(void)
Definition: mythcontext.cpp:1677
arg
arg(title).arg(filename).arg(doDelete))
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
currentDatabaseVersion
const QString currentDatabaseVersion
Definition: weatherdbcheck.cpp:12
MythWeatherVersionName
const QString MythWeatherVersionName
Definition: weatherdbcheck.cpp:13
mythdbcheck.h
DatabaseParams::m_dbName
QString m_dbName
database name
Definition: mythdbparams.h:26
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:60
performActualUpdate
bool performActualUpdate(const QString &component, const QString &versionkey, const DBUpdates &updates, const QString &version, QString &dbver)
Definition: dbcheckcommon.cpp:113
mythcontext.h
DBUpdates
std::vector< std::string > DBUpdates
Definition: mythdbcheck.h:9
gContext
MythContext * gContext
This global variable contains the MythContext instance for the application.
Definition: mythcontext.cpp:64
MythCoreContext::GetSetting
QString GetSetting(const QString &key, const QString &defaultval="")
Definition: mythcorecontext.cpp:919