Skip to content

[9.x] Move to anonymous migrations #37598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Illuminate/Cache/Console/stubs/cache.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -29,4 +29,4 @@ class CreateCacheTable extends Migration
{
Schema::dropIfExists('cache');
}
}
};
10 changes: 2 additions & 8 deletions src/Illuminate/Database/Migrations/MigrationCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Notifications/Console/stubs/notifications.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -32,4 +32,4 @@ class CreateNotificationsTable extends Migration
{
Schema::dropIfExists('notifications');
}
}
};
10 changes: 3 additions & 7 deletions src/Illuminate/Queue/Console/BatchesTableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Composer;
use Illuminate\Support\Str;

class BatchesTableCommand extends Command
{
Expand Down Expand Up @@ -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!');
Expand All @@ -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);
Expand Down
10 changes: 3 additions & 7 deletions src/Illuminate/Queue/Console/FailedTableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Composer;
use Illuminate\Support\Str;

class FailedTableCommand extends Command
{
Expand Down Expand Up @@ -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!');
Expand All @@ -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);
Expand Down
10 changes: 3 additions & 7 deletions src/Illuminate/Queue/Console/TableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Composer;
use Illuminate\Support\Str;

class TableCommand extends Command
{
Expand Down Expand Up @@ -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!');
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Queue/Console/stubs/batches.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -36,4 +36,4 @@ class Create{{tableClassName}}Table extends Migration
{
Schema::dropIfExists('{{table}}');
}
}
};
4 changes: 2 additions & 2 deletions src/Illuminate/Queue/Console/stubs/failed_jobs.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -33,4 +33,4 @@ class Create{{tableClassName}}Table extends Migration
{
Schema::dropIfExists('{{table}}');
}
}
};
4 changes: 2 additions & 2 deletions src/Illuminate/Queue/Console/stubs/jobs.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -33,4 +33,4 @@ class Create{{tableClassName}}Table extends Migration
{
Schema::dropIfExists('{{table}}');
}
}
};
4 changes: 2 additions & 2 deletions src/Illuminate/Session/Console/stubs/database.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -32,4 +32,4 @@ class CreateSessionsTable extends Migration
{
Schema::dropIfExists('sessions');
}
}
};
16 changes: 8 additions & 8 deletions tests/Database/DatabaseMigrationCreatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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');

Expand All @@ -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');

Expand All @@ -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');

Expand Down