1
This commit is contained in:
167
vendor/topthink/think-migration/src/command/Migrate.php
vendored
Normal file
167
vendor/topthink/think-migration/src/command/Migrate.php
vendored
Normal file
@@ -0,0 +1,167 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2016 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: yunwuxin <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
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $migrations;
|
||||
|
||||
public function __construct($name = null)
|
||||
{
|
||||
|
||||
parent::__construct($name);
|
||||
|
||||
$this->addOption('--config', null, InputOption::VALUE_REQUIRED, 'The database config name', 'database');
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
* @param Input $input An InputInterface instance
|
||||
* @param Output $output An OutputInterface instance
|
||||
*/
|
||||
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>');
|
||||
|
||||
// Execute the migration and log the time elapsed.
|
||||
$start = microtime(true);
|
||||
|
||||
$startTime = time();
|
||||
$direction = ($direction === MigrationInterface::UP) ? MigrationInterface::UP : MigrationInterface::DOWN;
|
||||
$migration->setAdapter($this->getAdapter());
|
||||
|
||||
// begin the transaction if the adapter supports it
|
||||
if ($this->getAdapter()->hasTransactions()) {
|
||||
$this->getAdapter()->beginTransaction();
|
||||
}
|
||||
|
||||
// Run the migration
|
||||
if (method_exists($migration, MigrationInterface::CHANGE)) {
|
||||
if ($direction === MigrationInterface::DOWN) {
|
||||
// Create an instance of the ProxyAdapter so we can record all
|
||||
// of the migration commands for reverse playback
|
||||
/** @var ProxyAdapter $proxyAdapter */
|
||||
$proxyAdapter = AdapterFactory::instance()->getWrapper('proxy', $this->getAdapter());
|
||||
$migration->setAdapter($proxyAdapter);
|
||||
/** @noinspection PhpUndefinedMethodInspection */
|
||||
$migration->change();
|
||||
$proxyAdapter->executeInvertedCommands();
|
||||
$migration->setAdapter($this->getAdapter());
|
||||
} else {
|
||||
/** @noinspection PhpUndefinedMethodInspection */
|
||||
$migration->change();
|
||||
}
|
||||
} else {
|
||||
$migration->{$direction}();
|
||||
}
|
||||
|
||||
// commit the transaction if the adapter supports it
|
||||
if ($this->getAdapter()->hasTransactions()) {
|
||||
$this->getAdapter()->commitTransaction();
|
||||
}
|
||||
|
||||
// Record it in the database
|
||||
$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);
|
||||
|
||||
// filter the files to only get the ones that match our naming scheme
|
||||
$fileNames = [];
|
||||
/** @var Migrator[] $versions */
|
||||
$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()));
|
||||
}
|
||||
|
||||
// convert the filename to a class name
|
||||
$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);
|
||||
|
||||
// load the migration file
|
||||
/** @noinspection PhpIncludeInspection */
|
||||
require_once $filePath;
|
||||
if (!class_exists($class)) {
|
||||
throw new \InvalidArgumentException(sprintf('Could not find class "%s" in file "%s"', $class, $filePath));
|
||||
}
|
||||
|
||||
// instantiate it
|
||||
$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;
|
||||
}
|
||||
}
|
||||
72
vendor/topthink/think-migration/src/command/Seed.php
vendored
Normal file
72
vendor/topthink/think-migration/src/command/Seed.php
vendored
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2016 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: yunwuxin <448901948@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\migration\command;
|
||||
|
||||
use Phinx\Seed\AbstractSeed;
|
||||
use Phinx\Util\Util;
|
||||
use think\migration\Command;
|
||||
use think\migration\Seeder;
|
||||
|
||||
abstract class Seed extends Command
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $seeds;
|
||||
|
||||
protected function getPath()
|
||||
{
|
||||
return $this->getConfig('path', ROOT_PATH . 'database') . DS . 'seeds';
|
||||
}
|
||||
|
||||
public function getSeeds()
|
||||
{
|
||||
if (null === $this->seeds) {
|
||||
$phpFiles = glob($this->getPath() . DS . '*.php', defined('GLOB_BRACE') ? GLOB_BRACE : 0);
|
||||
|
||||
// filter the files to only get the ones that match our naming scheme
|
||||
$fileNames = [];
|
||||
/** @var Seeder[] $seeds */
|
||||
$seeds = [];
|
||||
|
||||
foreach ($phpFiles as $filePath) {
|
||||
if (Util::isValidSeedFileName(basename($filePath))) {
|
||||
// convert the filename to a class name
|
||||
$class = pathinfo($filePath, PATHINFO_FILENAME);
|
||||
$fileNames[$class] = basename($filePath);
|
||||
|
||||
// load the seed file
|
||||
/** @noinspection PhpIncludeInspection */
|
||||
require_once $filePath;
|
||||
if (!class_exists($class)) {
|
||||
throw new \InvalidArgumentException(sprintf('Could not find class "%s" in file "%s"', $class, $filePath));
|
||||
}
|
||||
|
||||
// instantiate it
|
||||
$seed = new $class($this->input, $this->output);
|
||||
|
||||
if (!($seed instanceof AbstractSeed)) {
|
||||
throw new \InvalidArgumentException(sprintf('The class "%s" in file "%s" must extend \Phinx\Seed\AbstractSeed', $class, $filePath));
|
||||
}
|
||||
|
||||
$seeds[$class] = $seed;
|
||||
}
|
||||
}
|
||||
|
||||
ksort($seeds);
|
||||
$this->seeds = $seeds;
|
||||
}
|
||||
|
||||
return $this->seeds;
|
||||
}
|
||||
}
|
||||
92
vendor/topthink/think-migration/src/command/migrate/Breakpoint.php
vendored
Normal file
92
vendor/topthink/think-migration/src/command/migrate/Breakpoint.php
vendored
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2016 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: yunwuxin <448901948@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\migration\command\migrate;
|
||||
|
||||
use think\console\Input;
|
||||
use think\console\input\Option as InputOption;
|
||||
use think\console\Output;
|
||||
use think\migration\command\Migrate;
|
||||
|
||||
class Breakpoint extends Migrate
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('migrate:breakpoint')
|
||||
->setDescription('Manage breakpoints')
|
||||
->addOption('--target', '-t', InputOption::VALUE_REQUIRED, 'The version number to set or clear a breakpoint against')
|
||||
->addOption('--remove-all', '-r', InputOption::VALUE_NONE, 'Remove all breakpoints')
|
||||
->setHelp(<<<EOT
|
||||
The <info>breakpoint</info> command allows you to set or clear a breakpoint against a specific target to inhibit rollbacks beyond a certain target.
|
||||
If no target is supplied then the most recent migration will be used.
|
||||
You cannot specify un-migrated targets
|
||||
|
||||
<info>phinx breakpoint</info>
|
||||
<info>phinx breakpoint -t 20110103081132</info>
|
||||
<info>phinx breakpoint -r</info>
|
||||
EOT
|
||||
);
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$version = $input->getOption('target');
|
||||
$removeAll = $input->getOption('remove-all');
|
||||
|
||||
if ($version && $removeAll) {
|
||||
throw new \InvalidArgumentException('Cannot toggle a breakpoint and remove all breakpoints at the same time.');
|
||||
}
|
||||
|
||||
// Remove all breakpoints
|
||||
if ($removeAll) {
|
||||
$this->removeBreakpoints();
|
||||
} else {
|
||||
// Toggle the breakpoint.
|
||||
$this->toggleBreakpoint($version);
|
||||
}
|
||||
}
|
||||
|
||||
protected function toggleBreakpoint($version)
|
||||
{
|
||||
$migrations = $this->getMigrations();
|
||||
$versions = $this->getVersionLog();
|
||||
|
||||
if (empty($versions) || empty($migrations)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (null === $version) {
|
||||
$lastVersion = end($versions);
|
||||
$version = $lastVersion['version'];
|
||||
}
|
||||
|
||||
if (0 != $version && !isset($migrations[$version])) {
|
||||
$this->output->writeln(sprintf('<comment>warning</comment> %s is not a valid version', $version));
|
||||
return;
|
||||
}
|
||||
|
||||
$this->getAdapter()->toggleBreakpoint($migrations[$version]);
|
||||
|
||||
$versions = $this->getVersionLog();
|
||||
|
||||
$this->output->writeln(' Breakpoint ' . ($versions[$version]['breakpoint'] ? 'set' : 'cleared') . ' for <info>' . $version . '</info>' . ' <comment>' . $migrations[$version]->getName() . '</comment>');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all breakpoints
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function removeBreakpoints()
|
||||
{
|
||||
$this->output->writeln(sprintf(' %d breakpoints cleared.', $this->getAdapter()->resetAllBreakpoints()));
|
||||
}
|
||||
}
|
||||
95
vendor/topthink/think-migration/src/command/migrate/Create.php
vendored
Normal file
95
vendor/topthink/think-migration/src/command/migrate/Create.php
vendored
Normal file
@@ -0,0 +1,95 @@
|
||||
<?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\Util\Util;
|
||||
use think\console\input\Argument as InputArgument;
|
||||
use think\console\Input;
|
||||
use think\console\Output;
|
||||
use think\migration\command\Migrate;
|
||||
|
||||
class Create extends Migrate
|
||||
{
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('migrate:create')
|
||||
->setDescription('Create a new migration')
|
||||
->addArgument('name', InputArgument::REQUIRED, 'What is the name of the migration?')
|
||||
->setHelp(sprintf('%sCreates a new database migration%s', PHP_EOL, PHP_EOL));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the new migration.
|
||||
*
|
||||
* @param Input $input
|
||||
* @param Output $output
|
||||
* @throws \RuntimeException
|
||||
* @throws \InvalidArgumentException
|
||||
* @return void
|
||||
*/
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$path = $this->getPath();
|
||||
|
||||
if (!file_exists($path)) {
|
||||
if ($this->output->confirm($this->input, 'Create migrations directory? [y]/n')) {
|
||||
mkdir($path, 0755, true);
|
||||
}
|
||||
}
|
||||
|
||||
$this->verifyMigrationDirectory($path);
|
||||
|
||||
$path = realpath($path);
|
||||
$className = $input->getArgument('name');
|
||||
|
||||
if (!Util::isValidPhinxClassName($className)) {
|
||||
throw new \InvalidArgumentException(sprintf('The migration class name "%s" is invalid. Please use CamelCase format.', $className));
|
||||
}
|
||||
|
||||
if (!Util::isUniqueMigrationClassName($className, $path)) {
|
||||
throw new \InvalidArgumentException(sprintf('The migration class name "%s" already exists', $className));
|
||||
}
|
||||
|
||||
// Compute the file path
|
||||
$fileName = Util::mapClassNameToFileName($className);
|
||||
$filePath = $path . DS . $fileName;
|
||||
|
||||
if (is_file($filePath)) {
|
||||
throw new \InvalidArgumentException(sprintf('The file "%s" already exists', $filePath));
|
||||
}
|
||||
|
||||
// Verify that the template creation class (or the aliased class) exists and that it implements the required interface.
|
||||
$aliasedClassName = null;
|
||||
|
||||
// Load the alternative template if it is defined.
|
||||
$contents = file_get_contents($this->getTemplate());
|
||||
|
||||
// inject the class names appropriate to this migration
|
||||
$contents = strtr($contents, [
|
||||
'$className' => $className,
|
||||
]);
|
||||
|
||||
if (false === file_put_contents($filePath, $contents)) {
|
||||
throw new \RuntimeException(sprintf('The file "%s" could not be written to', $path));
|
||||
}
|
||||
|
||||
$output->writeln('<info>created</info> .' . str_replace(getcwd(), '', $filePath));
|
||||
}
|
||||
|
||||
protected function getTemplate()
|
||||
{
|
||||
return __DIR__ . '/../stubs/migrate.stub';
|
||||
}
|
||||
|
||||
}
|
||||
146
vendor/topthink/think-migration/src/command/migrate/Rollback.php
vendored
Normal file
146
vendor/topthink/think-migration/src/command/migrate/Rollback.php
vendored
Normal file
@@ -0,0 +1,146 @@
|
||||
<?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\Option as InputOption;
|
||||
use think\console\Input;
|
||||
use think\console\Output;
|
||||
use think\migration\command\Migrate;
|
||||
|
||||
class Rollback extends Migrate
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('migrate:rollback')
|
||||
->setDescription('Rollback the last or to a specific migration')
|
||||
->addOption('--target', '-t', InputOption::VALUE_REQUIRED, 'The version number to rollback to')
|
||||
->addOption('--date', '-d', InputOption::VALUE_REQUIRED, 'The date to rollback to')
|
||||
->addOption('--force', '-f', InputOption::VALUE_NONE, 'Force rollback to ignore breakpoints')
|
||||
->setHelp(<<<EOT
|
||||
The <info>migrate:rollback</info> command reverts the last migration, or optionally up to a specific version
|
||||
|
||||
<info>php console migrate:rollback</info>
|
||||
<info>php console migrate:rollback -t 20111018185412</info>
|
||||
<info>php console migrate:rollback -d 20111018</info>
|
||||
<info>php console migrate:rollback -v</info>
|
||||
|
||||
EOT
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rollback the migration.
|
||||
*
|
||||
* @param Input $input
|
||||
* @param Output $output
|
||||
* @return void
|
||||
*/
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$version = $input->getOption('target');
|
||||
$date = $input->getOption('date');
|
||||
$force = !!$input->getOption('force');
|
||||
|
||||
// rollback the specified environment
|
||||
$start = microtime(true);
|
||||
if (null !== $date) {
|
||||
$this->rollbackToDateTime(new \DateTime($date), $force);
|
||||
} else {
|
||||
$this->rollback($version, $force);
|
||||
}
|
||||
$end = microtime(true);
|
||||
|
||||
$output->writeln('');
|
||||
$output->writeln('<comment>All Done. Took ' . sprintf('%.4fs', $end - $start) . '</comment>');
|
||||
}
|
||||
|
||||
protected function rollback($version = null, $force = false)
|
||||
{
|
||||
$migrations = $this->getMigrations();
|
||||
$versionLog = $this->getVersionLog();
|
||||
$versions = array_keys($versionLog);
|
||||
|
||||
ksort($migrations);
|
||||
sort($versions);
|
||||
|
||||
// Check we have at least 1 migration to revert
|
||||
if (empty($versions) || $version == end($versions)) {
|
||||
$this->output->writeln('<error>No migrations to rollback</error>');
|
||||
return;
|
||||
}
|
||||
|
||||
// If no target version was supplied, revert the last migration
|
||||
if (null === $version) {
|
||||
// Get the migration before the last run migration
|
||||
$prev = count($versions) - 2;
|
||||
$version = $prev < 0 ? 0 : $versions[$prev];
|
||||
} else {
|
||||
// Get the first migration number
|
||||
$first = $versions[0];
|
||||
|
||||
// If the target version is before the first migration, revert all migrations
|
||||
if ($version < $first) {
|
||||
$version = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Check the target version exists
|
||||
if (0 !== $version && !isset($migrations[$version])) {
|
||||
$this->output->writeln("<error>Target version ($version) not found</error>");
|
||||
return;
|
||||
}
|
||||
|
||||
// Revert the migration(s)
|
||||
krsort($migrations);
|
||||
foreach ($migrations as $migration) {
|
||||
if ($migration->getVersion() <= $version) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (in_array($migration->getVersion(), $versions)) {
|
||||
if (isset($versionLog[$migration->getVersion()]) && 0 != $versionLog[$migration->getVersion()]['breakpoint'] && !$force) {
|
||||
$this->output->writeln('<error>Breakpoint reached. Further rollbacks inhibited.</error>');
|
||||
break;
|
||||
}
|
||||
$this->executeMigration($migration, MigrationInterface::DOWN);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function rollbackToDateTime(\DateTime $dateTime, $force = false)
|
||||
{
|
||||
$versions = $this->getVersions();
|
||||
$dateString = $dateTime->format('YmdHis');
|
||||
sort($versions);
|
||||
|
||||
$earlierVersion = null;
|
||||
$availableMigrations = array_filter($versions, function ($version) use ($dateString, &$earlierVersion) {
|
||||
if ($version <= $dateString) {
|
||||
$earlierVersion = $version;
|
||||
}
|
||||
return $version >= $dateString;
|
||||
});
|
||||
|
||||
if (count($availableMigrations) > 0) {
|
||||
if (is_null($earlierVersion)) {
|
||||
$this->output->writeln('Rolling back all migrations');
|
||||
$migration = 0;
|
||||
} else {
|
||||
$this->output->writeln('Rolling back to version ' . $earlierVersion);
|
||||
$migration = $earlierVersion;
|
||||
}
|
||||
$this->rollback($migration, $force);
|
||||
}
|
||||
}
|
||||
}
|
||||
141
vendor/topthink/think-migration/src/command/migrate/Run.php
vendored
Normal file
141
vendor/topthink/think-migration/src/command/migrate/Run.php
vendored
Normal file
@@ -0,0 +1,141 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
124
vendor/topthink/think-migration/src/command/migrate/Status.php
vendored
Normal file
124
vendor/topthink/think-migration/src/command/migrate/Status.php
vendored
Normal file
@@ -0,0 +1,124 @@
|
||||
<?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 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(<<<EOT
|
||||
The <info>migrate:status</info> command prints a list of all migrations, along with their current status
|
||||
|
||||
<info>php console migrate:status</info>
|
||||
<info>php console migrate:status -f json</info>
|
||||
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('<info>using format</info> ' . $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 = ' <info>up</info> ';
|
||||
} else {
|
||||
$status = ' <error>down</error> ';
|
||||
}
|
||||
$maxNameLength = max($maxNameLength, strlen($migration->getName()));
|
||||
|
||||
$output->writeln(sprintf('%s %14.0f %19s %19s <comment>%s</comment>', $status, $migration->getVersion(), $version['start_time'], $version['end_time'], $migration->getName()));
|
||||
|
||||
if ($version && $version['breakpoint']) {
|
||||
$output->writeln(' <error>BREAKPOINT SET</error>');
|
||||
}
|
||||
|
||||
$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(' <error>up</error> %14.0f %19s %19s <comment>%s</comment> <error>** MISSING **</error>', $missing, $version['start_time'], $version['end_time'], str_pad($version['migration_name'], $maxNameLength, ' ')));
|
||||
|
||||
if ($version && $version['breakpoint']) {
|
||||
$output->writeln(' <error>BREAKPOINT SET</error>');
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// there are no migrations
|
||||
$output->writeln('');
|
||||
$output->writeln('There are no available migrations. Try creating one using the <info>create</info> 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('<info>Unsupported format: ' . $format . '</info>');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
85
vendor/topthink/think-migration/src/command/seed/Create.php
vendored
Normal file
85
vendor/topthink/think-migration/src/command/seed/Create.php
vendored
Normal file
@@ -0,0 +1,85 @@
|
||||
<?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\seed;
|
||||
|
||||
use Phinx\Util\Util;
|
||||
use think\console\Input;
|
||||
use think\console\Output;
|
||||
use think\console\input\Argument as InputArgument;
|
||||
use think\migration\command\Seed;
|
||||
|
||||
class Create extends Seed
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('seed:create')
|
||||
->setDescription('Create a new database seeder')
|
||||
->addArgument('name', InputArgument::REQUIRED, 'What is the name of the seeder?')
|
||||
->setHelp(sprintf('%sCreates a new database seeder%s', PHP_EOL, PHP_EOL));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the new seeder.
|
||||
*
|
||||
* @param Input $input
|
||||
* @param Output $output
|
||||
* @throws \RuntimeException
|
||||
* @throws \InvalidArgumentException
|
||||
* @return void
|
||||
*/
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$path = $this->getPath();
|
||||
|
||||
if (!file_exists($path)) {
|
||||
if ($this->output->confirm($this->input, 'Create seeds directory? [y]/n')) {
|
||||
mkdir($path, 0755, true);
|
||||
}
|
||||
}
|
||||
|
||||
$this->verifyMigrationDirectory($path);
|
||||
|
||||
$path = realpath($path);
|
||||
|
||||
$className = $input->getArgument('name');
|
||||
|
||||
if (!Util::isValidPhinxClassName($className)) {
|
||||
throw new \InvalidArgumentException(sprintf('The seed class name "%s" is invalid. Please use CamelCase format', $className));
|
||||
}
|
||||
|
||||
// Compute the file path
|
||||
$filePath = $path . DS . $className . '.php';
|
||||
|
||||
if (is_file($filePath)) {
|
||||
throw new \InvalidArgumentException(sprintf('The file "%s" already exists', basename($filePath)));
|
||||
}
|
||||
|
||||
// inject the class names appropriate to this seeder
|
||||
$contents = file_get_contents($this->getTemplate());
|
||||
$classes = [
|
||||
'$className' => $className
|
||||
];
|
||||
$contents = strtr($contents, $classes);
|
||||
|
||||
if (false === file_put_contents($filePath, $contents)) {
|
||||
throw new \RuntimeException(sprintf('The file "%s" could not be written to', $path));
|
||||
}
|
||||
|
||||
$output->writeln('<info>created</info> .' . str_replace(getcwd(), '', $filePath));
|
||||
}
|
||||
|
||||
protected function getTemplate()
|
||||
{
|
||||
return __DIR__ . '/../stubs/seed.stub';
|
||||
}
|
||||
}
|
||||
107
vendor/topthink/think-migration/src/command/seed/Run.php
vendored
Normal file
107
vendor/topthink/think-migration/src/command/seed/Run.php
vendored
Normal file
@@ -0,0 +1,107 @@
|
||||
<?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\seed;
|
||||
|
||||
use Phinx\Seed\SeedInterface;
|
||||
use think\console\Input;
|
||||
use think\console\input\Option as InputOption;
|
||||
use think\console\Output;
|
||||
use think\migration\command\Seed;
|
||||
|
||||
class Run extends Seed
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('seed:run')
|
||||
->setDescription('Run database seeders')
|
||||
->addOption('--seed', '-s', InputOption::VALUE_REQUIRED, 'What is the name of the seeder?')
|
||||
->setHelp(<<<EOT
|
||||
The <info>seed:run</info> command runs all available or individual seeders
|
||||
|
||||
<info>php console seed:run</info>
|
||||
<info>php console seed:run -s UserSeeder</info>
|
||||
<info>php console seed:run -v</info>
|
||||
|
||||
EOT
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run database seeders.
|
||||
*
|
||||
* @param Input $input
|
||||
* @param Output $output
|
||||
* @return void
|
||||
*/
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$seed = $input->getOption('seed');
|
||||
|
||||
// run the seed(ers)
|
||||
$start = microtime(true);
|
||||
$this->seed($seed);
|
||||
$end = microtime(true);
|
||||
|
||||
$output->writeln('');
|
||||
$output->writeln('<comment>All Done. Took ' . sprintf('%.4fs', $end - $start) . '</comment>');
|
||||
}
|
||||
|
||||
public function seed($seed = null)
|
||||
{
|
||||
$seeds = $this->getSeeds();
|
||||
|
||||
if (null === $seed) {
|
||||
// run all seeders
|
||||
foreach ($seeds as $seeder) {
|
||||
if (array_key_exists($seeder->getName(), $seeds)) {
|
||||
$this->executeSeed($seeder);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// run only one seeder
|
||||
if (array_key_exists($seed, $seeds)) {
|
||||
$this->executeSeed($seeds[$seed]);
|
||||
} else {
|
||||
throw new \InvalidArgumentException(sprintf('The seed class "%s" does not exist', $seed));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function executeSeed(SeedInterface $seed)
|
||||
{
|
||||
$this->output->writeln('');
|
||||
$this->output->writeln(' ==' . ' <info>' . $seed->getName() . ':</info>' . ' <comment>seeding</comment>');
|
||||
|
||||
// Execute the seeder and log the time elapsed.
|
||||
$start = microtime(true);
|
||||
$seed->setAdapter($this->getAdapter());
|
||||
|
||||
// begin the transaction if the adapter supports it
|
||||
if ($this->getAdapter()->hasTransactions()) {
|
||||
$this->getAdapter()->beginTransaction();
|
||||
}
|
||||
|
||||
// Run the seeder
|
||||
if (method_exists($seed, SeedInterface::RUN)) {
|
||||
$seed->run();
|
||||
}
|
||||
|
||||
// commit the transaction if the adapter supports it
|
||||
if ($this->getAdapter()->hasTransactions()) {
|
||||
$this->getAdapter()->commitTransaction();
|
||||
}
|
||||
$end = microtime(true);
|
||||
|
||||
$this->output->writeln(' ==' . ' <info>' . $seed->getName() . ':</info>' . ' <comment>seeded' . ' ' . sprintf('%.4fs', $end - $start) . '</comment>');
|
||||
}
|
||||
}
|
||||
33
vendor/topthink/think-migration/src/command/stubs/migrate.stub
vendored
Normal file
33
vendor/topthink/think-migration/src/command/stubs/migrate.stub
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use think\migration\Migrator;
|
||||
use think\migration\db\Column;
|
||||
|
||||
class $className extends Migrator
|
||||
{
|
||||
/**
|
||||
* Change Method.
|
||||
*
|
||||
* Write your reversible migrations using this method.
|
||||
*
|
||||
* More information on writing migrations is available here:
|
||||
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
|
||||
*
|
||||
* The following commands can be used in this method and Phinx will
|
||||
* automatically reverse them when rolling back:
|
||||
*
|
||||
* createTable
|
||||
* renameTable
|
||||
* addColumn
|
||||
* renameColumn
|
||||
* addIndex
|
||||
* addForeignKey
|
||||
*
|
||||
* Remember to call "create()" or "update()" and NOT "save()" when working
|
||||
* with the Table class.
|
||||
*/
|
||||
public function change()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
19
vendor/topthink/think-migration/src/command/stubs/seed.stub
vendored
Normal file
19
vendor/topthink/think-migration/src/command/stubs/seed.stub
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
use think\migration\Seeder;
|
||||
|
||||
class $className extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run Method.
|
||||
*
|
||||
* Write your database seeder using this method.
|
||||
*
|
||||
* More information on writing seeders is available here:
|
||||
* http://docs.phinx.org/en/latest/seeding.html
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user