From fadfec264be061c96c8e97cb00d79f00dddc9083 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Tue, 15 Oct 2024 14:54:37 +1300 Subject: [PATCH] FIX Ensure UserFormsColumnCleanTask runs safely (#1335) --- code/Task/UserFormsColumnCleanTask.php | 30 +++++++++++++------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/code/Task/UserFormsColumnCleanTask.php b/code/Task/UserFormsColumnCleanTask.php index e62a086b..49bb98be 100644 --- a/code/Task/UserFormsColumnCleanTask.php +++ b/code/Task/UserFormsColumnCleanTask.php @@ -33,32 +33,32 @@ public function run($request) $schema = DataObject::getSchema(); foreach ($this->tables as $db) { + $table = $schema->tableName($db); $columns = $schema->databaseFields($db); - $query = "SHOW COLUMNS FROM $db"; + $query = "SHOW COLUMNS FROM $table"; $liveColumns = DB::query($query)->column(); - $backedUp = 0; - $query = "SHOW TABLES LIKE 'Backup_$db'"; + $query = "SHOW TABLES LIKE 'Backup_$table'"; $tableExists = DB::query($query)->value(); if ($tableExists != null) { - echo "Tasks run already on $db exiting"; + echo "Tasks run already on $table exiting"; return; } - $backedUp = 0; - foreach ($liveColumns as $index => $column) { - if ($backedUp == 0) { - echo "Backing up $db
"; - echo "Creating Backup_$db
"; + $backedUp = false; + foreach ($liveColumns as $column) { + if (!$backedUp) { + echo "Backing up $table
"; + echo "Creating Backup_$table
"; // backup table - $query = "CREATE TABLE Backup_$db LIKE $db"; + $query = "CREATE TABLE Backup_$table LIKE $table"; DB::query($query); - echo "Populating Backup_$db
"; - $query = "INSERT Backup_$db SELECT * FROM $db"; + echo "Populating Backup_$table
"; + $query = "INSERT Backup_$table SELECT * FROM $table"; DB::query($query); - $backedUp = 1; + $backedUp = true; } if (!isset($columns[$column]) && !in_array($column, $this->keepColumns ?? [])) { - echo "Dropping $column from $db
"; - $query = "ALTER TABLE $db DROP COLUMN $column"; + echo "Dropping $column from $table
"; + $query = "ALTER TABLE $table DROP COLUMN $column"; DB::query($query); } }