-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
migrate
command isolated (#44743)
* Add Isolated interface to `migrate` command * Add CommandMutex with cache implementation * Remove typehints in favor of docblocks * Apply StyleCI * Add support for releasing lock again * fix db migrate command tests * cleanup * Add `--isolated` flag to command * rename file. formatting * allow exit code * fix option * fix order Co-authored-by: Taylor Otwell <taylor@laravel.com>
- Loading branch information
1 parent
4d126e8
commit c2798fd
Showing
8 changed files
with
374 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
namespace Illuminate\Console; | ||
|
||
use Carbon\CarbonInterval; | ||
use Illuminate\Contracts\Cache\Factory as Cache; | ||
|
||
class CacheCommandMutex implements CommandMutex | ||
{ | ||
/** | ||
* The cache factory implementation. | ||
* | ||
* @var \Illuminate\Contracts\Cache\Factory | ||
*/ | ||
public $cache; | ||
|
||
/** | ||
* The cache store that should be used. | ||
* | ||
* @var string|null | ||
*/ | ||
public $store = null; | ||
|
||
/** | ||
* Create a new command mutex. | ||
* | ||
* @param \Illuminate\Contracts\Cache\Factory $cache | ||
*/ | ||
public function __construct(Cache $cache) | ||
{ | ||
$this->cache = $cache; | ||
} | ||
|
||
/** | ||
* Attempt to obtain a command mutex for the given command. | ||
* | ||
* @param \Illuminate\Console\Command $command | ||
* @return bool | ||
*/ | ||
public function create($command) | ||
{ | ||
return $this->cache->store($this->store)->add( | ||
$this->commandMutexName($command), | ||
true, | ||
method_exists($command, 'isolationExpiresAt') | ||
? $command->isolationExpiresAt() | ||
: CarbonInterval::hour(), | ||
); | ||
} | ||
|
||
/** | ||
* Determine if a command mutex exists for the given command. | ||
* | ||
* @param \Illuminate\Console\Command $command | ||
* @return bool | ||
*/ | ||
public function exists($command) | ||
{ | ||
return $this->cache->store($this->store)->has( | ||
$this->commandMutexName($command) | ||
); | ||
} | ||
|
||
/** | ||
* Release the mutex for the given command. | ||
* | ||
* @param \Illuminate\Console\Command $command | ||
* @return bool | ||
*/ | ||
public function forget($command) | ||
{ | ||
return $this->cache->store($this->store)->forget( | ||
$this->commandMutexName($command) | ||
); | ||
} | ||
|
||
/** | ||
* @param \Illuminate\Console\Command $command | ||
* @return string | ||
*/ | ||
protected function commandMutexName($command) | ||
{ | ||
return 'framework'.DIRECTORY_SEPARATOR.'command-'.$command->getName(); | ||
} | ||
|
||
/** | ||
* Specify the cache store that should be used. | ||
* | ||
* @param string|null $store | ||
* @return $this | ||
*/ | ||
public function useStore($store) | ||
{ | ||
$this->store = $store; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Illuminate\Console; | ||
|
||
interface CommandMutex | ||
{ | ||
/** | ||
* Attempt to obtain a command mutex for the given command. | ||
* | ||
* @param \Illuminate\Console\Command $command | ||
* @return bool | ||
*/ | ||
public function create($command); | ||
|
||
/** | ||
* Determine if a command mutex exists for the given command. | ||
* | ||
* @param \Illuminate\Console\Command $command | ||
* @return bool | ||
*/ | ||
public function exists($command); | ||
|
||
/** | ||
* Release the mutex for the given command. | ||
* | ||
* @param \Illuminate\Console\Command $command | ||
* @return bool | ||
*/ | ||
public function forget($command); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Illuminate\Contracts\Console; | ||
|
||
interface Isolatable | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Console; | ||
|
||
use Illuminate\Console\CacheCommandMutex; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Contracts\Cache\Factory; | ||
use Illuminate\Contracts\Cache\Repository; | ||
use Mockery as m; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class CacheCommandMutexTest extends TestCase | ||
{ | ||
/** | ||
* @var \Illuminate\Console\CacheCommandMutex | ||
*/ | ||
protected $mutex; | ||
|
||
/** | ||
* @var \Illuminate\Console\Command | ||
*/ | ||
protected $command; | ||
|
||
/** | ||
* @var \Illuminate\Contracts\Cache\Factory | ||
*/ | ||
protected $cacheFactory; | ||
|
||
/** | ||
* @var \Illuminate\Contracts\Cache\Repository | ||
*/ | ||
protected $cacheRepository; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->cacheFactory = m::mock(Factory::class); | ||
$this->cacheRepository = m::mock(Repository::class); | ||
$this->cacheFactory->shouldReceive('store')->andReturn($this->cacheRepository); | ||
$this->mutex = new CacheCommandMutex($this->cacheFactory); | ||
$this->command = new class extends Command | ||
{ | ||
protected $name = 'command-name'; | ||
}; | ||
} | ||
|
||
public function testCanCreateMutex() | ||
{ | ||
$this->cacheRepository->shouldReceive('add') | ||
->andReturn(true) | ||
->once(); | ||
$actual = $this->mutex->create($this->command); | ||
|
||
$this->assertTrue($actual); | ||
} | ||
|
||
public function testCannotCreateMutexIfAlreadyExist() | ||
{ | ||
$this->cacheRepository->shouldReceive('add') | ||
->andReturn(false) | ||
->once(); | ||
$actual = $this->mutex->create($this->command); | ||
|
||
$this->assertFalse($actual); | ||
} | ||
|
||
public function testCanCreateMutexWithCustomConnection() | ||
{ | ||
$this->cacheRepository->shouldReceive('getStore') | ||
->with('test') | ||
->andReturn($this->cacheRepository); | ||
$this->cacheRepository->shouldReceive('add') | ||
->andReturn(false) | ||
->once(); | ||
$this->mutex->useStore('test'); | ||
|
||
$this->mutex->create($this->command); | ||
} | ||
} |
Oops, something went wrong.