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

M180222000000WorkerPing   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 40
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A safeDown() 0 4 1
B safeUp() 0 25 3
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