Passed
Push — master ( 9987a6...c54eda )
by Paweł
03:35
created

m180429_160926_fix_public_link::safeDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 1
f 0
1
<?php
2
3
use yii\db\Migration;
4
5
/**
6
 * Class m180429_160926_fix_public_link
7
 */
8
class m180429_160926_fix_public_link extends Migration
9
{
10
    /**
11
     * {@inheritdoc}
12
     */
13
    public function safeUp()
14
    {
15
        $accountIds = (new \yii\db\Query())
16
            ->from('account')
17
            ->andWhere(['account.monitoring' => 1])
18
            ->andWhere(['account.uid' => null])
19
            ->column();
20
21
        foreach ($accountIds as $accountId) {
22
23
            do {
24
                $uid = Yii::$app->security->generateRandomString(64);
25
                $uidExist = (new \yii\db\Query())
26
                    ->from('account')
27
                    ->andWhere(['account.uid' => $uid])
28
                    ->exists();
29
            } while ($uidExist);
30
31
            $this->update('account', ['uid' => $uid], ['id' => $accountId]);
32
        }
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function safeDown()
39
    {
40
        return true;
41
    }
42
43
    /*
44
    // Use up()/down() to run migration code without a transaction.
45
    public function up()
46
    {
47
48
    }
49
50
    public function down()
51
    {
52
        echo "m180429_160926_fix_public_link cannot be reverted.\n";
53
54
        return false;
55
    }
56
    */
57
}
58