silverstripe-userforms/code/Task/UserFormsColumnCleanTask.php

74 lines
2.4 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\UserForms\Task;
use SilverStripe\Dev\BuildTask;
use SilverStripe\PolyExecution\PolyOutput;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB;
use SilverStripe\UserForms\Model\EditableFormField;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
/**
* UserForms Column Clean Task
*
* Column clean up tasks for Userforms
*
* @package userforms
*/
class UserFormsColumnCleanTask extends BuildTask
2016-07-21 07:53:59 +02:00
{
protected static string $commandName = 'userforms-column-clean';
2016-07-21 07:53:59 +02:00
protected string $title = 'UserForms EditableFormField Column Clean task';
protected static string $description = 'Removes unused columns from EditableFormField for MySQL databases;';
2016-07-21 07:53:59 +02:00
protected $tables = [EditableFormField::class];
2016-07-21 07:53:59 +02:00
protected $keepColumns = ['ID'];
2016-07-21 07:53:59 +02:00
/**
* Publish the existing forms.
*/
protected function execute(InputInterface $input, PolyOutput $output): int
2016-07-21 07:53:59 +02:00
{
$schema = DataObject::getSchema();
2016-07-21 07:53:59 +02:00
foreach ($this->tables as $db) {
$columns = $schema->databaseFields($db);
2016-07-21 07:53:59 +02:00
$query = "SHOW COLUMNS FROM $db";
$liveColumns = DB::query($query)->column();
$backedUp = 0;
$query = "SHOW TABLES LIKE 'Backup_$db'";
$tableExists = DB::query($query)->value();
if ($tableExists != null) {
$output->writeln("Tasks run already on $db exiting");
return Command::SUCCESS;
2016-07-21 07:53:59 +02:00
}
$backedUp = 0;
foreach ($liveColumns as $index => $column) {
if ($backedUp == 0) {
$output->writeln("Backing up $db <br />");
$output->writeln("Creating Backup_$db <br />");
2016-07-21 07:53:59 +02:00
// backup table
$query = "CREATE TABLE Backup_$db LIKE $db";
DB::query($query);
$output->writeln("Populating Backup_$db <br />");
2016-07-21 07:53:59 +02:00
$query = "INSERT Backup_$db SELECT * FROM $db";
DB::query($query);
$backedUp = 1;
}
2022-04-13 03:52:56 +02:00
if (!isset($columns[$column]) && !in_array($column, $this->keepColumns ?? [])) {
$output->writeln("Dropping $column from $db <br />");
2016-07-21 07:53:59 +02:00
$query = "ALTER TABLE $db DROP COLUMN $column";
DB::query($query);
}
}
}
return Command::SUCCESS;
2016-07-21 07:53:59 +02:00
}
}