コード例 #1
0
ファイル: DiffCommand.php プロジェクト: rimantoro/mysql-diff
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $purge = $fks = false;
     $mode = 0;
     $checks = [];
     if ($input->getOption('create')) {
         $mode |= CompareService::PROCESS_CREATE;
         $checks[] = 'Add missing tables';
     }
     if ($input->getOption('alter')) {
         $mode |= CompareService::PROCESS_ALTER;
         $fks = (bool) $input->getOption('fks');
         if ($input->getOption('purge')) {
             $mode |= CompareService::PROCESS_PURGE;
             $purge = true;
         }
         $checks[] = 'Check table changes ' . ($fks ? '' : 'NOT ') . 'including foreign keys';
     }
     if ($input->getOption('drop')) {
         $mode |= CompareService::PROCESS_DROP;
         $checks[] = 'Drop legacy tables';
     }
     if ($mode === 0) {
         $output->writeln('<info>Nothing to process: use the tcaFd flags to control what should be checked</info>');
         return;
     }
     $output->writeln(sprintf('<info>Processing tables:</info>', $mode));
     foreach ($checks as $ln) {
         $output->writeln(sprintf('<info>%s</info>', $ln));
     }
     if ($purge === true) {
         $output->getFormatter()->setStyle('purge', new OutputFormatterStyle('red', 'yellow', ['bold', 'blink', 'underscore']));
         $output->writeln('<purge>Dropping old fields</purge>');
     }
     $dbService = $this->getDbService($input, $output);
     if (!$dbService) {
         return;
     }
     $compare = new CompareService($dbService);
     $compare->processTables($mode, $fks);
     $changes = $compare->getChanges();
     foreach ($changes as $key => $values) {
         if ($values) {
             $output->writeln(sprintf('-- %s', $key));
             foreach ($values as $val) {
                 $output->writeln($val);
             }
         }
     }
 }
コード例 #2
0
 /**
  * @param string $task
  * @param CompareService $service
  * @param OutputInterface $output
  * @param array $dbs
  * @param bool $loaded
  * @return array
  */
 protected function processTask($task, CompareService $service, OutputInterface $output, array $dbs, $loaded)
 {
     $queries = [];
     switch ($task) {
         case 'create':
             if ($this->interactive >= self::INTERACTION_LOW) {
                 $renames = $this->dialog->askConfirmation($output, '<question>Attempt to rename tables? - Default true if interactive + loaded dependencies, false in other cases</question>', $this->interactive && $loaded);
             } else {
                 $renames = false;
             }
             if ($renames && !$loaded) {
                 //inform the user, but let them shoot themselves in the foot regardless...
                 $output->writeln('<error>Renaming tables without checking relations between tables is dangerous!</error>');
             }
             $return = $service->getMissingTablesFor($dbs['base'], $dbs['target'], $renames);
             $output->writeln(sprintf('<info>Added %d new tables (%s)</info>', count($return['added']), implode(', ', array_keys($return['added']))));
             /** @var Table $table */
             foreach ($return['added'] as $table) {
                 $queries[] = $table->getDefinitionString();
             }
             if ($return['renames']) {
                 $process = $this->processRenames($return['renames'], $output);
                 /** @var Database $base */
                 $base = $dbs['base'];
                 $base->addMissingTables($process['add']);
                 foreach ($process['add'] as $table) {
                     $queries[] = $table->getDefinitionString();
                 }
                 foreach ($process['rename'] as $oldName => $table) {
                     $queries[] = $table->getRenameQuery();
                     $base->applyRename($table, $oldName);
                 }
             }
             break;
         case 'alter':
             $dropFields = false;
             $checkFks = false;
             if ($this->interactive) {
                 $dropFields = $this->dialog->askConfirmation($output, '<question>Drop fields not in target table? (default: false)</question>', $dropFields);
                 $checkFks = $this->dialog->askConfirmation($output, '<question>Check Foreign Key constraints? (default: false)</question>', $checkFks);
             }
             $queries = $service->compareTables($dbs['base'], $dbs['target'], $dropFields, $checkFks);
             break;
         case 'constraints':
             $output->writeln('<info>constraints task not implemented yet</info>');
             $queries[] = '';
             break;
         case 'drop':
             if (!$loaded) {
                 $output->writeln('<error>Cannot reliably drop tables if relational table links were not set up</error>');
                 $output->writeln('<comment>As a result, these drop statements might not work</comment>');
             }
             $queries = $service->dropRedundantTables($dbs['base'], $dbs['target']);
             break;
         default:
             $queries[] = '';
     }
     return $queries;
 }