修复seeder错误;删除第三方的think迁移工具,在extend中重新实现;

This commit is contained in:
2023-09-20 15:35:20 +08:00
parent 0b23e0a940
commit 85f2f29b81
91 changed files with 20798 additions and 66 deletions

View 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>php think migrate:breakpoint</info>
<info>php think migrate:breakpoint -t 20110103081132</info>
<info>php think migrate: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()));
}
}

View File

@@ -0,0 +1,55 @@
<?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 InvalidArgumentException;
use RuntimeException;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument as InputArgument;
use think\console\Output;
use think\migration\Creator;
class Create extends Command
{
/**
* {@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
* @return void
* @throws InvalidArgumentException
* @throws RuntimeException
*/
protected function execute(Input $input, Output $output)
{
/** @var Creator $creator */
$creator = $this->app->get('migration.creator');
$className = $input->getArgument('name');
$path = $creator->create($className);
$output->writeln('<info>created</info> .' . str_replace(getcwd(), '', realpath($path)));
}
}

View 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 think migrate:rollback</info>
<info>php think migrate:rollback -t 20111018185412</info>
<info>php think migrate:rollback -d 20111018</info>
<info>php think 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);
}
}
}

View File

@@ -0,0 +1,140 @@
<?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 think migrate:run</info>
<info>php think migrate:run -t 20110103081132</info>
<info>php think migrate:run -d 20110103</info>
<info>php think migrate:run -v</info>
EOT
);
}
/**
* Migrate the database.
*
* @param Input $input
* @param Output $output
*/
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;
}
}

View File

@@ -0,0 +1,126 @@
<?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 think migrate:status</info>
<info>php think 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> ';
$version = [];
$version['start_time'] = $version['end_time'] = $version['breakpoint'] = '';
}
$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>');
}
}
}
}