1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
5
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
6
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
7
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
8
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
9
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
10
|
|
|
* THE SOFTWARE. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Ytake\LaravelCouchbase\Console; |
14
|
|
|
|
15
|
|
|
use Illuminate\Console\Command; |
16
|
|
|
use Illuminate\Database\DatabaseManager; |
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
18
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
19
|
|
|
use Ytake\LaravelCouchbase\Database\CouchbaseConnection; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class PrimaryIndexRemoverCommand |
23
|
|
|
* |
24
|
|
|
* @author Yuuki Takezawa<[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class PrimaryIndexRemoverCommand extends Command |
27
|
|
|
{ |
28
|
|
|
/** @var string */ |
29
|
|
|
protected $name = 'couchbase:drop-primary-index'; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
protected $description = 'Drop the given primary index associated with the current bucket.'; |
33
|
|
|
|
34
|
|
|
/** @var DatabaseManager */ |
35
|
|
|
protected $databaseManager; |
36
|
|
|
|
37
|
|
|
/** @var string */ |
38
|
|
|
protected $defaultDatabase = 'couchbase'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* IndexFinderCommand constructor. |
42
|
|
|
* |
43
|
|
|
* @param DatabaseManager $databaseManager |
44
|
|
|
*/ |
45
|
1 |
|
public function __construct(DatabaseManager $databaseManager) |
46
|
|
|
{ |
47
|
1 |
|
$this->databaseManager = $databaseManager; |
48
|
1 |
|
parent::__construct(); |
49
|
1 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string[] |
53
|
|
|
*/ |
54
|
1 |
|
protected function getArguments() |
55
|
|
|
{ |
56
|
|
|
return [ |
57
|
1 |
|
['bucket', InputArgument::REQUIRED, 'Represents a bucket connection.'], |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the console command options. |
63
|
|
|
* |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
1 |
View Code Duplication |
protected function getOptions() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
return [ |
69
|
1 |
|
['database', 'db', InputOption::VALUE_REQUIRED, 'The database connection to use.', $this->defaultDatabase], |
70
|
|
|
['name', null, InputOption::VALUE_REQUIRED, 'the custom name for the primary index.', '#primary'], |
71
|
|
|
[ |
72
|
|
|
'ignore', |
73
|
|
|
'ig', |
74
|
|
|
InputOption::VALUE_NONE, |
75
|
|
|
'if a primary index already exists, an exception will be thrown unless this is set to true.', |
76
|
|
|
], |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Execute the console command |
82
|
|
|
*/ |
83
|
1 |
View Code Duplication |
public function fire() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
/** @var \Illuminate\Database\Connection|CouchbaseConnection $connection */ |
86
|
1 |
|
$connection = $this->databaseManager->connection($this->option('database')); |
|
|
|
|
87
|
1 |
|
if ($connection instanceof CouchbaseConnection) { |
88
|
1 |
|
$bucket = $connection->openBucket($this->argument('bucket')); |
|
|
|
|
89
|
1 |
|
$bucket->manager()->dropN1qlPrimaryIndex( |
90
|
1 |
|
$this->option('name'), |
91
|
1 |
|
$this->option('ignore') |
92
|
|
|
); |
93
|
1 |
|
$this->info("dropped PRIMARY INDEX [{$this->option('name')}] for [{$this->argument('bucket')}] bucket."); |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
return; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.