summaryrefslogtreecommitdiffstats
path: root/lib/Migration/MigrateStatusFlags.php
blob: 514b16f4aed3c04c3ef531bd5d72c181eff56e40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
 * Nextcloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author    Daniel Opitz <dev@copynpaste.de>
 * @copyright Daniel Opitz 2017
 */

namespace OCA\News\Migration;

use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Migration\IRepairStep;
use OCP\Migration\IOutput;

class MigrateStatusFlags implements IRepairStep
{

    /**
     * @var IDBConnection
     */
    private $db;

    /**
     * @var IConfig
     */
    private $config;

    /**
     * @param IDBConnection $db
     * @param IConfig       $config
     */
    public function __construct(IDBConnection $db, IConfig $config)
    {
        $this->db = $db;
        $this->config = $config;
    }

    public function getName()
    {
        return 'Migrate binary status into separate boolean fields';
    }

    public function run(IOutput $output)
    {
        $version = $this->config->getAppValue('news', 'installed_version', '0.0.0');
        if (version_compare($version, '11.0.6', '>=')) {
            return;
        }

        $sql = 'UPDATE `*PREFIX*news_items` '
            . 'SET `unread` = ((`status` & 2) = 2), '
            . '`starred` = ((`status` & 4) = 4)';
        $query = $this->db->prepare($sql);

        if (!$query->execute()) {
            throw new \Exception('Could not migrate status');
        }
    }
}