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