. * --------------------------------------------------------------------- */ namespace Glpi\Console\Migration; if (!defined('GLPI_ROOT')) { die("Sorry. You can't access this file directly"); } use DB; use Glpi\Console\AbstractCommand; use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ConfirmationQuestion; class MyIsamToInnoDbCommand extends AbstractCommand { /** * Error code returned when failed to migrate one table. * * @var integer */ const ERROR_TABLE_MIGRATION_FAILED = 1; protected function configure() { parent::configure(); $this->setName('glpi:migration:myisam_to_innodb'); $this->setDescription(__('Migrate MyISAM tables to InnoDB')); } protected function execute(InputInterface $input, OutputInterface $output) { $no_interaction = $input->getOption('no-interaction'); // Base symfony/console option $myisam_tables = $this->db->getMyIsamTables(); $output->writeln( sprintf( '' . __('Found %s table(s) using MyISAM engine.') . '', $myisam_tables->count() ) ); if (0 === $myisam_tables->count()) { $output->writeln('' . __('No migration needed.') . ''); return 0; } if (!$no_interaction) { // Ask for confirmation (unless --no-interaction) /** @var QuestionHelper $question_helper */ $question_helper = $this->getHelper('question'); $run = $question_helper->ask( $input, $output, new ConfirmationQuestion(__('Do you want to continue ?') . ' [Yes/no]', true) ); if (!$run) { $output->writeln( '' . __('Migration aborted.') . '', OutputInterface::VERBOSITY_VERBOSE ); return 0; } } while ($table = $myisam_tables->next()) { $table_name = DB::quoteName($table['TABLE_NAME']); $output->writeln( '' . sprintf(__('Migrating table "%s"...'), $table_name) . '', OutputInterface::VERBOSITY_VERBOSE ); $result = $this->db->query(sprintf('ALTER TABLE %s ENGINE = InnoDB', $table_name)); if (false === $result) { $message = sprintf( __('Migration of table "%s" failed with message "(%s) %s".'), $table_name, $this->db->errno(), $this->db->error() ); $output->writeln( '' . $message . '', OutputInterface::VERBOSITY_QUIET ); return self::ERROR_TABLE_MIGRATION_FAILED; } } $output->writeln('' . __('Migration done.') . ''); return 0; // Success } }