// +---------------------------------------------------------------------- namespace think\migration\command\migrate; use think\console\input\Option as InputOption; use think\console\Input; use think\console\Output; use think\migration\command\Migrate; class Status extends Migrate { /** * {@inheritdoc} */ protected function configure() { $this->setName('migrate:status') ->setDescription('Show migration status') ->addOption('--format', '-f', InputOption::VALUE_REQUIRED, 'The output format: text or json. Defaults to text.') ->setHelp(<<migrate:status command prints a list of all migrations, along with their current status php think migrate:status php think migrate:status -f json EOT ); } /** * Show the migration status. * * @param Input $input * @param Output $output * @return integer 0 if all migrations are up, or an error code */ protected function execute(Input $input, Output $output) { $format = $input->getOption('format'); if (null !== $format) { $output->writeln('using format ' . $format); } // print the status return $this->printStatus($format); } protected function printStatus($format = null) { $output = $this->output; $migrations = []; if (count($this->getMigrations())) { // TODO - rewrite using Symfony Table Helper as we already have this library // included and it will fix formatting issues (e.g drawing the lines) $output->writeln(''); $output->writeln(' Status Migration ID Started Finished Migration Name '); $output->writeln('----------------------------------------------------------------------------------'); $versions = $this->getVersionLog(); $maxNameLength = $versions ? max(array_map(function ($version) { return strlen($version['migration_name']); }, $versions)) : 0; foreach ($this->getMigrations() as $migration) { $version = array_key_exists($migration->getVersion(), $versions) ? $versions[$migration->getVersion()] : false; if ($version) { $status = ' up '; } else { $status = ' down '; } $maxNameLength = max($maxNameLength, strlen($migration->getName())); $output->writeln(sprintf('%s %14.0f %19s %19s %s', $status, $migration->getVersion(), $version['start_time'], $version['end_time'], $migration->getName())); if ($version && $version['breakpoint']) { $output->writeln(' BREAKPOINT SET'); } $migrations[] = [ 'migration_status' => trim(strip_tags($status)), 'migration_id' => sprintf('%14.0f', $migration->getVersion()), 'migration_name' => $migration->getName() ]; unset($versions[$migration->getVersion()]); } if (count($versions)) { foreach ($versions as $missing => $version) { $output->writeln(sprintf(' up %14.0f %19s %19s %s ** MISSING **', $missing, $version['start_time'], $version['end_time'], str_pad($version['migration_name'], $maxNameLength, ' '))); if ($version && $version['breakpoint']) { $output->writeln(' BREAKPOINT SET'); } } } } else { // there are no migrations $output->writeln(''); $output->writeln('There are no available migrations. Try creating one using the create command.'); } // write an empty line $output->writeln(''); if ($format !== null) { switch ($format) { case 'json': $output->writeln(json_encode([ 'pending_count' => count($this->getMigrations()), 'migrations' => $migrations ])); break; default: $output->writeln('Unsupported format: ' . $format . ''); } } } }