Completed
Pull Request — master (#647)
by Robbie
02:09
created

UserFormsColumnCleanTask   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 51
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C run() 0 37 7
1
<?php
2
3
namespace SilverStripe\UserForms\Task;
4
5
use SilverStripe\Dev\MigrationTask;
6
use SilverStripe\ORM\DataObject;
7
use SilverStripe\ORM\DB;
8
use SilverStripe\UserForms\Model\EditableFormField;
9
10
/**
11
 * UserForms Column Clean Task
12
 *
13
 * Column clean up tasks for Userforms
14
 *
15
 * @package userforms
16
 */
17
18
class UserFormsColumnCleanTask extends MigrationTask
19
{
20
    protected $title = 'UserForms EditableFormField Column Clean task';
21
22
    protected $description = 'Removes unused columns from EditableFormField for MySQL databases;';
23
24
    protected $tables = [EditableFormField::class];
25
26
    protected $keepColumns = ['ID'];
27
28
    /**
29
     * Publish the existing forms.
30
     */
31
    public function run($request)
32
    {
33
        /** @var \SilverStripe\ORM\DataObjectSchema $schema */
34
        $schema = DataObject::getSchema();
35
36
        foreach ($this->tables as $db) {
37
            $columns = $schema->databaseFields($db);
38
            $query = "SHOW COLUMNS FROM $db";
39
            $liveColumns = DB::query($query)->column();
40
            $backedUp = 0;
0 ignored issues
show
Unused Code introduced by
$backedUp is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
41
            $query = "SHOW TABLES LIKE 'Backup_$db'";
42
            $tableExists = DB::query($query)->value();
43
            if ($tableExists != null) {
44
                echo "Tasks run already on $db exiting";
45
                return;
46
            }
47
            $backedUp = 0;
48
            foreach ($liveColumns as $index => $column) {
49
                if ($backedUp == 0) {
50
                    echo "Backing up $db <br />";
51
                    echo "Creating Backup_$db <br />";
52
                    // backup table
53
                    $query = "CREATE TABLE Backup_$db LIKE $db";
54
                    DB::query($query);
55
                    echo "Populating Backup_$db <br />";
56
                    $query = "INSERT Backup_$db SELECT * FROM $db";
57
                    DB::query($query);
58
                    $backedUp = 1;
59
                }
60
                if (!isset($columns[$column]) && !in_array($column, $this->keepColumns)) {
61
                    echo "Dropping $column from $db <br />";
62
                    $query = "ALTER TABLE $db DROP COLUMN $column";
63
                    DB::query($query);
64
                }
65
            }
66
        }
67
    }
68
}
69