2013-05-25 06:03:59 +02:00
|
|
|
<?php
|
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
namespace SilverStripe\UserForms\Task;
|
|
|
|
|
|
|
|
use SilverStripe\Dev\MigrationTask;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\DB;
|
2017-08-11 02:36:28 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2013-05-25 06:03:59 +02:00
|
|
|
/**
|
|
|
|
* UserForms Column Clean Task
|
|
|
|
*
|
|
|
|
* Column clean up tasks for Userforms
|
|
|
|
*
|
|
|
|
* @package userforms
|
|
|
|
*/
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
class UserFormsColumnCleanTask extends MigrationTask
|
|
|
|
{
|
2017-08-11 01:33:06 +02:00
|
|
|
protected $title = 'UserForms EditableFormField Column Clean task';
|
2016-07-21 07:53:59 +02:00
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
protected $description = 'Removes unused columns from EditableFormField for MySQL databases;';
|
2016-07-21 07:53:59 +02:00
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
protected $tables = [EditableFormField::class];
|
2016-07-21 07:53:59 +02:00
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
protected $keepColumns = ['ID'];
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Publish the existing forms.
|
|
|
|
*/
|
|
|
|
public function run($request)
|
|
|
|
{
|
2017-08-11 01:33:06 +02:00
|
|
|
$schema = DataObject::getSchema();
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
foreach ($this->tables as $db) {
|
2024-10-15 03:54:37 +02:00
|
|
|
$table = $schema->tableName($db);
|
2017-08-11 01:33:06 +02:00
|
|
|
$columns = $schema->databaseFields($db);
|
2024-10-15 03:54:37 +02:00
|
|
|
$query = "SHOW COLUMNS FROM $table";
|
2016-07-21 07:53:59 +02:00
|
|
|
$liveColumns = DB::query($query)->column();
|
2024-10-15 03:54:37 +02:00
|
|
|
$query = "SHOW TABLES LIKE 'Backup_$table'";
|
2016-07-21 07:53:59 +02:00
|
|
|
$tableExists = DB::query($query)->value();
|
|
|
|
if ($tableExists != null) {
|
2024-10-15 03:54:37 +02:00
|
|
|
echo "Tasks run already on $table exiting";
|
2016-07-21 07:53:59 +02:00
|
|
|
return;
|
|
|
|
}
|
2024-10-15 03:54:37 +02:00
|
|
|
$backedUp = false;
|
|
|
|
foreach ($liveColumns as $column) {
|
|
|
|
if (!$backedUp) {
|
|
|
|
echo "Backing up $table <br />";
|
|
|
|
echo "Creating Backup_$table <br />";
|
2016-07-21 07:53:59 +02:00
|
|
|
// backup table
|
2024-10-15 03:54:37 +02:00
|
|
|
$query = "CREATE TABLE Backup_$table LIKE $table";
|
2016-07-21 07:53:59 +02:00
|
|
|
DB::query($query);
|
2024-10-15 03:54:37 +02:00
|
|
|
echo "Populating Backup_$table <br />";
|
|
|
|
$query = "INSERT Backup_$table SELECT * FROM $table";
|
2016-07-21 07:53:59 +02:00
|
|
|
DB::query($query);
|
2024-10-15 03:54:37 +02:00
|
|
|
$backedUp = true;
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
2022-04-13 03:52:56 +02:00
|
|
|
if (!isset($columns[$column]) && !in_array($column, $this->keepColumns ?? [])) {
|
2024-10-15 03:54:37 +02:00
|
|
|
echo "Dropping $column from $table <br />";
|
|
|
|
$query = "ALTER TABLE $table DROP COLUMN $column";
|
2016-07-21 07:53:59 +02:00
|
|
|
DB::query($query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-25 06:03:59 +02:00
|
|
|
}
|