Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 1 | <?php |
Adrià Vilanova Martínez | 5af8651 | 2023-12-02 20:44:16 +0100 | [diff] [blame] | 2 | /* |
| 3 | * hores |
| 4 | * Copyright (c) 2023 Adrià Vilanova Martínez |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as |
| 8 | * published by the Free Software Foundation, either version 3 of the |
| 9 | * License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public |
| 17 | * License along with this program. |
| 18 | * If not, see http://www.gnu.org/licenses/. |
| 19 | */ |
| 20 | |
Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 21 | // @description: Program used to upgrade the database after the implementation of issue #23. |
| 22 | |
| 23 | require_once(__DIR__."/../core.php"); |
| 24 | |
| 25 | if (php_sapi_name() != "cli") { |
| 26 | security::notFound(); |
| 27 | exit(); |
| 28 | } |
| 29 | |
| 30 | echo "=========================\n"; |
| 31 | echo "upgradebaixes2history.php\n"; |
| 32 | echo "=========================\n\n"; |
| 33 | |
| 34 | // Check whether the database was already upgraded |
| 35 | $prequery = mysqli_query($con, "SHOW COLUMNS FROM workers LIKE 'hidden'"); |
| 36 | if (!mysqli_num_rows($prequery)) { |
| 37 | die("[fatal error] The database was already upgraded.\n"); |
| 38 | } |
| 39 | |
| 40 | echo "[info] Adding new database schema...\n"; |
| 41 | if (!mysqli_query($con, "CREATE TABLE workhistory ( |
| 42 | id INT NOT NULL AUTO_INCREMENT, |
| 43 | PRIMARY KEY(id), |
| 44 | worker INT NOT NULL, |
| 45 | INDEX(worker), |
| 46 | day INT NOT NULL, |
| 47 | status INT NOT NULL |
| 48 | )")) die("[fatal error] Couldn't add new database.\n"); |
| 49 | |
| 50 | echo "[info] Transfering affiliation information from the workers table to the workhistory table...\n"; |
| 51 | $query = mysqli_query($con, "SELECT * FROM workers"); |
| 52 | |
| 53 | if ($query === false) exit(); |
| 54 | |
| 55 | while ($worker = mysqli_fetch_assoc($query)) { |
| 56 | $sid = (int)$worker["id"]; |
| 57 | $sday = (int)$worker["lastupdated"]; |
| 58 | $sstatus = (int)($worker["hidden"] == "1" ? (workers::AFFILIATION_STATUS_AUTO_NOTWORKING) : (workers::AFFILIATION_STATUS_AUTO_WORKING)); |
| 59 | $sql = "INSERT INTO workhistory (worker, day, status) VALUES ($sid, $sday, $sstatus)"; |
| 60 | if (!mysqli_query($con, $sql)) { |
| 61 | echo "[error] Failed to upgrade ".$worker["id"]."\n"; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | echo "[info] Removing the 'hidden' and 'lastupdated' columns from the 'workers' table, and the 'baixa' column from the 'people' table...\n"; |
| 66 | if (!mysqli_query($con, "ALTER TABLE workers DROP hidden")) echo "[error] Failed to remove 'hidden' column from 'workers' table.\n"; |
| 67 | if (!mysqli_query($con, "ALTER TABLE workers DROP lastupdated")) echo "[error] Failed to remove 'lastupdated' column from 'workers' table.\n"; |
| 68 | if (!mysqli_query($con, "ALTER TABLE people DROP baixa")) echo "[error] Failed to remove 'baixa' column from 'people' table.\n"; |
| 69 | |
| 70 | echo "[info] Done\n"; |