Passed
Push — master ( e0e6c6...849d03 )
by Roman
02:16
created

M180222000000WorkerPing::safeUp()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 18
nc 2
nop 0
1
<?php
2
/**
3
 * @link https://github.com/zhuravljov/yii2-queue-monitor
4
 * @copyright Copyright (c) 2017 Roman Zhuravlev
5
 * @license http://opensource.org/licenses/BSD-3-Clause
6
 */
7
8
namespace zhuravljov\yii\queue\monitor\migrations;
9
10
use yii\db\Query;
11
use zhuravljov\yii\queue\monitor\base\Migration;
12
13
/**
14
 * Storage of worker events
15
 *
16
 * @author Roman Zhuravlev <[email protected]>
17
 */
18
class M180222000000WorkerPing extends Migration
19
{
20
    /**
21
     * @inheritdoc
22
     */
23
    public function safeUp()
24
    {
25
        $this->addColumn(
26
            $this->env->workerTableName,
27
            'pinged_at',
28
            $this->integer()->notNull()->after('started_at')
29
        );
30
31
        $time = $this->beginCommand("update {$this->env->workerTableName}");
32
        $this->compact = true;
33
        $query = (new Query())->from($this->env->workerTableName);
34
        foreach ($query->all($this->db) as $row) {
35
            $this->update(
36
                $this->env->workerTableName,
37
                ['pinged_at' => $row['finished_at'] ?: time()],
38
                ['id' => $row['id']]
39
            );
40
        }
41
        $this->compact = false;
42
        $this->endCommand($time);
43
44
        $this->addColumn(
45
            $this->env->workerTableName,
46
            'stopped_at',
47
            $this->integer()->after('pinged_at')
48
        );
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public function safeDown()
55
    {
56
        $this->dropColumn($this->env->workerTableName, 'stopped_at');
57
        $this->dropColumn($this->env->workerTableName, 'pinged_at');
58
    }
59
}
60