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