Issues (29)

src/Commands/ClearTableCommand.php (6 issues)

1
<?php
2
3
/**
4
 * Created by PhpStorm.
5
 * User: hugh.li
6
 * Date: 2021/2/22
7
 * Time: 11:18.
8
 */
9
10
namespace HughCube\Laravel\OTS\Commands;
11
12
use Aliyun\OTS\Consts\DirectionConst;
13
use Aliyun\OTS\Consts\OperationTypeConst;
14
use Aliyun\OTS\Consts\PrimaryKeyTypeConst;
15
use Aliyun\OTS\Consts\RowExistenceExpectationConst;
16
use Aliyun\OTS\OTSClientException;
17
use Aliyun\OTS\OTSServerException;
18
use Carbon\Carbon;
19
use Exception;
20
use HughCube\Laravel\OTS\Connection;
21
use HughCube\Laravel\OTS\Ots;
22
use Illuminate\Console\Command;
23
use Illuminate\Console\Scheduling\Schedule;
24
use Illuminate\Support\Arr;
25
26
class ClearTableCommand extends Command
27
{
28
    /**
29
     * @inheritdoc
30
     */
31
    protected $signature = 'ots:clear-table
32
                            {--ots=ots : ots connection. }
33
                            {--name= : ots table name. }';
34
35
    /**
36
     * @inheritdoc
37
     */
38
    protected $description = 'Clear the ots table data.';
39
40
    /**
41
     * @param Schedule $schedule
42
     *
43
     * @throws OTSClientException
44
     * @throws OTSServerException
45
     *
46
     * @return void
47
     */
48
    public function handle(Schedule $schedule)
0 ignored issues
show
The parameter $schedule is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

48
    public function handle(/** @scrutinizer ignore-unused */ Schedule $schedule)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
    {
50
        $tableName = $this->getTable();
51
52
        try {
53
            $table = $this->getOts()->describeTable(['table_name' => $tableName]);
0 ignored issues
show
The method describeTable() does not exist on HughCube\Laravel\OTS\Connection. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
            $table = $this->getOts()->/** @scrutinizer ignore-call */ describeTable(['table_name' => $tableName]);
Loading history...
54
        } catch (OTSServerException $exception) {
55
            $this->error(sprintf('RequestId: %s', $exception->getRequestId()));
56
            $this->error(sprintf('%s: %s', $exception->getOTSErrorCode(), $exception->getOTSErrorMessage()));
57
58
            return;
59
        }
60
61
        if (!$this->confirm(sprintf('Are you sure you want to clear the "%s" table?', $tableName))) {
62
            return;
63
        }
64
65
        /** @var array $pks 表主键 */
66
        $pks = Arr::get($table, 'table_meta.primary_key_schema');
67
68
        /** 起始主键 */
69
        list($startPk, $endPk) = $this->parseRangePk($pks);
70
71
        $rowCount = 0;
72
        while (!empty($startPk)) {
73
            $request = [
74
                'table_name'                  => $tableName, 'max_versions' => 1,
75
                'direction'                   => DirectionConst::CONST_FORWARD,
76
                'inclusive_start_primary_key' => $startPk,
77
                'exclusive_end_primary_key'   => $endPk,
78
                'limit'                       => 200,
79
            ];
80
            $response = $this->getOts()->getRange($request);
0 ignored issues
show
The method getRange() does not exist on HughCube\Laravel\OTS\Connection. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
            $response = $this->getOts()->/** @scrutinizer ignore-call */ getRange($request);
Loading history...
81
82
            /** 删除查询出来的数据 */
83
            if (!empty($rows = $this->parseDeleteRows($response))) {
84
                $this->getOts()->batchWriteRow([
0 ignored issues
show
The method batchWriteRow() does not exist on HughCube\Laravel\OTS\Connection. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
                $this->getOts()->/** @scrutinizer ignore-call */ batchWriteRow([
Loading history...
85
                    'tables' => [['table_name' => $tableName, 'rows' => $rows]],
86
                ]);
87
            }
88
89
            /** 下一次轮询的key */
90
            $startPk = $response['next_start_primary_key'];
91
92
            $rowCount += count($rows);
93
            $this->comment(sprintf(
94
                '%s Delete %s rows from the "%s" table.',
95
                Carbon::now()->format('Y-m-d H:i:s.u'),
96
                count($rows),
97
                $tableName
98
            ));
99
        }
100
101
        $this->info(sprintf(
102
            '%s The "%s" table has been cleared and %s data items have been deleted.',
103
            Carbon::now()->format('Y-m-d H:i:s.u'),
104
            $tableName,
105
            $rowCount
106
        ));
107
    }
108
109
    protected function parseRangePk($pks): array
110
    {
111
        /** 起始主键 */
112
        $endPk = $startPk = [];
113
        foreach ($pks as $pk) {
114
            $startPk[] = [$pk[0], null, PrimaryKeyTypeConst::CONST_INF_MIN];
115
            $endPk[] = [$pk[0], null, PrimaryKeyTypeConst::CONST_INF_MAX];
116
        }
117
118
        return [$startPk, $endPk];
119
    }
120
121
    protected function parseDeleteRows($response): array
122
    {
123
        $rows = [];
124
        foreach ($response['rows'] as $row) {
125
            $rows[] = [
126
                'operation_type' => OperationTypeConst::CONST_DELETE,
127
                'condition'      => RowExistenceExpectationConst::CONST_IGNORE,
128
                'primary_key'    => $row['primary_key'],
129
            ];
130
        }
131
132
        return $rows;
133
    }
134
135
    /**
136
     * @throws Exception
137
     *
138
     * @return Connection
139
     */
140
    public function getOts(): Connection
141
    {
142
        return Ots::connection($this->option('ots'));
143
    }
144
145
    /**
146
     * @throws OTSClientException
147
     * @throws OTSServerException
148
     *
149
     * @return string
150
     */
151
    protected function getTable(): string
152
    {
153
        $name = $this->option('name');
154
        if (!empty($name)) {
155
            return $name;
156
        }
157
158
        $tables = $this->getOts()->listTable([]);
0 ignored issues
show
The method listTable() does not exist on HughCube\Laravel\OTS\Connection. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

158
        $tables = $this->getOts()->/** @scrutinizer ignore-call */ listTable([]);
Loading history...
159
160
        return $this->choice('You want to erase the data from that table?', $tables);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->choice('Yo... that table?', $tables) could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
161
    }
162
}
163