Skip to content

Commit 95b1899

Browse files
authored
[9.x] Move to anonymous migrations (#37598)
* Move to anonymous migrations * Fix tests
1 parent be221e5 commit 95b1899

File tree

11 files changed

+31
-49
lines changed

11 files changed

+31
-49
lines changed

src/Illuminate/Cache/Console/stubs/cache.stub

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration;
44
use Illuminate\Database\Schema\Blueprint;
55
use Illuminate\Support\Facades\Schema;
66

7-
class CreateCacheTable extends Migration
7+
return new class extends Migration
88
{
99
/**
1010
* Run the migrations.
@@ -29,4 +29,4 @@ class CreateCacheTable extends Migration
2929
{
3030
Schema::dropIfExists('cache');
3131
}
32-
}
32+
};

src/Illuminate/Database/Migrations/MigrationCreator.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function create($name, $path, $table = null, $create = false)
6868
$this->files->ensureDirectoryExists(dirname($path));
6969

7070
$this->files->put(
71-
$path, $this->populateStub($name, $stub, $table)
71+
$path, $this->populateStub($stub, $table)
7272
);
7373

7474
// Next, we will fire any hooks that are supposed to fire after a migration is
@@ -132,18 +132,12 @@ protected function getStub($table, $create)
132132
/**
133133
* Populate the place-holders in the migration stub.
134134
*
135-
* @param string $name
136135
* @param string $stub
137136
* @param string|null $table
138137
* @return string
139138
*/
140-
protected function populateStub($name, $stub, $table)
139+
protected function populateStub($stub, $table)
141140
{
142-
$stub = str_replace(
143-
['DummyClass', '{{ class }}', '{{class}}'],
144-
$this->getClassName($name), $stub
145-
);
146-
147141
// Here we will replace the table place-holders with the table specified by
148142
// the developer, which is useful for quickly creating a tables creation
149143
// or update migration from the console instead of typing it manually.

src/Illuminate/Notifications/Console/stubs/notifications.stub

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration;
44
use Illuminate\Database\Schema\Blueprint;
55
use Illuminate\Support\Facades\Schema;
66

7-
class CreateNotificationsTable extends Migration
7+
return new class extends Migration
88
{
99
/**
1010
* Run the migrations.
@@ -32,4 +32,4 @@ class CreateNotificationsTable extends Migration
3232
{
3333
Schema::dropIfExists('notifications');
3434
}
35-
}
35+
};

src/Illuminate/Queue/Console/BatchesTableCommand.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Illuminate\Console\Command;
66
use Illuminate\Filesystem\Filesystem;
77
use Illuminate\Support\Composer;
8-
use Illuminate\Support\Str;
98

109
class BatchesTableCommand extends Command
1110
{
@@ -69,7 +68,7 @@ public function handle()
6968
$table = $this->laravel['config']['queue.batching.table'] ?? 'job_batches';
7069

7170
$this->replaceMigration(
72-
$this->createBaseMigration($table), $table, Str::studly($table)
71+
$this->createBaseMigration($table), $table
7372
);
7473

7574
$this->info('Migration created successfully!');
@@ -95,15 +94,12 @@ protected function createBaseMigration($table = 'failed_jobs')
9594
*
9695
* @param string $path
9796
* @param string $table
98-
* @param string $tableClassName
9997
* @return void
10098
*/
101-
protected function replaceMigration($path, $table, $tableClassName)
99+
protected function replaceMigration($path, $table)
102100
{
103101
$stub = str_replace(
104-
['{{table}}', '{{tableClassName}}'],
105-
[$table, $tableClassName],
106-
$this->files->get(__DIR__.'/stubs/batches.stub')
102+
'{{table}}', $table, $this->files->get(__DIR__.'/stubs/batches.stub')
107103
);
108104

109105
$this->files->put($path, $stub);

src/Illuminate/Queue/Console/FailedTableCommand.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Illuminate\Console\Command;
66
use Illuminate\Filesystem\Filesystem;
77
use Illuminate\Support\Composer;
8-
use Illuminate\Support\Str;
98

109
class FailedTableCommand extends Command
1110
{
@@ -69,7 +68,7 @@ public function handle()
6968
$table = $this->laravel['config']['queue.failed.table'];
7069

7170
$this->replaceMigration(
72-
$this->createBaseMigration($table), $table, Str::studly($table)
71+
$this->createBaseMigration($table), $table
7372
);
7473

7574
$this->info('Migration created successfully!');
@@ -95,15 +94,12 @@ protected function createBaseMigration($table = 'failed_jobs')
9594
*
9695
* @param string $path
9796
* @param string $table
98-
* @param string $tableClassName
9997
* @return void
10098
*/
101-
protected function replaceMigration($path, $table, $tableClassName)
99+
protected function replaceMigration($path, $table)
102100
{
103101
$stub = str_replace(
104-
['{{table}}', '{{tableClassName}}'],
105-
[$table, $tableClassName],
106-
$this->files->get(__DIR__.'/stubs/failed_jobs.stub')
102+
'{{table}}', $table, $this->files->get(__DIR__.'/stubs/failed_jobs.stub')
107103
);
108104

109105
$this->files->put($path, $stub);

src/Illuminate/Queue/Console/TableCommand.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Illuminate\Console\Command;
66
use Illuminate\Filesystem\Filesystem;
77
use Illuminate\Support\Composer;
8-
use Illuminate\Support\Str;
98

109
class TableCommand extends Command
1110
{
@@ -69,7 +68,7 @@ public function handle()
6968
$table = $this->laravel['config']['queue.connections.database.table'];
7069

7170
$this->replaceMigration(
72-
$this->createBaseMigration($table), $table, Str::studly($table)
71+
$this->createBaseMigration($table), $table
7372
);
7473

7574
$this->info('Migration created successfully!');
@@ -95,15 +94,12 @@ protected function createBaseMigration($table = 'jobs')
9594
*
9695
* @param string $path
9796
* @param string $table
98-
* @param string $tableClassName
9997
* @return void
10098
*/
101-
protected function replaceMigration($path, $table, $tableClassName)
99+
protected function replaceMigration($path, $table)
102100
{
103101
$stub = str_replace(
104-
['{{table}}', '{{tableClassName}}'],
105-
[$table, $tableClassName],
106-
$this->files->get(__DIR__.'/stubs/jobs.stub')
102+
'{{table}}', $table, $this->files->get(__DIR__.'/stubs/jobs.stub')
107103
);
108104

109105
$this->files->put($path, $stub);

src/Illuminate/Queue/Console/stubs/batches.stub

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration;
44
use Illuminate\Database\Schema\Blueprint;
55
use Illuminate\Support\Facades\Schema;
66

7-
class Create{{tableClassName}}Table extends Migration
7+
return new class extends Migration
88
{
99
/**
1010
* Run the migrations.
@@ -36,4 +36,4 @@ class Create{{tableClassName}}Table extends Migration
3636
{
3737
Schema::dropIfExists('{{table}}');
3838
}
39-
}
39+
};

src/Illuminate/Queue/Console/stubs/failed_jobs.stub

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration;
44
use Illuminate\Database\Schema\Blueprint;
55
use Illuminate\Support\Facades\Schema;
66

7-
class Create{{tableClassName}}Table extends Migration
7+
return new class extends Migration
88
{
99
/**
1010
* Run the migrations.
@@ -33,4 +33,4 @@ class Create{{tableClassName}}Table extends Migration
3333
{
3434
Schema::dropIfExists('{{table}}');
3535
}
36-
}
36+
};

src/Illuminate/Queue/Console/stubs/jobs.stub

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration;
44
use Illuminate\Database\Schema\Blueprint;
55
use Illuminate\Support\Facades\Schema;
66

7-
class Create{{tableClassName}}Table extends Migration
7+
return new class extends Migration
88
{
99
/**
1010
* Run the migrations.
@@ -33,4 +33,4 @@ class Create{{tableClassName}}Table extends Migration
3333
{
3434
Schema::dropIfExists('{{table}}');
3535
}
36-
}
36+
};

src/Illuminate/Session/Console/stubs/database.stub

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration;
44
use Illuminate\Database\Schema\Blueprint;
55
use Illuminate\Support\Facades\Schema;
66

7-
class CreateSessionsTable extends Migration
7+
return new class extends Migration
88
{
99
/**
1010
* Run the migrations.
@@ -32,4 +32,4 @@ class CreateSessionsTable extends Migration
3232
{
3333
Schema::dropIfExists('sessions');
3434
}
35-
}
35+
};

tests/Database/DatabaseMigrationCreatorTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public function testBasicCreateMethodStoresMigrationFile()
2121

2222
$creator->expects($this->any())->method('getDatePrefix')->willReturn('foo');
2323
$creator->getFilesystem()->shouldReceive('exists')->once()->with('stubs/migration.stub')->andReturn(false);
24-
$creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.stub')->andReturn('DummyClass');
24+
$creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.stub')->andReturn('return new class');
2525
$creator->getFilesystem()->shouldReceive('ensureDirectoryExists')->once()->with('foo');
26-
$creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'CreateBar');
26+
$creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'return new class');
2727
$creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/*.php')->andReturn(['foo/foo_create_bar.php']);
2828
$creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/foo_create_bar.php');
2929

@@ -44,9 +44,9 @@ public function testBasicCreateMethodCallsPostCreateHooks()
4444

4545
$creator->expects($this->any())->method('getDatePrefix')->willReturn('foo');
4646
$creator->getFilesystem()->shouldReceive('exists')->once()->with('stubs/migration.update.stub')->andReturn(false);
47-
$creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.update.stub')->andReturn('DummyClass DummyTable');
47+
$creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.update.stub')->andReturn('return new class DummyTable');
4848
$creator->getFilesystem()->shouldReceive('ensureDirectoryExists')->once()->with('foo');
49-
$creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'CreateBar baz');
49+
$creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'return new class baz');
5050
$creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/*.php')->andReturn(['foo/foo_create_bar.php']);
5151
$creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/foo_create_bar.php');
5252

@@ -64,9 +64,9 @@ public function testTableUpdateMigrationStoresMigrationFile()
6464
$creator = $this->getCreator();
6565
$creator->expects($this->any())->method('getDatePrefix')->willReturn('foo');
6666
$creator->getFilesystem()->shouldReceive('exists')->once()->with('stubs/migration.update.stub')->andReturn(false);
67-
$creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.update.stub')->andReturn('DummyClass DummyTable');
67+
$creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.update.stub')->andReturn('return new class DummyTable');
6868
$creator->getFilesystem()->shouldReceive('ensureDirectoryExists')->once()->with('foo');
69-
$creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'CreateBar baz');
69+
$creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'return new class baz');
7070
$creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/*.php')->andReturn(['foo/foo_create_bar.php']);
7171
$creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/foo_create_bar.php');
7272

@@ -78,9 +78,9 @@ public function testTableCreationMigrationStoresMigrationFile()
7878
$creator = $this->getCreator();
7979
$creator->expects($this->any())->method('getDatePrefix')->willReturn('foo');
8080
$creator->getFilesystem()->shouldReceive('exists')->once()->with('stubs/migration.create.stub')->andReturn(false);
81-
$creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.create.stub')->andReturn('DummyClass DummyTable');
81+
$creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.create.stub')->andReturn('return new class DummyTable');
8282
$creator->getFilesystem()->shouldReceive('ensureDirectoryExists')->once()->with('foo');
83-
$creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'CreateBar baz');
83+
$creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'return new class baz');
8484
$creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/*.php')->andReturn(['foo/foo_create_bar.php']);
8585
$creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/foo_create_bar.php');
8686

0 commit comments

Comments
 (0)