Passed
Push — master ( 1d1c44...e0e6c6 )
by Roman
02:44
created

M180222000000WorkerPing::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
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\Migration;
11
use yii\db\Query;
12
use zhuravljov\yii\queue\monitor\Env;
13
14
/**
15
 * Storage of worker events
16
 *
17
 * @author Roman Zhuravlev <[email protected]>
18
 */
19
class M180222000000WorkerPing extends Migration
20
{
21
    /**
22
     * @var Env
23
     */
24
    protected $env;
25
26
    /**
27
     * @param Env $env
28
     * @inheritdoc
29
     */
30
    public function __construct(Env $env, $config = [])
31
    {
32
        $this->env = $env;
33
        parent::__construct($config);
34
    }
35
36
    /**
37
     * @inheritdoc
38
     */
39
    public function up()
40
    {
41
        $this->addColumn(
42
            $this->env->workerTableName,
43
            'pinged_at',
44
            $this->integer()->notNull()->after('started_at')
45
        );
46
47
        $query = (new Query())->from($this->env->workerTableName);
48
        foreach ($query->all() as $row) {
49
            $this->update(
50
                $this->env->workerTableName,
51
                ['pinged_at' => $row['finished_at'] ?: time()],
52
                ['id' => $row['id']]
53
            );
54
        }
55
56
        $this->addColumn(
57
            $this->env->workerTableName,
58
            'stopped_at',
59
            $this->integer()->after('pinged_at')
60
        );
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public function down()
67
    {
68
        $this->dropColumn($this->env->workerTableName, 'stopped_at');
69
        $this->dropColumn($this->env->workerTableName, 'pinged_at');
70
    }
71
}
72