diff --git a/.gitignore b/.gitignore index bbf528a..6482272 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .phpunit.result.cache -vendor -.idea \ No newline at end of file + +/.idea +/vendor diff --git a/database/migrations/2023_02_28_000000_create_one_time_operations_table.php b/database/migrations/2023_02_28_000000_create_one_time_operations_table.php index 9cc247f..f68892c 100644 --- a/database/migrations/2023_02_28_000000_create_one_time_operations_table.php +++ b/database/migrations/2023_02_28_000000_create_one_time_operations_table.php @@ -6,18 +6,10 @@ use TimoKoerber\LaravelOneTimeOperations\Models\Operation; use TimoKoerber\LaravelOneTimeOperations\OneTimeOperationManager; -class CreateOneTimeOperationsTable extends Migration -{ - protected string $name; - - public function __construct() - { - $this->name = OneTimeOperationManager::getTableName(); - } - +return new class () extends Migration { public function up() { - Schema::create($this->name, function (Blueprint $table) { + Schema::create(OneTimeOperationManager::getTableName(), function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->enum('dispatched', [Operation::DISPATCHED_SYNC, Operation::DISPATCHED_ASYNC]); @@ -27,6 +19,6 @@ public function up() public function down() { - Schema::dropIfExists($this->name); + Schema::dropIfExists(OneTimeOperationManager::getTableName()); } -} +}; diff --git a/src/Providers/OneTimeOperationsServiceProvider.php b/src/Providers/OneTimeOperationsServiceProvider.php index 74c0d6d..7d57e5c 100644 --- a/src/Providers/OneTimeOperationsServiceProvider.php +++ b/src/Providers/OneTimeOperationsServiceProvider.php @@ -2,6 +2,7 @@ namespace TimoKoerber\LaravelOneTimeOperations\Providers; +use Illuminate\Filesystem\Filesystem; use Illuminate\Support\ServiceProvider; use TimoKoerber\LaravelOneTimeOperations\Commands\OneTimeOperationShowCommand; use TimoKoerber\LaravelOneTimeOperations\Commands\OneTimeOperationsMakeCommand; @@ -11,16 +12,24 @@ class OneTimeOperationsServiceProvider extends ServiceProvider { public function boot(): void { - $this->loadMigrationsFrom([__DIR__.'/../../database/migrations']); + if (! $this->app->runningInConsole()) { + return; + } + + $this->commands(OneTimeOperationsMakeCommand::class); + $this->commands(OneTimeOperationsProcessCommand::class); + $this->commands(OneTimeOperationShowCommand::class); $this->publishes([ __DIR__.'/../../config/one-time-operations.php' => config_path('one-time-operations.php'), - ]); + ], 'one-time-operations-config'); + + $this->publishes([ + __DIR__.'/../../database/migrations/2023_02_28_000000_create_one_time_operations_table.php' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_one_time_operations_table.php'), + ], 'one-time-operations-migrations'); - if ($this->app->runningInConsole()) { - $this->commands(OneTimeOperationsMakeCommand::class); - $this->commands(OneTimeOperationsProcessCommand::class); - $this->commands(OneTimeOperationShowCommand::class); + if (! $this->migrationFileExists()) { + $this->loadMigrationsFrom([__DIR__.'/../../database/migrations']); } } @@ -30,4 +39,16 @@ public function register() __DIR__.'/../../config/one-time-operations.php', 'one-time-operations' ); } + + protected function migrationFileExists(): bool + { + $files = $this->app->make(Filesystem::class)->glob(sprintf( + '%s%s%s', + database_path('migrations'), + DIRECTORY_SEPARATOR, + '*_create_one_time_operations_table.php' + )); + + return count($files) > 0; + } }