Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Ensure UserFormsColumnCleanTask runs safely #1335

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions code/Task/UserFormsColumnCleanTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <br />";
echo "Creating Backup_$db <br />";
$backedUp = false;
foreach ($liveColumns as $column) {
if (!$backedUp) {
echo "Backing up $table <br />";
echo "Creating Backup_$table <br />";
// backup table
$query = "CREATE TABLE Backup_$db LIKE $db";
$query = "CREATE TABLE Backup_$table LIKE $table";
DB::query($query);
echo "Populating Backup_$db <br />";
$query = "INSERT Backup_$db SELECT * FROM $db";
echo "Populating Backup_$table <br />";
$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 <br />";
$query = "ALTER TABLE $db DROP COLUMN $column";
echo "Dropping $column from $table <br />";
$query = "ALTER TABLE $table DROP COLUMN $column";
DB::query($query);
}
}
Expand Down
Loading