FIX Ensure UserFormsColumnCleanTask runs safely (#1335)

This commit is contained in:
Guy Sartorelli 2024-10-15 14:54:37 +13:00 committed by GitHub
parent f5cfc22aba
commit fadfec264b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -33,32 +33,32 @@ class UserFormsColumnCleanTask extends MigrationTask
$schema = DataObject::getSchema(); $schema = DataObject::getSchema();
foreach ($this->tables as $db) { foreach ($this->tables as $db) {
$table = $schema->tableName($db);
$columns = $schema->databaseFields($db); $columns = $schema->databaseFields($db);
$query = "SHOW COLUMNS FROM $db"; $query = "SHOW COLUMNS FROM $table";
$liveColumns = DB::query($query)->column(); $liveColumns = DB::query($query)->column();
$backedUp = 0; $query = "SHOW TABLES LIKE 'Backup_$table'";
$query = "SHOW TABLES LIKE 'Backup_$db'";
$tableExists = DB::query($query)->value(); $tableExists = DB::query($query)->value();
if ($tableExists != null) { if ($tableExists != null) {
echo "Tasks run already on $db exiting"; echo "Tasks run already on $table exiting";
return; return;
} }
$backedUp = 0; $backedUp = false;
foreach ($liveColumns as $index => $column) { foreach ($liveColumns as $column) {
if ($backedUp == 0) { if (!$backedUp) {
echo "Backing up $db <br />"; echo "Backing up $table <br />";
echo "Creating Backup_$db <br />"; echo "Creating Backup_$table <br />";
// backup table // backup table
$query = "CREATE TABLE Backup_$db LIKE $db"; $query = "CREATE TABLE Backup_$table LIKE $table";
DB::query($query); DB::query($query);
echo "Populating Backup_$db <br />"; echo "Populating Backup_$table <br />";
$query = "INSERT Backup_$db SELECT * FROM $db"; $query = "INSERT Backup_$table SELECT * FROM $table";
DB::query($query); DB::query($query);
$backedUp = 1; $backedUp = true;
} }
if (!isset($columns[$column]) && !in_array($column, $this->keepColumns ?? [])) { if (!isset($columns[$column]) && !in_array($column, $this->keepColumns ?? [])) {
echo "Dropping $column from $db <br />"; echo "Dropping $column from $table <br />";
$query = "ALTER TABLE $db DROP COLUMN $column"; $query = "ALTER TABLE $table DROP COLUMN $column";
DB::query($query); DB::query($query);
} }
} }