MigrateCommentParentsTask   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 30
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 19 3
1
<?php
2
3
namespace SilverStripe\Comments\Tasks;
4
5
use SilverStripe\Comments\Model\Comment;
6
use SilverStripe\Core\Config\Config;
7
use SilverStripe\Dev\BuildTask;
8
use SilverStripe\ORM\DB;
9
10
/**
11
 * Migrates all 3.x comment's BaseClass fields to the new ParentClass fields
12
 *
13
 * @package comments
14
 */
15
class MigrateCommentParentsTask extends BuildTask
16
{
17
    private static $segment = 'MigrateCommentParentsTask';
0 ignored issues
show
introduced by
The private property $segment is not used, and could be removed.
Loading history...
18
19
    protected $title = 'Migrate Comment Parent classes from 3.x';
20
21
    protected $description = 'Migrates all 3.x Comment BaseClass fields to the new ParentClass fields in 4.0';
22
23
    /**
24
     * @param HTTPRequest $request
0 ignored issues
show
Bug introduced by
The type SilverStripe\Comments\Tasks\HTTPRequest was not found. Did you mean HTTPRequest? If so, make sure to prefix the type with \.
Loading history...
25
     */
26
    public function run($request)
27
    {
28
        // Check if anything needs to be done
29
        $tableName = Comment::getSchema()->tableName(Comment::class);
30
        if (!DB::get_schema()->hasField($tableName, 'BaseClass')) {
31
            DB::alteration_message('"BaseClass" does not exist on "' . $tableName . '", nothing to upgrade.', 'notice');
32
            return;
33
        }
34
35
        // Set the class names to fully qualified class names first
36
        $remapping = Config::inst()->get('SilverStripe\\ORM\\DatabaseAdmin', 'classname_value_remapping');
37
        $updateQuery = "UPDATE \"Comment\" SET \"BaseClass\" = ? WHERE \"BaseClass\" = ?";
38
        foreach ($remapping as $old => $new) {
39
            DB::prepared_query($updateQuery, [$new, $old]);
40
        }
41
42
        // Move these values to ParentClass (the 4.x column name)
43
        DB::query('UPDATE "Comment" SET "ParentClass" = "BaseClass"');
44
        DB::alteration_message('Finished updating any applicable Comment class columns', 'notice');
45
    }
46
}
47