Skip to content

Commit

Permalink
API Update API to reflect changes to CLI interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Sep 8, 2024
1 parent 3792144 commit 59da273
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
28 changes: 17 additions & 11 deletions code/Task/UserFormsColumnCleanTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace SilverStripe\UserForms\Task;

use SilverStripe\Dev\MigrationTask;
use SilverStripe\Dev\BuildTask;
use SilverStripe\HybridExecution\HybridOutput;
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
Expand All @@ -15,11 +18,13 @@
* @package userforms
*/

class UserFormsColumnCleanTask extends MigrationTask
class UserFormsColumnCleanTask extends BuildTask
{
protected $title = 'UserForms EditableFormField Column Clean task';
protected static string $commandName = 'userforms-column-clean';

protected $description = 'Removes unused columns from EditableFormField for MySQL databases;';
protected string $title = 'UserForms EditableFormField Column Clean task';

protected static string $description = 'Removes unused columns from EditableFormField for MySQL databases;';

protected $tables = [EditableFormField::class];

Expand All @@ -28,7 +33,7 @@ class UserFormsColumnCleanTask extends MigrationTask
/**
* Publish the existing forms.
*/
public function run($request)
protected function execute(InputInterface $input, HybridOutput $output): int
{
$schema = DataObject::getSchema();

Expand All @@ -40,28 +45,29 @@ public function run($request)
$query = "SHOW TABLES LIKE 'Backup_$db'";
$tableExists = DB::query($query)->value();
if ($tableExists != null) {
echo "Tasks run already on $db exiting";
return;
$output->writeln("Tasks run already on $db exiting");
return Command::SUCCESS;
}
$backedUp = 0;
foreach ($liveColumns as $index => $column) {
if ($backedUp == 0) {
echo "Backing up $db <br />";
echo "Creating Backup_$db <br />";
$output->writeln("Backing up $db <br />");
$output->writeln("Creating Backup_$db <br />");
// backup table
$query = "CREATE TABLE Backup_$db LIKE $db";
DB::query($query);
echo "Populating Backup_$db <br />";
$output->writeln("Populating Backup_$db <br />");
$query = "INSERT Backup_$db SELECT * FROM $db";
DB::query($query);
$backedUp = 1;
}
if (!isset($columns[$column]) && !in_array($column, $this->keepColumns ?? [])) {
echo "Dropping $column from $db <br />";
$output->writeln("Dropping $column from $db <br />");
$query = "ALTER TABLE $db DROP COLUMN $column";
DB::query($query);
}
}
}
return Command::SUCCESS;
}
}
2 changes: 1 addition & 1 deletion docs/en/03_troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Currently it only supports MySQL and when it is run it queries the EditableFormF
it then grabs the columns for the live database. It will create a backup of the table and then remove any columns that
are surplus.

To run the task, log in as an administrator and go to `https://example.com/dev/tasks/UserFormsColumnCleanTask` in your browser, or run `sake dev/tasks/UserFormsColumnCleanTask` from the command line.
To run the task, log in as an administrator and go to `https://example.com/dev/tasks/userforms-column-clean` in your browser, or run `sake tasks:userforms-column-clean` from the command line.

## My CSV export times out or runs out of memory

Expand Down

0 comments on commit 59da273

Please sign in to comment.