1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
6
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
7
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
8
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
9
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
10
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
11
|
|
|
* THE SOFTWARE. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Ytake\LaravelCouchbase\Console; |
15
|
|
|
|
16
|
|
|
use Illuminate\Console\Command; |
17
|
|
|
use Illuminate\Database\DatabaseManager; |
18
|
|
|
use Symfony\Component\Console\Input\InputOption; |
19
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
20
|
|
|
use Ytake\LaravelCouchbase\Database\CouchbaseConnection; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class QueueCreatorCommand |
24
|
|
|
* |
25
|
|
|
* @codeCoverageIgnore |
26
|
|
|
* |
27
|
|
|
* @author Yuuki Takezawa<[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class QueueCreatorCommand extends Command |
30
|
|
|
{ |
31
|
|
|
/** @var string */ |
32
|
|
|
protected $name = 'couchbase:create-queue-index'; |
33
|
|
|
|
34
|
|
|
/** @var string */ |
35
|
|
|
protected $description = 'Create primary index, secondary indexes for the queue jobs couchbase bucket.'; |
36
|
|
|
|
37
|
|
|
/** @var DatabaseManager */ |
38
|
|
|
protected $databaseManager; |
39
|
|
|
|
40
|
|
|
/** @var string */ |
41
|
|
|
protected $defaultDatabase = 'couchbase'; |
42
|
|
|
|
43
|
|
|
const PRIMARY_KEY = '#job_queue_primary'; |
44
|
|
|
|
45
|
|
|
/** @var array<string, string[]> */ |
46
|
|
|
protected $secondaryIndexes = [ |
47
|
|
|
'idx_job_queue' => [ // index name |
48
|
|
|
'queue', // fields |
49
|
|
|
], |
50
|
|
|
'idx_job_identifier' => [ |
51
|
|
|
'id', |
52
|
|
|
], |
53
|
|
|
'idx_job_queue_cover' => [ |
54
|
|
|
'queue', |
55
|
|
|
'reserved_at', |
56
|
|
|
'available_at', |
57
|
|
|
'id', |
58
|
|
|
], |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* IndexFinderCommand constructor. |
63
|
|
|
* |
64
|
|
|
* @param DatabaseManager $databaseManager |
65
|
|
|
*/ |
66
|
|
|
public function __construct(DatabaseManager $databaseManager) |
67
|
|
|
{ |
68
|
|
|
$this->databaseManager = $databaseManager; |
69
|
|
|
parent::__construct(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string[] |
74
|
|
|
*/ |
75
|
|
|
protected function getArguments() |
76
|
|
|
{ |
77
|
|
|
return [ |
78
|
|
|
['bucket', InputArgument::OPTIONAL, 'Represents a bucket connection.', 'jobs'], |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the console command options. |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
protected function getOptions() |
88
|
|
|
{ |
89
|
|
|
return [ |
90
|
|
|
['database', 'db', InputOption::VALUE_REQUIRED, 'The database connection to use.', $this->defaultDatabase], |
91
|
|
|
[ |
92
|
|
|
'ignore', |
93
|
|
|
'ig', |
94
|
|
|
InputOption::VALUE_NONE, |
95
|
|
|
'if a primary index already exists, an exception will be thrown unless this is set to true.', |
96
|
|
|
], |
97
|
|
|
[ |
98
|
|
|
'defer', |
99
|
|
|
null, |
100
|
|
|
InputOption::VALUE_NONE, |
101
|
|
|
'true to defer building of the index until buildN1qlDeferredIndexes()}is called (or a direct call to the corresponding query service API)', |
102
|
|
|
], |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Execute the console command |
108
|
|
|
*/ |
109
|
|
|
public function handle() |
110
|
|
|
{ |
111
|
|
|
/** @var \Illuminate\Database\Connection|CouchbaseConnection $connection */ |
112
|
|
|
$connection = $this->databaseManager->connection($this->option('database')); |
|
|
|
|
113
|
|
|
if ($connection instanceof CouchbaseConnection) { |
114
|
|
|
$bucket = $connection->openBucket($this->argument('bucket')); |
|
|
|
|
115
|
|
|
$primary = self::PRIMARY_KEY; |
116
|
|
|
try { |
117
|
|
|
$bucket->manager()->createN1qlPrimaryIndex( |
118
|
|
|
$primary, |
119
|
|
|
$this->option('ignore'), |
120
|
|
|
$this->option('defer') |
121
|
|
|
); |
122
|
|
|
$this->info("created PRIMARY INDEX [{$primary}] for [{$this->argument('bucket')}] bucket."); |
123
|
|
|
} catch (\Exception $e) { |
124
|
|
|
$this->error($e->getMessage()); |
125
|
|
|
} |
126
|
|
|
foreach ($this->secondaryIndexes as $name => $fields) { |
127
|
|
|
try { |
128
|
|
|
$bucket->manager()->createN1qlIndex( |
129
|
|
|
$name, |
130
|
|
|
$fields, |
131
|
|
|
'', |
132
|
|
|
$this->option('ignore'), |
133
|
|
|
$this->option('defer') |
134
|
|
|
); |
135
|
|
|
$field = implode(",", $fields); |
136
|
|
|
$this->info("created SECONDARY INDEX [{$name}] fields [{$field}], for [{$this->argument('bucket')}] bucket."); |
137
|
|
|
} catch (\Exception $e) { |
138
|
|
|
$this->error($e->getMessage()); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.