From 59da273da287706faa0797552d3a6c92ab169475 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Fri, 6 Sep 2024 14:56:21 +1200 Subject: [PATCH] API Update API to reflect changes to CLI interaction --- code/Task/UserFormsColumnCleanTask.php | 28 ++++++++++++++++---------- docs/en/03_troubleshooting.md | 2 +- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/code/Task/UserFormsColumnCleanTask.php b/code/Task/UserFormsColumnCleanTask.php index e62a086b..79b03a7a 100644 --- a/code/Task/UserFormsColumnCleanTask.php +++ b/code/Task/UserFormsColumnCleanTask.php @@ -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 @@ -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]; @@ -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(); @@ -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
"; - echo "Creating Backup_$db
"; + $output->writeln("Backing up $db
"); + $output->writeln("Creating Backup_$db
"); // backup table $query = "CREATE TABLE Backup_$db LIKE $db"; DB::query($query); - echo "Populating Backup_$db
"; + $output->writeln("Populating Backup_$db
"); $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
"; + $output->writeln("Dropping $column from $db
"); $query = "ALTER TABLE $db DROP COLUMN $column"; DB::query($query); } } } + return Command::SUCCESS; } } diff --git a/docs/en/03_troubleshooting.md b/docs/en/03_troubleshooting.md index ea83de84..19d86ae7 100644 --- a/docs/en/03_troubleshooting.md +++ b/docs/en/03_troubleshooting.md @@ -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