Skip to content

Commit

Permalink
[10.x] Prevent session:table command from creating duplicates (#48602)
Browse files Browse the repository at this point in the history
* Prevent `session:table` command from creating duplicates

* Fix `session:table` test mock

* Fix Windows paths

* Update SessionTableCommand.php

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
jessarcher and taylorotwell authored Oct 2, 2023
1 parent 388c835 commit cd68b64
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Illuminate/Session/Console/SessionTableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public function __construct(Filesystem $files, Composer $composer)
*/
public function handle()
{
if ($this->migrationExists()) {
$this->components->error('Migration already exists.');

return 1;
}

$fullPath = $this->createBaseMigration();

$this->files->put($fullPath, $this->files->get(__DIR__.'/stubs/database.stub'));
Expand All @@ -80,4 +86,16 @@ protected function createBaseMigration()

return $this->laravel['migration.creator']->create($name, $path);
}

/**
* Determine whether the session table migration already exists.
*
* @return bool
*/
protected function migrationExists()
{
return count($this->files->glob(
$this->laravel->joinPaths($this->laravel->databasePath('migrations'), '*_*_*_*_create_sessions_table.php')
)) !== 0;
}
}
1 change: 1 addition & 0 deletions tests/Session/SessionTableCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function testCreateMakesMigration()
$command->setLaravel($app);
$path = __DIR__.'/migrations';
$creator->shouldReceive('create')->once()->with('create_sessions_table', $path)->andReturn($path);
$files->shouldReceive('glob')->once()->with($app->joinPaths($app->databasePath('migrations'), '*_*_*_*_create_sessions_table.php'))->andReturn([]);
$files->shouldReceive('get')->once()->andReturn('foo');
$files->shouldReceive('put')->once()->with($path, 'foo');

Expand Down

0 comments on commit cd68b64

Please sign in to comment.