Completed
Push — master ( 82c4a1...4bf0a8 )
by Robbie
10:38
created

src/Tasks/MigrateCommentParentsTask.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
The property $segment is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can 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
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