Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 1 | <?php |
| 2 | // @description: Program used to upgrade the database after the implementation of issue #23. |
| 3 | |
| 4 | require_once(__DIR__."/../core.php"); |
| 5 | |
| 6 | if (php_sapi_name() != "cli") { |
| 7 | security::notFound(); |
| 8 | exit(); |
| 9 | } |
| 10 | |
| 11 | echo "=========================\n"; |
| 12 | echo "upgradebaixes2history.php\n"; |
| 13 | echo "=========================\n\n"; |
| 14 | |
| 15 | // Check whether the database was already upgraded |
| 16 | $prequery = mysqli_query($con, "SHOW COLUMNS FROM workers LIKE 'hidden'"); |
| 17 | if (!mysqli_num_rows($prequery)) { |
| 18 | die("[fatal error] The database was already upgraded.\n"); |
| 19 | } |
| 20 | |
| 21 | echo "[info] Adding new database schema...\n"; |
| 22 | if (!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 | |
| 31 | echo "[info] Transfering affiliation information from the workers table to the workhistory table...\n"; |
| 32 | $query = mysqli_query($con, "SELECT * FROM workers"); |
| 33 | |
| 34 | if ($query === false) exit(); |
| 35 | |
| 36 | while ($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 | |
| 46 | echo "[info] Removing the 'hidden' and 'lastupdated' columns from the 'workers' table, and the 'baixa' column from the 'people' table...\n"; |
| 47 | if (!mysqli_query($con, "ALTER TABLE workers DROP hidden")) echo "[error] Failed to remove 'hidden' column from 'workers' table.\n"; |
| 48 | if (!mysqli_query($con, "ALTER TABLE workers DROP lastupdated")) echo "[error] Failed to remove 'lastupdated' column from 'workers' table.\n"; |
| 49 | if (!mysqli_query($con, "ALTER TABLE people DROP baixa")) echo "[error] Failed to remove 'baixa' column from 'people' table.\n"; |
| 50 | |
| 51 | echo "[info] Done\n"; |