Skip to content

Publish migrations #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.phpunit.result.cache
vendor
.idea

/.idea
/vendor
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -27,6 +19,6 @@ public function up()

public function down()
{
Schema::dropIfExists($this->name);
Schema::dropIfExists(OneTimeOperationManager::getTableName());
}
}
};
33 changes: 27 additions & 6 deletions src/Providers/OneTimeOperationsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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']);
}
}

Expand All @@ -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;
}
}