blob: f06c01bb6bdc8b091f654cfdd10b4fbe6968065b [file] [log] [blame]
Copybara botbe50d492023-11-30 00:16:42 +01001<?php
2// @description: Program used to upgrade the database after the implementation of issue #23.
3
4require_once(__DIR__."/../core.php");
5
6if (php_sapi_name() != "cli") {
7 security::notFound();
8 exit();
9}
10
11echo "=========================\n";
12echo "upgradebaixes2history.php\n";
13echo "=========================\n\n";
14
15// Check whether the database was already upgraded
16$prequery = mysqli_query($con, "SHOW COLUMNS FROM workers LIKE 'hidden'");
17if (!mysqli_num_rows($prequery)) {
18 die("[fatal error] The database was already upgraded.\n");
19}
20
21echo "[info] Adding new database schema...\n";
22if (!mysqli_query($con, "CREATE TABLE workhistory (
23 id INT NOT NULL AUTO_INCREMENT,
24 PRIMARY KEY(id),
25 worker INT NOT NULL,
26 INDEX(worker),
27 day INT NOT NULL,
28 status INT NOT NULL
29)")) die("[fatal error] Couldn't add new database.\n");
30
31echo "[info] Transfering affiliation information from the workers table to the workhistory table...\n";
32$query = mysqli_query($con, "SELECT * FROM workers");
33
34if ($query === false) exit();
35
36while ($worker = mysqli_fetch_assoc($query)) {
37 $sid = (int)$worker["id"];
38 $sday = (int)$worker["lastupdated"];
39 $sstatus = (int)($worker["hidden"] == "1" ? (workers::AFFILIATION_STATUS_AUTO_NOTWORKING) : (workers::AFFILIATION_STATUS_AUTO_WORKING));
40 $sql = "INSERT INTO workhistory (worker, day, status) VALUES ($sid, $sday, $sstatus)";
41 if (!mysqli_query($con, $sql)) {
42 echo "[error] Failed to upgrade ".$worker["id"]."\n";
43 }
44}
45
46echo "[info] Removing the 'hidden' and 'lastupdated' columns from the 'workers' table, and the 'baixa' column from the 'people' table...\n";
47if (!mysqli_query($con, "ALTER TABLE workers DROP hidden")) echo "[error] Failed to remove 'hidden' column from 'workers' table.\n";
48if (!mysqli_query($con, "ALTER TABLE workers DROP lastupdated")) echo "[error] Failed to remove 'lastupdated' column from 'workers' table.\n";
49if (!mysqli_query($con, "ALTER TABLE people DROP baixa")) echo "[error] Failed to remove 'baixa' column from 'people' table.\n";
50
51echo "[info] Done\n";