Server : nginx/1.20.2 System : Linux VM-4-4-centos 3.10.0-1160.66.1.el7.x86_64 #1 SMP Wed May 18 16:02:34 UTC 2022 x86_64 User : www ( 1000) PHP Version : 5.6.40 Disable Function : passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv Directory : /www/wwwroot/greatapp.cn/vendor/topthink/think-migration/src/command/migrate/ |
<?php // +---------------------------------------------------------------------- // | TopThink [ WE CAN DO IT JUST THINK IT ] // +---------------------------------------------------------------------- // | Copyright (c) 2016 http://www.topthink.com All rights reserved. // +---------------------------------------------------------------------- // | Author: zhangyajun <448901948@qq.com> // +---------------------------------------------------------------------- namespace think\migration\command\migrate; use Phinx\Migration\MigrationInterface; use think\console\Input; use think\console\input\Option as InputOption; use think\console\Output; use think\migration\command\Migrate; class Run extends Migrate { /** * {@inheritdoc} */ protected function configure() { $this->setName('migrate:run') ->setDescription('Migrate the database') ->addOption('--target', '-t', InputOption::VALUE_REQUIRED, 'The version number to migrate to') ->addOption('--date', '-d', InputOption::VALUE_REQUIRED, 'The date to migrate to') ->setHelp(<<<EOT The <info>migrate:run</info> command runs all available migrations, optionally up to a specific version <info>php console migrate:run</info> <info>php console migrate:run -t 20110103081132</info> <info>php console migrate:run -d 20110103</info> <info>php console migrate:run -v</info> EOT ); } /** * Migrate the database. * * @param Input $input * @param Output $output * @return integer integer 0 on success, or an error code. */ protected function execute(Input $input, Output $output) { $version = $input->getOption('target'); $date = $input->getOption('date'); // run the migrations $start = microtime(true); if (null !== $date) { $this->migrateToDateTime(new \DateTime($date)); } else { $this->migrate($version); } $end = microtime(true); $output->writeln(''); $output->writeln('<comment>All Done. Took ' . sprintf('%.4fs', $end - $start) . '</comment>'); } public function migrateToDateTime(\DateTime $dateTime) { $versions = array_keys($this->getMigrations()); $dateString = $dateTime->format('YmdHis'); $outstandingMigrations = array_filter($versions, function ($version) use ($dateString) { return $version <= $dateString; }); if (count($outstandingMigrations) > 0) { $migration = max($outstandingMigrations); $this->output->writeln('Migrating to version ' . $migration); $this->migrate($migration); } } protected function migrate($version = null) { $migrations = $this->getMigrations(); $versions = $this->getVersions(); $current = $this->getCurrentVersion(); if (empty($versions) && empty($migrations)) { return; } if (null === $version) { $version = max(array_merge($versions, array_keys($migrations))); } else { if (0 != $version && !isset($migrations[$version])) { $this->output->writeln(sprintf('<comment>warning</comment> %s is not a valid version', $version)); return; } } // are we migrating up or down? $direction = $version > $current ? MigrationInterface::UP : MigrationInterface::DOWN; if ($direction === MigrationInterface::DOWN) { // run downs first krsort($migrations); foreach ($migrations as $migration) { if ($migration->getVersion() <= $version) { break; } if (in_array($migration->getVersion(), $versions)) { $this->executeMigration($migration, MigrationInterface::DOWN); } } } ksort($migrations); foreach ($migrations as $migration) { if ($migration->getVersion() > $version) { break; } if (!in_array($migration->getVersion(), $versions)) { $this->executeMigration($migration, MigrationInterface::UP); } } } protected function getCurrentVersion() { $versions = $this->getVersions(); $version = 0; if (!empty($versions)) { $version = end($versions); } return $version; } }