1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Db3v4l\Command; |
4
|
|
|
|
5
|
|
|
use Db3v4l\Core\DatabaseSchemaManager; |
|
|
|
|
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
|
|
|
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
|
|
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
|
|
|
9
|
|
|
|
10
|
|
|
class UserDrop extends DatabaseManagingCommand |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
protected static $defaultName = 'db3v4l:user:drop'; |
13
|
|
|
|
14
|
|
|
protected function configure() |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
$this |
17
|
|
|
->setDescription('Drops a database user in parallel on all configured database instances') |
18
|
|
|
->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'The name of the user to drop') |
19
|
|
|
->addCommonOptions() |
|
|
|
|
20
|
|
|
; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
|
|
|
|
24
|
|
|
* @param InputInterface $input |
|
|
|
|
25
|
|
|
* @param OutputInterface $output |
|
|
|
|
26
|
|
|
* @return int |
|
|
|
|
27
|
|
|
* @throws \Exception |
|
|
|
|
28
|
|
|
*/ |
29
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
30
|
|
|
{ |
31
|
|
|
$start = microtime(true); |
32
|
|
|
|
33
|
|
|
// as per https://www.php.net/manual/en/function.ignore-user-abort.php: for cli scripts, it is probably a good idea |
34
|
|
|
// to use ignore_user_abort |
35
|
|
|
ignore_user_abort(true); |
36
|
|
|
|
37
|
|
|
$this->setOutput($output); |
38
|
|
|
$this->setVerbosity($output->getVerbosity()); |
39
|
|
|
|
40
|
|
|
$instanceList = $this->parseCommonOptions($input); |
41
|
|
|
|
42
|
|
|
$userName = $input->getOption('user'); |
43
|
|
|
|
44
|
|
|
if ($userName == null) { |
45
|
|
|
throw new \Exception("Please provide a username"); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if ($this->outputFormat === 'text') { |
49
|
|
|
$this->writeln('<info>Dropping users...</info>'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$userToDropSpecs = []; |
53
|
|
|
foreach($instanceList as $instanceName => $instanceSpecs) { |
|
|
|
|
54
|
|
|
$userToDropSpecs[$instanceName] = [ |
55
|
|
|
'user' => $userName |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
$results = $this->dropUsers($instanceList, $userToDropSpecs); |
59
|
|
|
|
60
|
|
|
// BC with versions < 0.8 |
61
|
|
|
$results['data'] = null; |
62
|
|
|
|
63
|
|
|
$time = microtime(true) - $start; |
64
|
|
|
|
65
|
|
|
$this->writeResults($results, $time); |
66
|
|
|
|
67
|
|
|
return (int)$results['failed']; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
|
|
|
|
71
|
|
|
* @param $instanceList |
|
|
|
|
72
|
|
|
* @param $userToDropSpecs |
|
|
|
|
73
|
|
|
* @param bool $ifExists |
|
|
|
|
74
|
|
|
* @return array |
|
|
|
|
75
|
|
|
* @throws \Exception |
|
|
|
|
76
|
|
|
*/ |
77
|
|
|
protected function dropUsers($instanceList, $userToDropSpecs, $ifExists = false) |
78
|
|
|
{ |
79
|
|
|
return $this->executeSqlAction( |
80
|
|
|
$instanceList, |
81
|
|
|
'Dropping of user', |
82
|
|
|
function ($schemaManager, $instanceName) use ($userToDropSpecs, $ifExists) { |
83
|
|
|
$dbConnectionSpec = $userToDropSpecs[$instanceName]; |
84
|
|
|
/** @var DatabaseSchemaManager $schemaManager */ |
|
|
|
|
85
|
|
|
return $schemaManager->getDropUserSqlAction( |
86
|
|
|
$dbConnectionSpec['user'], |
87
|
|
|
$ifExists |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|