src/MainWindow.cpp

changeset 527
84091b9cb800
parent 523
632591d9825e
equal deleted inserted replaced
526:3b9abdae181e 527:84091b9cb800
162 /* 162 /*
163 * Upgrade database. Check and do upgrades. 163 * Upgrade database. Check and do upgrades.
164 */ 164 */
165 void MainWindow::updateDataBase() 165 void MainWindow::updateDataBase()
166 { 166 {
167 QSqlQuery query1, query2, query4; 167 QSqlQuery query, query2, query4;
168 QString sql;
168 int count = 0; 169 int count = 0;
169 bool added_packs = false; 170 bool added_packs = false;
170 171
171 qDebug() << "updateDatabase() start"; 172 qDebug() << "updateDatabase() start";
172 173
173 /* 174 /*
174 * Version 0.4.0. 175 * Version 0.4.0.
175 * Make sure we have the inventory_yeastpack with initial records. 176 * Make sure we have the inventory_yeastpack with initial records.
176 */ 177 */
177 query1.exec("CREATE TABLE IF NOT EXISTS `inventory_yeastpack` (" 178 query.exec("CREATE TABLE IF NOT EXISTS `inventory_yeastpack` ("
178 "`record` int(11) NOT NULL AUTO_INCREMENT," 179 "`record` int(11) NOT NULL AUTO_INCREMENT,"
179 "`uuid` varchar(36) NOT NULL," 180 "`uuid` varchar(36) NOT NULL,"
180 "`laboratory` varchar(128) NOT NULL," 181 "`laboratory` varchar(128) NOT NULL,"
181 "`form` tinyint(4) NOT NULL DEFAULT 0," 182 "`form` tinyint(4) NOT NULL DEFAULT 0,"
182 "`package` varchar(128) NOT NULL," 183 "`package` varchar(128) NOT NULL,"
190 "PRIMARY KEY (`record`)," 191 "PRIMARY KEY (`record`),"
191 "UNIQUE KEY `uuid` (`uuid`)," 192 "UNIQUE KEY `uuid` (`uuid`),"
192 "UNIQUE KEY `package` (`laboratory`,`form`,`package`)," 193 "UNIQUE KEY `package` (`laboratory`,`form`,`package`),"
193 "KEY `lab_form` (`laboratory`,`form`) USING BTREE" 194 "KEY `lab_form` (`laboratory`,`form`) USING BTREE"
194 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Yeast packages data'"); 195 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Yeast packages data'");
195 if (query1.lastError().isValid()) { 196 if (query.lastError().isValid()) {
196 qWarning() << " create inventory_yeastpack" << query1.lastError(); 197 qWarning() << " create inventory_yeastpack" << query.lastError();
197 } else { 198 } else {
198 query1.exec("SELECT DISTINCT laboratory,form FROM inventory_yeasts"); 199 query.exec("SELECT DISTINCT laboratory,form FROM inventory_yeasts");
199 while (query1.next()) { 200 while (query.next()) {
200 query2.prepare("SELECT record FROM inventory_yeastpack WHERE laboratory=:laboratory AND form=:form"); 201 query2.prepare("SELECT record FROM inventory_yeastpack WHERE laboratory=:laboratory AND form=:form");
201 query2.bindValue(":laboratory", query1.value("laboratory").toString()); 202 query2.bindValue(":laboratory", query.value("laboratory").toString());
202 query2.bindValue(":form", query1.value("form").toInt()); 203 query2.bindValue(":form", query.value("form").toInt());
203 query2.exec(); 204 query2.exec();
204 if (! query2.first()) { 205 if (! query2.first()) {
205 qDebug() << " add yeastpack" << query1.value("laboratory").toString() << query1.value("form").toInt(); 206 qDebug() << " add yeastpack" << query.value("laboratory").toString() << query.value("form").toInt();
206 207
207 query4.prepare("INSERT INTO inventory_yeastpack SET uuid=:uuid, laboratory=:laboratory, " 208 query4.prepare("INSERT INTO inventory_yeastpack SET uuid=:uuid, laboratory=:laboratory, "
208 "form=:form, package=:package, viability=:viability, max=:max"); 209 "form=:form, package=:package, viability=:viability, max=:max");
209 query4.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36)); 210 query4.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36));
210 query4.bindValue(":laboratory", query1.value("laboratory").toString()); 211 query4.bindValue(":laboratory", query.value("laboratory").toString());
211 query4.bindValue(":form", query1.value("form").toInt()); 212 query4.bindValue(":form", query.value("form").toInt());
212 query4.bindValue(":package", g_yeast_forms[query1.value("form").toInt()]); 213 query4.bindValue(":package", g_yeast_forms[query.value("form").toInt()]);
213 switch (query1.value("form").toInt()) { 214 switch (query.value("form").toInt()) {
214 case YEAST_FORMS_LIQUID: query4.bindValue(":viability", 0.80); 215 case YEAST_FORMS_LIQUID: query4.bindValue(":viability", 0.80);
215 query4.bindValue(":max", 97); 216 query4.bindValue(":max", 97);
216 break; 217 break;
217 case YEAST_FORMS_DRY: query4.bindValue(":viability", 0.998); 218 case YEAST_FORMS_DRY: query4.bindValue(":viability", 0.998);
218 query4.bindValue(":max", 100); 219 query4.bindValue(":max", 100);
230 count++; 231 count++;
231 added_packs = true; 232 added_packs = true;
232 } 233 }
233 } 234 }
234 } 235 }
236 }
237
238 /*
239 * Version 0.4.6
240 * Upgrade database extra columns for target water.
241 */
242 query.exec("SHOW COLUMNS FROM `products` LIKE 'tw_name'");
243 if (! query.first()) {
244 qWarning() << "updateDataBase: products with target water";
245 sql = "ALTER TABLE `products` ADD `tw_name` VARCHAR(64) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL AFTER `calc_acid`, ";
246 sql.append("ADD `tw_calcium` FLOAT NOT NULL DEFAULT '0' AFTER `tw_name`, ");
247 sql.append("ADD `tw_sulfate` FLOAT NOT NULL DEFAULT '0' AFTER `tw_calcium`, ");
248 sql.append("ADD `tw_chloride` FLOAT NOT NULL DEFAULT '0' AFTER `tw_sulfate`, ");
249 sql.append("ADD `tw_sodium` FLOAT NOT NULL DEFAULT '0' AFTER `tw_chloride`, ");
250 sql.append("ADD `tw_magnesium` FLOAT NOT NULL DEFAULT '0' AFTER `tw_sodium`, ");
251 sql.append("ADD `tw_total_alkalinity` FLOAT NOT NULL DEFAULT '0' AFTER `tw_magnesium`, ");
252 sql.append("ADD `tw_ph` FLOAT NOT NULL DEFAULT '7' AFTER `tw_total_alkalinity`, ");
253 sql.append("ADD `eq_HLT_volume` FLOAT NOT NULL DEFAULT '20' AFTER `eq_chiller_loss`, ");
254 sql.append("ADD `eq_HLT_deadspace` FLOAT NOT NULL DEFAULT '2' AFTER `eq_HLT_volume`;");
255 // qDebug() << sql;
256 query.exec(sql);
257 if (query.lastError().isValid()) {
258 qWarning() << " ALTER products" << query.lastError();
259 } else {
260 count++;
261 }
262 }
263
264 /*
265 * Version 0.4.6
266 * Add HLT fields to equipments
267 */
268 query.exec("SHOW COLUMNS FROM `inventory_equipments` LIKE 'HLT_volume'");
269 if (! query.first()) {
270 qWarning() << "updateDataBase: inventory_equipments with HLT";
271 sql = "ALTER TABLE `inventory_equipments` ADD `HLT_volume` FLOAT NOT NULL DEFAULT '20' AFTER `chiller_lpm`, ";
272 sql.append("ADD `HLT_deadspace` FLOAT NOT NULL DEFAULT '2' AFTER `HLT_volume`;");
273 // qDebug() << sql;
274 query.exec(sql);
275 if (query.lastError().isValid()) {
276 qWarning() << " ALTER inventory_equipments" << query.lastError();
277 } else {
278 count++;
279 QMessageBox::information(this, tr("Equipments upgrade"),
280 tr("The Equipents database has two new HLT fields.\nPlease check the Inventory->Equipents records"));
281 }
235 } 282 }
236 283
237 qDebug() << "updateDatabase()" << count << "updates"; 284 qDebug() << "updateDatabase()" << count << "updates";
238 } 285 }
239 286

mercurial