diff -r 3b9abdae181e -r 84091b9cb800 src/MainWindow.cpp --- a/src/MainWindow.cpp Sat Jun 01 21:10:54 2024 +0200 +++ b/src/MainWindow.cpp Sat Jun 08 15:54:30 2024 +0200 @@ -164,7 +164,8 @@ */ void MainWindow::updateDataBase() { - QSqlQuery query1, query2, query4; + QSqlQuery query, query2, query4; + QString sql; int count = 0; bool added_packs = false; @@ -174,7 +175,7 @@ * Version 0.4.0. * Make sure we have the inventory_yeastpack with initial records. */ - query1.exec("CREATE TABLE IF NOT EXISTS `inventory_yeastpack` (" + query.exec("CREATE TABLE IF NOT EXISTS `inventory_yeastpack` (" "`record` int(11) NOT NULL AUTO_INCREMENT," "`uuid` varchar(36) NOT NULL," "`laboratory` varchar(128) NOT NULL," @@ -192,25 +193,25 @@ "UNIQUE KEY `package` (`laboratory`,`form`,`package`)," "KEY `lab_form` (`laboratory`,`form`) USING BTREE" ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Yeast packages data'"); - if (query1.lastError().isValid()) { - qWarning() << " create inventory_yeastpack" << query1.lastError(); + if (query.lastError().isValid()) { + qWarning() << " create inventory_yeastpack" << query.lastError(); } else { - query1.exec("SELECT DISTINCT laboratory,form FROM inventory_yeasts"); - while (query1.next()) { + query.exec("SELECT DISTINCT laboratory,form FROM inventory_yeasts"); + while (query.next()) { query2.prepare("SELECT record FROM inventory_yeastpack WHERE laboratory=:laboratory AND form=:form"); - query2.bindValue(":laboratory", query1.value("laboratory").toString()); - query2.bindValue(":form", query1.value("form").toInt()); + query2.bindValue(":laboratory", query.value("laboratory").toString()); + query2.bindValue(":form", query.value("form").toInt()); query2.exec(); if (! query2.first()) { - qDebug() << " add yeastpack" << query1.value("laboratory").toString() << query1.value("form").toInt(); + qDebug() << " add yeastpack" << query.value("laboratory").toString() << query.value("form").toInt(); query4.prepare("INSERT INTO inventory_yeastpack SET uuid=:uuid, laboratory=:laboratory, " "form=:form, package=:package, viability=:viability, max=:max"); query4.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36)); - query4.bindValue(":laboratory", query1.value("laboratory").toString()); - query4.bindValue(":form", query1.value("form").toInt()); - query4.bindValue(":package", g_yeast_forms[query1.value("form").toInt()]); - switch (query1.value("form").toInt()) { + query4.bindValue(":laboratory", query.value("laboratory").toString()); + query4.bindValue(":form", query.value("form").toInt()); + query4.bindValue(":package", g_yeast_forms[query.value("form").toInt()]); + switch (query.value("form").toInt()) { case YEAST_FORMS_LIQUID: query4.bindValue(":viability", 0.80); query4.bindValue(":max", 97); break; @@ -234,6 +235,52 @@ } } + /* + * Version 0.4.6 + * Upgrade database extra columns for target water. + */ + query.exec("SHOW COLUMNS FROM `products` LIKE 'tw_name'"); + if (! query.first()) { + qWarning() << "updateDataBase: products with target water"; + sql = "ALTER TABLE `products` ADD `tw_name` VARCHAR(64) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL AFTER `calc_acid`, "; + sql.append("ADD `tw_calcium` FLOAT NOT NULL DEFAULT '0' AFTER `tw_name`, "); + sql.append("ADD `tw_sulfate` FLOAT NOT NULL DEFAULT '0' AFTER `tw_calcium`, "); + sql.append("ADD `tw_chloride` FLOAT NOT NULL DEFAULT '0' AFTER `tw_sulfate`, "); + sql.append("ADD `tw_sodium` FLOAT NOT NULL DEFAULT '0' AFTER `tw_chloride`, "); + sql.append("ADD `tw_magnesium` FLOAT NOT NULL DEFAULT '0' AFTER `tw_sodium`, "); + sql.append("ADD `tw_total_alkalinity` FLOAT NOT NULL DEFAULT '0' AFTER `tw_magnesium`, "); + sql.append("ADD `tw_ph` FLOAT NOT NULL DEFAULT '7' AFTER `tw_total_alkalinity`, "); + sql.append("ADD `eq_HLT_volume` FLOAT NOT NULL DEFAULT '20' AFTER `eq_chiller_loss`, "); + sql.append("ADD `eq_HLT_deadspace` FLOAT NOT NULL DEFAULT '2' AFTER `eq_HLT_volume`;"); +// qDebug() << sql; + query.exec(sql); + if (query.lastError().isValid()) { + qWarning() << " ALTER products" << query.lastError(); + } else { + count++; + } + } + + /* + * Version 0.4.6 + * Add HLT fields to equipments + */ + query.exec("SHOW COLUMNS FROM `inventory_equipments` LIKE 'HLT_volume'"); + if (! query.first()) { + qWarning() << "updateDataBase: inventory_equipments with HLT"; + sql = "ALTER TABLE `inventory_equipments` ADD `HLT_volume` FLOAT NOT NULL DEFAULT '20' AFTER `chiller_lpm`, "; + sql.append("ADD `HLT_deadspace` FLOAT NOT NULL DEFAULT '2' AFTER `HLT_volume`;"); +// qDebug() << sql; + query.exec(sql); + if (query.lastError().isValid()) { + qWarning() << " ALTER inventory_equipments" << query.lastError(); + } else { + count++; + QMessageBox::information(this, tr("Equipments upgrade"), + tr("The Equipents database has two new HLT fields.\nPlease check the Inventory->Equipents records")); + } + } + qDebug() << "updateDatabase()" << count << "updates"; }