Skip to content

Commit

Permalink
Merge pull request #2 from netsells/feature/handle-initial-migration
Browse files Browse the repository at this point in the history
feature/handle-initial-migration
  • Loading branch information
tomchkk authored Mar 10, 2023
2 parents eae7bde + 92a4e46 commit 9189f41
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Optionally publish the package config file:

## Usage

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:
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.

`php artisan migrate --mutex`

Expand Down
7 changes: 6 additions & 1 deletion src/MigrateCommandExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Console\Migrations\MigrateCommand;
use Illuminate\Database\Migrations\Migrator;
use Illuminate\Support\Collection;
use Netsells\LaravelMutexMigrations\Mutex\DatabaseCacheTableNotFoundException;

class MigrateCommandExtension extends MigrateCommand
{
Expand All @@ -19,7 +20,11 @@ public function __construct(Migrator $migrator, Dispatcher $dispatcher)
public function handle(): int
{
if ($this->option(MutexMigrateCommand::OPTION_MUTEX)) {
return $this->call(MutexMigrateCommand::class, $this->getCommandOptions());
try {
return $this->call(MutexMigrateCommand::class, $this->getCommandOptions());
} catch (DatabaseCacheTableNotFoundException $e) {
$this->components->warn('Falling back to a standard migration');
}
}

return parent::handle();
Expand Down
7 changes: 3 additions & 4 deletions src/Mutex/MutexRelay.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Netsells\LaravelMutexMigrations\Mutex;

use Illuminate\Contracts\Cache\Lock;
use Illuminate\Contracts\Cache\LockProvider;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Database\QueryException;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -48,10 +47,10 @@ private function getLock(): Lock
return $this->lock;
}

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

return $this->lock = $provider->lock(self::KEY . '.lock');
return $this->lock = $store->lock(self::KEY . '.lock');
}

private function isCacheTableNotFoundException(\Throwable $th): bool
Expand Down
2 changes: 1 addition & 1 deletion src/MutexMigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function handle()
} catch (DatabaseCacheTableNotFoundException $e) {
$this->components->error($e->getMessage());

return self::FAILURE;
throw $e;
} finally {
$this->processor->terminate();
}
Expand Down
4 changes: 4 additions & 0 deletions src/Processors/MutexMigrationProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public function start(): void

public function terminate(): void
{
if ($this->relay instanceof NullRelay) {
return;
}

if (! $this->relay->releaseLock()) {
$this->components->info('The mutex lock was not released');

Expand Down

0 comments on commit 9189f41

Please sign in to comment.