<?php
<448901948@qq.com>
namespace think\migration\command;
use Phinx\Db\Adapter\AdapterFactory;
use Phinx\Db\Adapter\ProxyAdapter;
use Phinx\Migration\AbstractMigration;
use Phinx\Migration\MigrationInterface;
use Phinx\Util\Util;
use think\console\Input;
use think\console\input\Option as InputOption;
use think\console\Output;
use think\migration\Command;
use think\migration\Migrator;
abstract class Migrate extends Command
{
protected $migrations;
public function __construct($name = null)
{
parent::__construct($name);
$this->addOption('--config', null, InputOption::VALUE_REQUIRED, 'The database config name', 'database');
}
protected function initialize(Input $input, Output $output)
{
$this->config = $input->getOption('config');
}
protected function getPath()
{
return $this->getConfig('path', ROOT_PATH . 'database') . DS . 'migrations' . ($this->config !== 'database' ? DS . $this->config : '');
}
protected function executeMigration(MigrationInterface $migration, $direction = MigrationInterface::UP)
{
$this->output->writeln('');
$this->output->writeln(' ==' . ' <info>' . $migration->getVersion() . ' ' . $migration->getName() . ':</info>' . ' <comment>' . ($direction === MigrationInterface::UP ? 'migrating' : 'reverting') . '</comment>');
$start = microtime(true);
$startTime = time();
$direction = ($direction === MigrationInterface::UP) ? MigrationInterface::UP : MigrationInterface::DOWN;
$migration->setAdapter($this->getAdapter());
if ($this->getAdapter()->hasTransactions()) {
$this->getAdapter()->beginTransaction();
}
if (method_exists($migration, MigrationInterface::CHANGE)) {
if ($direction === MigrationInterface::DOWN) {
$proxyAdapter = AdapterFactory::instance()->getWrapper('proxy', $this->getAdapter());
$migration->setAdapter($proxyAdapter);
$migration->change();
$proxyAdapter->executeInvertedCommands();
$migration->setAdapter($this->getAdapter());
} else {
$migration->change();
}
} else {
$migration->{$direction}();
}
if ($this->getAdapter()->hasTransactions()) {
$this->getAdapter()->commitTransaction();
}
$this->getAdapter()
->migrated($migration, $direction, date('Y-m-d H:i:s', $startTime), date('Y-m-d H:i:s', time()));
$end = microtime(true);
$this->output->writeln(' ==' . ' <info>' . $migration->getVersion() . ' ' . $migration->getName() . ':</info>' . ' <comment>' . ($direction === MigrationInterface::UP ? 'migrated' : 'reverted') . ' ' . sprintf('%.4fs', $end - $start) . '</comment>');
}
protected function getVersionLog()
{
return $this->getAdapter()->getVersionLog();
}
protected function getVersions()
{
return $this->getAdapter()->getVersions();
}
protected function getMigrations()
{
if (null === $this->migrations) {
$phpFiles = glob($this->getPath() . DS . '*.php', defined('GLOB_BRACE') ? GLOB_BRACE : 0);
$fileNames = [];
$versions = [];
foreach ($phpFiles as $filePath) {
if (Util::isValidMigrationFileName(basename($filePath))) {
$version = Util::getVersionFromFileName(basename($filePath));
if (isset($versions[$version])) {
throw new \InvalidArgumentException(sprintf('Duplicate migration - "%s" has the same version as "%s"', $filePath, $versions[$version]->getVersion()));
}
$class = Util::mapFileNameToClassName(basename($filePath));
if (isset($fileNames[$class])) {
throw new \InvalidArgumentException(sprintf('Migration "%s" has the same name as "%s"', basename($filePath), $fileNames[$class]));
}
$fileNames[$class] = basename($filePath);
require_once $filePath;
if (!class_exists($class)) {
throw new \InvalidArgumentException(sprintf('Could not find class "%s" in file "%s"', $class, $filePath));
}
$migration = new $class($version, $this->input, $this->output);
if (!($migration instanceof AbstractMigration)) {
throw new \InvalidArgumentException(sprintf('The class "%s" in file "%s" must extend \Phinx\Migration\AbstractMigration', $class, $filePath));
}
$versions[$version] = $migration;
}
}
ksort($versions);
$this->migrations = $versions;
}
return $this->migrations;
}
}