Skip to content

Commit 9189f41

Browse files
authored
Merge pull request #2 from netsells/feature/handle-initial-migration
feature/handle-initial-migration
2 parents eae7bde + 92a4e46 commit 9189f41

File tree

5 files changed

+15
-7
lines changed

5 files changed

+15
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Optionally publish the package config file:
1717

1818
## Usage
1919

20-
Before a mutex migration can be run using the default `database` store, the store's `cache_locks` table **must** already have been created by running `php artisan cache:table` followed by a standard migration - i.e. `php artisan migrate`. Once this table exists migrations can be run safely as follows:
20+
Running a mutex migration using the default `database` store requires the existence of a table to store cache locks. If it does not exist the command will automatically fallback to a standard migration.
2121

2222
`php artisan migrate --mutex`
2323

src/MigrateCommandExtension.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Database\Console\Migrations\MigrateCommand;
77
use Illuminate\Database\Migrations\Migrator;
88
use Illuminate\Support\Collection;
9+
use Netsells\LaravelMutexMigrations\Mutex\DatabaseCacheTableNotFoundException;
910

1011
class MigrateCommandExtension extends MigrateCommand
1112
{
@@ -19,7 +20,11 @@ public function __construct(Migrator $migrator, Dispatcher $dispatcher)
1920
public function handle(): int
2021
{
2122
if ($this->option(MutexMigrateCommand::OPTION_MUTEX)) {
22-
return $this->call(MutexMigrateCommand::class, $this->getCommandOptions());
23+
try {
24+
return $this->call(MutexMigrateCommand::class, $this->getCommandOptions());
25+
} catch (DatabaseCacheTableNotFoundException $e) {
26+
$this->components->warn('Falling back to a standard migration');
27+
}
2328
}
2429

2530
return parent::handle();

src/Mutex/MutexRelay.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Netsells\LaravelMutexMigrations\Mutex;
44

55
use Illuminate\Contracts\Cache\Lock;
6-
use Illuminate\Contracts\Cache\LockProvider;
76
use Illuminate\Contracts\Cache\Repository;
87
use Illuminate\Database\QueryException;
98
use Illuminate\Support\Str;
@@ -48,10 +47,10 @@ private function getLock(): Lock
4847
return $this->lock;
4948
}
5049

51-
/** @var LockProvider $provider */
52-
$provider = $this->cache->getStore();
50+
/** @var \Illuminate\Contracts\Cache\LockProvider $store */
51+
$store = $this->cache->getStore();
5352

54-
return $this->lock = $provider->lock(self::KEY . '.lock');
53+
return $this->lock = $store->lock(self::KEY . '.lock');
5554
}
5655

5756
private function isCacheTableNotFoundException(\Throwable $th): bool

src/MutexMigrateCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function handle()
4242
} catch (DatabaseCacheTableNotFoundException $e) {
4343
$this->components->error($e->getMessage());
4444

45-
return self::FAILURE;
45+
throw $e;
4646
} finally {
4747
$this->processor->terminate();
4848
}

src/Processors/MutexMigrationProcessor.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public function start(): void
3737

3838
public function terminate(): void
3939
{
40+
if ($this->relay instanceof NullRelay) {
41+
return;
42+
}
43+
4044
if (! $this->relay->releaseLock()) {
4145
$this->components->info('The mutex lock was not released');
4246

0 commit comments

Comments
 (0)