| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Db3v4l\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Console\Input\InputOption; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class DatabaseDrop extends DatabaseManagingCommand | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     protected static $defaultName = 'db3v4l:database:drop'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     protected function configure() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |             ->setDescription('Drops a database & associated user in parallel on all configured database instances') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |             ->addOption('database', 'd', InputOption::VALUE_REQUIRED, 'The name of the database to drop') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |             ->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'The name of a user to drop. If omitted, no user will be dropped') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |         $dbName = $input->getOption('database'); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         // BC api | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         if ($dbName == null && $userName != null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |             $dbName = $userName; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         if ($dbName == null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             throw new \Exception("Please provide a database name"); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         if ($this->outputFormat === 'text') { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             $this->writeln('<info>Dropping databases...</info>'); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         $dbToDropSpecs = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         foreach($instanceList as $instanceName => $instanceSpecs) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |             $dbToDropSpecs[$instanceName] = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |                 'dbname' => $dbName | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             if ($userName != null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                 $dbToDropSpecs[$instanceName]['user'] = $userName; | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         $results = $this->dropDatabases($instanceList, $dbToDropSpecs); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         // q: shall we print something more useful? | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         $results['data'] = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         $time = microtime(true) - $start; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         $this->writeResults($results, $time); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         return (int)$results['failed']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 79 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |  |