From 5c0388f34c1cff2134cc6239bbae3126f74c3048 Mon Sep 17 00:00:00 2001
From: Henry Gross-Hellsen <6283258+cowpod@users.noreply.github.com>
Date: Tue, 29 Oct 2024 00:05:19 -0700
Subject: [PATCH] Update upgrade1.3.5to1.4.0.php
lint sql column names, update messages, make sure to only update if necessary. and also update builds' loadertype.
---
functions/upgrade1.3.5to1.4.0.php | 72 ++++++++++++++++++++++++-------
1 file changed, 57 insertions(+), 15 deletions(-)
diff --git a/functions/upgrade1.3.5to1.4.0.php b/functions/upgrade1.3.5to1.4.0.php
index 6976b09..d25dc46 100644
--- a/functions/upgrade1.3.5to1.4.0.php
+++ b/functions/upgrade1.3.5to1.4.0.php
@@ -10,15 +10,15 @@
$db=new Db;
$db->connect();
-echo "
Altering table mods
";
+echo "
Altering table columns
";
// 1.4.0: mod version ranges
// naturally, we assume user is using mysql.
-$addtype1=$db->execute("ALTER TABLE `mods` ADD COLUMN `loadertype` VARCHAR(32);");
-$addtype2=$db->execute("ALTER TABLE `builds` ADD COLUMN `loadertype` VARCHAR(32);");
+$addtype1=$db->execute("ALTER TABLE mods ADD COLUMN loadertype VARCHAR(32);");
+$addtype2=$db->execute("ALTER TABLE builds ADD COLUMN loadertype VARCHAR(32);");
if (!$addtype||!$addtype) {
- die("Couldn't add new columns loadertype to table `mods`! Are we already upgraded? Click here to return to index.");
+ die("Couldn't add new columns loadertype to table mods! Are we already upgraded? Click here to return to index.");
}
mkdir('../upgrade_work');
@@ -28,7 +28,7 @@
require('modInfo.php');
$mi = new modInfo();
-$mods=$db->query("SELECT id,filename FROM `mods` WHERE type='mod'");
+$mods=$db->query("SELECT id,filename FROM mods WHERE type='mod'");
foreach ($mods as $mod) {
$zip = new ZipArchive;
@@ -58,7 +58,7 @@
$modltq = $db->query("SELECT loadertype from mods where id = ".$mod['id']);
if ($modltq) {
if (sizeof($modltq)==1 && $modltq[0]['loadertype']===$loader_type) {
- echo 'mod id='.$mod['id'].' name='.$mod['name'].' already has loadertype='.$loader_type.'. skipping.
';
+ echo 'Skipping mod id='.$mod['id'].' name='.$mod['name'].' already has loadertype='.$loader_type.'
';
continue;
}
}
@@ -66,7 +66,7 @@
$updatemodx = $db->execute("UPDATE mods SET loadertype = '".$loader_type."' where id = ".$mod['id']);
if ($updatemodx) {
- echo 'mod id='.$mod['id'].', name='.$mod['name'].' updated, loadertype='.$loader_type.".
";
+ echo 'Updated mod id='.$mod['id'].', name='.$mod['name'].', loadertype='.$loader_type.".
";
} else {
echo 'failed to update mod id='.$mod['id'].' name='.$mod['name'];
}
@@ -81,16 +81,58 @@
echo "
Updating modloader entries
";
-$forges=$db->query("SELECT id,filename FROM `mods` WHERE type='forge'");
+$forges=$db->query("SELECT id,filename,loadertype FROM mods WHERE type='forge'");
foreach ($forges as $forge) {
- if (str_starts_with($forge['filename'], 'forge-')) {
- $db->execute("UPDATE mods SET loadertype='forge' WHERE id='".$forge['id']."'");
- }
- else if (str_starts_with($forge['filename'], 'neoforge-')) {
- $db->execute("UPDATE mods SET loadertype='neoforge' WHERE id='".$forge['id']."'");
+ if (empty($forge['loadertype'])) {
+ if (str_starts_with($forge['filename'], 'forge-')) {
+ $db->execute("UPDATE mods SET loadertype='forge' WHERE id='".$forge['id']."'");
+ }
+ else if (str_starts_with($forge['filename'], 'neoforge-')) {
+ $db->execute("UPDATE mods SET loadertype='neoforge' WHERE id='".$forge['id']."'");
+ }
+ else if (str_starts_with($forge['filename'], 'fabric-')) {
+ $db->execute("UPDATE mods SET loadertype='fabric' WHERE id='".$forge['id']."'");
+ } else {
+ error_log("unknown filename ".$forge['filename']);
+ echo "Unknown filename ".$forge['filename'];
+ continue; // idk what this is
+ }
+ echo "Updated ".$forge['filename']."
";
+ } else {
+ echo "Skipping ".$forge['filename']." as it already has loadertype=".$forge['loadertype']."
";
}
- else if (str_starts_with($forge['filename'], 'fabric-')) {
- $db->execute("UPDATE mods SET loadertype='fabric' WHERE id='".$forge['id']."'");
+}
+
+echo "
Updating builds entries
";
+
+$builds=$db->query("SELECT id,name,mods,loadertype FROM builds");
+
+foreach ($builds as $build) {
+ if (empty($build['loadertype'])) {
+ // now get the mod details
+ $mod_loadertype="";
+ foreach (explode(",", $build['mods']) as $id) {
+ $mod_loadertype = $db->query("SELECT id,loadertype FROM mods WHERE id = ".$id." AND type='forge'");
+ if ($mod_loadertype===FALSE || empty($mod_loadertype) || sizeof($mod_loadertype)!==1) {
+ continue;
+ }
+ // at this point we have a forge entry
+ $mod_loadertype=$mod_loadertype[0]['loadertype'];
+ break;
+ }
+ if (!empty($mod_loadertype)) {
+ $updateres = $db->execute("UPDATE builds SET loadertype = '".$mod_loadertype."' WHERE id = ".$build['id']);
+
+ if ($updateres===TRUE) {
+ echo "Updated ".$build['name']." = '".$mod_loadertype."'
";
+ } else {
+ echo "Failed to update ".$build['name']."
";
+ }
+ } else {
+ echo "Failed to update ".$build['name']."
";
+ }
+ } else {
+ echo "Build ".$build['name']." already has loadertype=".$build['loadertype']."
";
}
}