From df5d5f4ccd52befe570dd8459cbbd0d7e44a2bb9 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Fri, 4 Jun 2021 18:18:33 +0200 Subject: [PATCH 1/2] Move to anonymous migrations --- src/Illuminate/Cache/Console/stubs/cache.stub | 4 ++-- .../Database/Migrations/MigrationCreator.php | 10 ++-------- .../Notifications/Console/stubs/notifications.stub | 4 ++-- src/Illuminate/Queue/Console/BatchesTableCommand.php | 10 +++------- src/Illuminate/Queue/Console/FailedTableCommand.php | 10 +++------- src/Illuminate/Queue/Console/TableCommand.php | 10 +++------- src/Illuminate/Queue/Console/stubs/batches.stub | 4 ++-- src/Illuminate/Queue/Console/stubs/failed_jobs.stub | 4 ++-- src/Illuminate/Queue/Console/stubs/jobs.stub | 4 ++-- src/Illuminate/Session/Console/stubs/database.stub | 4 ++-- 10 files changed, 23 insertions(+), 41 deletions(-) diff --git a/src/Illuminate/Cache/Console/stubs/cache.stub b/src/Illuminate/Cache/Console/stubs/cache.stub index 7b73e5fd1a69..bb49432fe4a1 100644 --- a/src/Illuminate/Cache/Console/stubs/cache.stub +++ b/src/Illuminate/Cache/Console/stubs/cache.stub @@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class CreateCacheTable extends Migration +return new class extends Migration { /** * Run the migrations. @@ -29,4 +29,4 @@ class CreateCacheTable extends Migration { Schema::dropIfExists('cache'); } -} +}; diff --git a/src/Illuminate/Database/Migrations/MigrationCreator.php b/src/Illuminate/Database/Migrations/MigrationCreator.php index 78223f7dff4d..d8f1ce9d0b1e 100755 --- a/src/Illuminate/Database/Migrations/MigrationCreator.php +++ b/src/Illuminate/Database/Migrations/MigrationCreator.php @@ -68,7 +68,7 @@ public function create($name, $path, $table = null, $create = false) $this->files->ensureDirectoryExists(dirname($path)); $this->files->put( - $path, $this->populateStub($name, $stub, $table) + $path, $this->populateStub($stub, $table) ); // Next, we will fire any hooks that are supposed to fire after a migration is @@ -132,18 +132,12 @@ protected function getStub($table, $create) /** * Populate the place-holders in the migration stub. * - * @param string $name * @param string $stub * @param string|null $table * @return string */ - protected function populateStub($name, $stub, $table) + protected function populateStub($stub, $table) { - $stub = str_replace( - ['DummyClass', '{{ class }}', '{{class}}'], - $this->getClassName($name), $stub - ); - // Here we will replace the table place-holders with the table specified by // the developer, which is useful for quickly creating a tables creation // or update migration from the console instead of typing it manually. diff --git a/src/Illuminate/Notifications/Console/stubs/notifications.stub b/src/Illuminate/Notifications/Console/stubs/notifications.stub index 9797596dc4ce..4357c9efa5dc 100644 --- a/src/Illuminate/Notifications/Console/stubs/notifications.stub +++ b/src/Illuminate/Notifications/Console/stubs/notifications.stub @@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class CreateNotificationsTable extends Migration +return new class extends Migration { /** * Run the migrations. @@ -32,4 +32,4 @@ class CreateNotificationsTable extends Migration { Schema::dropIfExists('notifications'); } -} +}; diff --git a/src/Illuminate/Queue/Console/BatchesTableCommand.php b/src/Illuminate/Queue/Console/BatchesTableCommand.php index 86c9d928fb90..01cb784e1d76 100644 --- a/src/Illuminate/Queue/Console/BatchesTableCommand.php +++ b/src/Illuminate/Queue/Console/BatchesTableCommand.php @@ -5,7 +5,6 @@ use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Composer; -use Illuminate\Support\Str; class BatchesTableCommand extends Command { @@ -69,7 +68,7 @@ public function handle() $table = $this->laravel['config']['queue.batching.table'] ?? 'job_batches'; $this->replaceMigration( - $this->createBaseMigration($table), $table, Str::studly($table) + $this->createBaseMigration($table), $table ); $this->info('Migration created successfully!'); @@ -95,15 +94,12 @@ protected function createBaseMigration($table = 'failed_jobs') * * @param string $path * @param string $table - * @param string $tableClassName * @return void */ - protected function replaceMigration($path, $table, $tableClassName) + protected function replaceMigration($path, $table) { $stub = str_replace( - ['{{table}}', '{{tableClassName}}'], - [$table, $tableClassName], - $this->files->get(__DIR__.'/stubs/batches.stub') + '{{table}}', $table, $this->files->get(__DIR__.'/stubs/batches.stub') ); $this->files->put($path, $stub); diff --git a/src/Illuminate/Queue/Console/FailedTableCommand.php b/src/Illuminate/Queue/Console/FailedTableCommand.php index aab84d21a5dd..8cb20bc4c7d3 100644 --- a/src/Illuminate/Queue/Console/FailedTableCommand.php +++ b/src/Illuminate/Queue/Console/FailedTableCommand.php @@ -5,7 +5,6 @@ use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Composer; -use Illuminate\Support\Str; class FailedTableCommand extends Command { @@ -69,7 +68,7 @@ public function handle() $table = $this->laravel['config']['queue.failed.table']; $this->replaceMigration( - $this->createBaseMigration($table), $table, Str::studly($table) + $this->createBaseMigration($table), $table ); $this->info('Migration created successfully!'); @@ -95,15 +94,12 @@ protected function createBaseMigration($table = 'failed_jobs') * * @param string $path * @param string $table - * @param string $tableClassName * @return void */ - protected function replaceMigration($path, $table, $tableClassName) + protected function replaceMigration($path, $table) { $stub = str_replace( - ['{{table}}', '{{tableClassName}}'], - [$table, $tableClassName], - $this->files->get(__DIR__.'/stubs/failed_jobs.stub') + '{{table}}', $table, $this->files->get(__DIR__.'/stubs/failed_jobs.stub') ); $this->files->put($path, $stub); diff --git a/src/Illuminate/Queue/Console/TableCommand.php b/src/Illuminate/Queue/Console/TableCommand.php index 3a083d487ccf..dfdb3615730f 100644 --- a/src/Illuminate/Queue/Console/TableCommand.php +++ b/src/Illuminate/Queue/Console/TableCommand.php @@ -5,7 +5,6 @@ use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Composer; -use Illuminate\Support\Str; class TableCommand extends Command { @@ -69,7 +68,7 @@ public function handle() $table = $this->laravel['config']['queue.connections.database.table']; $this->replaceMigration( - $this->createBaseMigration($table), $table, Str::studly($table) + $this->createBaseMigration($table), $table ); $this->info('Migration created successfully!'); @@ -95,15 +94,12 @@ protected function createBaseMigration($table = 'jobs') * * @param string $path * @param string $table - * @param string $tableClassName * @return void */ - protected function replaceMigration($path, $table, $tableClassName) + protected function replaceMigration($path, $table) { $stub = str_replace( - ['{{table}}', '{{tableClassName}}'], - [$table, $tableClassName], - $this->files->get(__DIR__.'/stubs/jobs.stub') + '{{table}}', $table, $this->files->get(__DIR__.'/stubs/jobs.stub') ); $this->files->put($path, $stub); diff --git a/src/Illuminate/Queue/Console/stubs/batches.stub b/src/Illuminate/Queue/Console/stubs/batches.stub index d4fa380c7624..60be4d2b2ef1 100644 --- a/src/Illuminate/Queue/Console/stubs/batches.stub +++ b/src/Illuminate/Queue/Console/stubs/batches.stub @@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class Create{{tableClassName}}Table extends Migration +return new class extends Migration { /** * Run the migrations. @@ -36,4 +36,4 @@ class Create{{tableClassName}}Table extends Migration { Schema::dropIfExists('{{table}}'); } -} +}; diff --git a/src/Illuminate/Queue/Console/stubs/failed_jobs.stub b/src/Illuminate/Queue/Console/stubs/failed_jobs.stub index 6179e7f2f378..c804dd6d6e2e 100644 --- a/src/Illuminate/Queue/Console/stubs/failed_jobs.stub +++ b/src/Illuminate/Queue/Console/stubs/failed_jobs.stub @@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class Create{{tableClassName}}Table extends Migration +return new class extends Migration { /** * Run the migrations. @@ -33,4 +33,4 @@ class Create{{tableClassName}}Table extends Migration { Schema::dropIfExists('{{table}}'); } -} +}; diff --git a/src/Illuminate/Queue/Console/stubs/jobs.stub b/src/Illuminate/Queue/Console/stubs/jobs.stub index 45d6031de37a..5ea6d0cb39c3 100644 --- a/src/Illuminate/Queue/Console/stubs/jobs.stub +++ b/src/Illuminate/Queue/Console/stubs/jobs.stub @@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class Create{{tableClassName}}Table extends Migration +return new class extends Migration { /** * Run the migrations. @@ -33,4 +33,4 @@ class Create{{tableClassName}}Table extends Migration { Schema::dropIfExists('{{table}}'); } -} +}; diff --git a/src/Illuminate/Session/Console/stubs/database.stub b/src/Illuminate/Session/Console/stubs/database.stub index 88b4a316e6cd..c455537a9cc0 100755 --- a/src/Illuminate/Session/Console/stubs/database.stub +++ b/src/Illuminate/Session/Console/stubs/database.stub @@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class CreateSessionsTable extends Migration +return new class extends Migration { /** * Run the migrations. @@ -32,4 +32,4 @@ class CreateSessionsTable extends Migration { Schema::dropIfExists('sessions'); } -} +}; From 222a537ad2d7d92fed7c09394879972e0177db8a Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Fri, 4 Jun 2021 18:28:17 +0200 Subject: [PATCH 2/2] Fix tests --- tests/Database/DatabaseMigrationCreatorTest.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/Database/DatabaseMigrationCreatorTest.php b/tests/Database/DatabaseMigrationCreatorTest.php index 491be54c3eba..2b856ac74bbe 100755 --- a/tests/Database/DatabaseMigrationCreatorTest.php +++ b/tests/Database/DatabaseMigrationCreatorTest.php @@ -21,9 +21,9 @@ public function testBasicCreateMethodStoresMigrationFile() $creator->expects($this->any())->method('getDatePrefix')->willReturn('foo'); $creator->getFilesystem()->shouldReceive('exists')->once()->with('stubs/migration.stub')->andReturn(false); - $creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.stub')->andReturn('DummyClass'); + $creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.stub')->andReturn('return new class'); $creator->getFilesystem()->shouldReceive('ensureDirectoryExists')->once()->with('foo'); - $creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'CreateBar'); + $creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'return new class'); $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'); @@ -44,9 +44,9 @@ public function testBasicCreateMethodCallsPostCreateHooks() $creator->expects($this->any())->method('getDatePrefix')->willReturn('foo'); $creator->getFilesystem()->shouldReceive('exists')->once()->with('stubs/migration.update.stub')->andReturn(false); - $creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.update.stub')->andReturn('DummyClass DummyTable'); + $creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.update.stub')->andReturn('return new class DummyTable'); $creator->getFilesystem()->shouldReceive('ensureDirectoryExists')->once()->with('foo'); - $creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'CreateBar baz'); + $creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'return new class 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'); @@ -64,9 +64,9 @@ public function testTableUpdateMigrationStoresMigrationFile() $creator = $this->getCreator(); $creator->expects($this->any())->method('getDatePrefix')->willReturn('foo'); $creator->getFilesystem()->shouldReceive('exists')->once()->with('stubs/migration.update.stub')->andReturn(false); - $creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.update.stub')->andReturn('DummyClass DummyTable'); + $creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.update.stub')->andReturn('return new class DummyTable'); $creator->getFilesystem()->shouldReceive('ensureDirectoryExists')->once()->with('foo'); - $creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'CreateBar baz'); + $creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'return new class 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'); @@ -78,9 +78,9 @@ public function testTableCreationMigrationStoresMigrationFile() $creator = $this->getCreator(); $creator->expects($this->any())->method('getDatePrefix')->willReturn('foo'); $creator->getFilesystem()->shouldReceive('exists')->once()->with('stubs/migration.create.stub')->andReturn(false); - $creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.create.stub')->andReturn('DummyClass DummyTable'); + $creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.create.stub')->andReturn('return new class DummyTable'); $creator->getFilesystem()->shouldReceive('ensureDirectoryExists')->once()->with('foo'); - $creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'CreateBar baz'); + $creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'return new class 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');