Fixed bug that migration will be created although class already exists. (#1952)

* Fix same migration file class name

* show gen:migration command description

* Fix test

* Fix test

* Update CHANGELOG-2.0.md

Co-authored-by: 李铭昕 <715557344@qq.com>
This commit is contained in:
Kids Return 2020-06-23 16:39:11 +08:00 committed by GitHub
parent b4b6522ac1
commit e831fe0a93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 9 deletions

View File

@ -2,6 +2,7 @@
## Fixed
- [#1952](https://github.com/hyperf/hyperf/pull/1952) Fixed bug that migration will be created although class already exists.
- [#1960](https://github.com/hyperf/hyperf/pull/1960) Fixed `Hyperf\HttpServer\ResponseEmitter::isMethodsExists()` method does not works as expected.
- [#1961](https://github.com/hyperf/hyperf/pull/1961) Fixed start failed when `config/autoload/aspects.php` does not exists.
- [#1964](https://github.com/hyperf/hyperf/pull/1964) Fixed http status code 500 caused by empty body.

View File

@ -18,13 +18,6 @@ use Symfony\Component\Console\Input\InputOption;
class GenMigrateCommand extends BaseCommand
{
/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate a new migration file';
/**
* The migration creator instance.
*
@ -38,6 +31,7 @@ class GenMigrateCommand extends BaseCommand
public function __construct(MigrationCreator $creator)
{
parent::__construct('gen:migration');
$this->setDescription('Generate a new migration file');
$this->creator = $creator;
}

View File

@ -47,7 +47,7 @@ class MigrationCreator
*/
public function create(string $name, string $path, string $table = null, bool $create = false): string
{
$this->ensureMigrationDoesntAlreadyExist($name);
$this->ensureMigrationDoesntAlreadyExist($name, $path);
// First we will get the stub file for the migration, which serves as a type
// of template for the migration. Once we have those we will populate the
@ -100,8 +100,16 @@ class MigrationCreator
*
* @throws \InvalidArgumentException
*/
protected function ensureMigrationDoesntAlreadyExist(string $name)
protected function ensureMigrationDoesntAlreadyExist(string $name, ?string $migrationPath = null)
{
if (! empty($migrationPath)) {
$migrationFiles = $this->files->glob($migrationPath.'/*.php');
foreach ($migrationFiles as $migrationFile) {
$this->files->requireOnce($migrationFile);
}
}
if (class_exists($className = $this->getClassName($name))) {
throw new InvalidArgumentException("A {$className} class already exists.");
}

View File

@ -38,6 +38,8 @@ class DatabaseMigrationCreatorTest extends TestCase
$creator->expects($this->any())->method('getDatePrefix')->will($this->returnValue('foo'));
$creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath() . '/blank.stub')->andReturn('DummyClass');
$creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'CreateBar');
$creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/*.php')->andReturn(['foo/foo_create_bar.php']);
$creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/foo_create_bar.php');
$creator->create('create_bar', 'foo');
}
@ -55,6 +57,8 @@ class DatabaseMigrationCreatorTest extends TestCase
$creator->expects($this->any())->method('getDatePrefix')->will($this->returnValue('foo'));
$creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath() . '/update.stub')->andReturn('DummyClass DummyTable');
$creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'CreateBar baz');
$creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/*.php')->andReturn(['foo/foo_create_bar.php']);
$creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/foo_create_bar.php');
$creator->create('create_bar', 'foo', $table);
@ -69,6 +73,8 @@ class DatabaseMigrationCreatorTest extends TestCase
$creator->expects($this->any())->method('getDatePrefix')->will($this->returnValue('foo'));
$creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath() . '/update.stub')->andReturn('DummyClass DummyTable');
$creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'CreateBar baz');
$creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/*.php')->andReturn(['foo/foo_create_bar.php']);
$creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/foo_create_bar.php');
$creator->create('create_bar', 'foo', 'baz');
}
@ -79,6 +85,8 @@ class DatabaseMigrationCreatorTest extends TestCase
$creator->expects($this->any())->method('getDatePrefix')->will($this->returnValue('foo'));
$creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath() . '/create.stub')->andReturn('DummyClass DummyTable');
$creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'CreateBar baz');
$creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/*.php')->andReturn(['foo/foo_create_bar.php']);
$creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/foo_create_bar.php');
$creator->create('create_bar', 'foo', 'baz', true);
}
@ -90,6 +98,8 @@ class DatabaseMigrationCreatorTest extends TestCase
$creator = $this->getCreator();
$creator->getFilesystem()->shouldReceive('get');
$creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/*.php')->andReturn(['foo/foo_create_bar.php']);
$creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/foo_create_bar.php');
$creator->create(MigrationCreatorFakeMigration::class, 'foo');
}