blob: b1e22680d38fe90d2f07f3eb816c8f4273078495 [file] [log] [blame]
Copybara botbe50d492023-11-30 00:16:42 +01001<?php
Adrià Vilanova Martínez5af86512023-12-02 20:44:16 +01002/*
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 botbe50d492023-11-30 00:16:42 +010021// @description: Program used to upgrade the database after the implementation of issue #23.
22
23require_once(__DIR__."/../core.php");
24
25if (php_sapi_name() != "cli") {
26 security::notFound();
27 exit();
28}
29
30echo "=========================\n";
31echo "upgradebaixes2history.php\n";
32echo "=========================\n\n";
33
34// Check whether the database was already upgraded
35$prequery = mysqli_query($con, "SHOW COLUMNS FROM workers LIKE 'hidden'");
36if (!mysqli_num_rows($prequery)) {
37 die("[fatal error] The database was already upgraded.\n");
38}
39
40echo "[info] Adding new database schema...\n";
41if (!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
50echo "[info] Transfering affiliation information from the workers table to the workhistory table...\n";
51$query = mysqli_query($con, "SELECT * FROM workers");
52
53if ($query === false) exit();
54
55while ($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
65echo "[info] Removing the 'hidden' and 'lastupdated' columns from the 'workers' table, and the 'baixa' column from the 'people' table...\n";
66if (!mysqli_query($con, "ALTER TABLE workers DROP hidden")) echo "[error] Failed to remove 'hidden' column from 'workers' table.\n";
67if (!mysqli_query($con, "ALTER TABLE workers DROP lastupdated")) echo "[error] Failed to remove 'lastupdated' column from 'workers' table.\n";
68if (!mysqli_query($con, "ALTER TABLE people DROP baixa")) echo "[error] Failed to remove 'baixa' column from 'people' table.\n";
69
70echo "[info] Done\n";