<?php include "../patches.php"; $patch = new Patch(20); if (!$patch->exists()) { $emptySql = "SELECT count(id) FROM s3_files WHERE add_date='0000-00-00 00:00:00'"; $originalCount = db()->getValue($emptySql); $patch->log("Starting with {$originalCount} files with invalid dates."); // Fix known images $sql = "UPDATE s3_files, webcam_images\n\t\t\tSET s3_files.add_date=webcam_images.timestamp\n\t\t\tWHERE s3_files.id = webcam_images.image_id\n\t\t\tAND s3_files.add_date='0000-00-00 00:00:00'"; $currentCount = fix_timestamp($sql, "Known image timestamps", $originalCount); // Fix gcode files uploaded directly $sql = "UPDATE s3_files, jobs\n\t\t\tSET s3_files.add_date=jobs.created_time\n\t\t\tWHERE jobs.source_file_id=s3_files.id\n\t\t\tAND s3_files.add_date='0000-00-00 00:00:00'"; $currentCount = fix_timestamp($sql, "Known gcode files", $currentCount); // Fix gcode files made by a slicer $sql = "UPDATE s3_files, jobs, slice_jobs\n\t\t\tSET s3_files.add_date=slice_jobs.finish_date\n\t\t\tWHERE jobs.id=slice_jobs.job_id\n\t\t\tAND slice_jobs.input_id=jobs.source_file_id\n\t\t\tAND slice_jobs.output_id=jobs.file_id\n AND s3_files.id=jobs.file_id\n\t\t\tAND slice_jobs.finish_date!='0000-00-00 00:00:00'\n\t\t\tAND s3_files.add_date='0000-00-00 00:00:00'"; $currentCount = fix_timestamp($sql, "Sliced gcode files", $currentCount); // Fix files if a child has a timestamp, but the parent doesn't. $sql = "UPDATE s3_files a, s3_files b\n\t\t\tSET a.add_date=b.add_date\n\t\t\tWHERE a.id=b.parent_id\n\t\t\tAND b.parent_id!=0\n\t\t\tAND b.add_date!='0000-00-00 00:00:00'\n\t\t\tAND a.add_date='0000-00-00 00:00:00'"; $currentCount = fix_timestamp($sql, "Fixing parent files", $currentCount); // Fix files if a parent has a timestamp, but the child doesn't. $sql = "UPDATE s3_files a, s3_files b\n\t\t\tSET a.add_date=b.add_date\n\t\t\tWHERE a.parent_id=b.id\n\t\t\tAND a.parent_id!=0\n\t\t\tAND b.add_date!='0000-00-00 00:00:00'\n\t\t\tAND a.add_date='0000-00-00 00:00:00'"; $currentCount = fix_timestamp($sql, "Fixing child files", $currentCount); $patch->log("Total: " . ($originalCount - $currentCount) . " fixed, {$currentCount} remaining"); $patch->finish("Fixing bad timestamps for s3_files"); } function fix_timestamp($sql, $method, $lastCount) { global $patch, $emptySql; db()->execute($sql); $currentCount = db()->getValue($emptySql);
$user_id = $job->getUser()->id; $rowSql = "('" . date("Y-m-d H:i:s", $timestamp) . "', "; $rowSql .= "{$image_id}, {$user_id}, {$job->id}, "; $bot = $job->getBot(); if ($bot->isHydrated()) { $rowSql .= "{$bot->id}"; } else { $rowSql .= "NULL"; } $rowSql .= ")"; $rowData[] = $rowSql; } else { $failCount++; } } else { // Remove it from the array to save memory unset($existingImages[$image_id]); } } if (count($rowData) > 0) { db()->execute("INSERT IGNORE INTO webcam_images(`timestamp`, `image_id`, `user_id`, `job_id`, `bot_id`) VALUES " . implode(",", $rowData)); } $count++; $patch->progress($count * 100 / $total); } } if ($failCount > 0) { $patch->log($failCount . " images no longer exist in the database"); } $patch->finish("Added webcam images table"); }
<?php include "../patches.php"; $patch = new Patch(22); if (!$patch->exists()) { $sql = "UPDATE jobs set user_sort=0 WHERE status IN (\n\t\t\t'complete',\n\t\t\t'failure',\n\t\t\t'canceled'\n\t\t)"; $jobsAffected = db()->execute($sql); $patch->log("{$jobsAffected} jobs cleaned"); $patch->finish("Cleaning up user_sort for jobs"); }