Skip to content
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

Deferred migrations system #175

Merged
merged 1 commit into from
Feb 14, 2024
Merged
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
18 changes: 18 additions & 0 deletions src/Facades/DeferredMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Seat\Services\Facades;

use Closure;
use Illuminate\Support\Facades\Facade;
use Seat\Services\Services\DeferredMigrationRegistry;

/**
* @method static void schedule(Closure $migration)
*/
class DeferredMigration extends Facade
{
protected static function getFacadeAccessor(): string
{
return DeferredMigrationRegistry::class;
}
}
19 changes: 19 additions & 0 deletions src/Listeners/RunDeferredMigrations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Seat\Services\Listeners;

use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Database\Events\MigrationsEnded;
use Seat\Services\Services\DeferredMigrationRegistry;

class RunDeferredMigrations
{
/**
* @throws BindingResolutionException
*/
public function handle(MigrationsEnded $event) {
$registry = app()->make(DeferredMigrationRegistry::class);

$registry->runMigrations();
}
}
25 changes: 25 additions & 0 deletions src/Services/DeferredMigrationRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Seat\Services\Services;

use Closure;

class DeferredMigrationRegistry
{
/**
* @var array<Closure>
*/
protected array $deferred_migrations = [];

public function schedule(Closure $migration): void {
$this->deferred_migrations[] = $migration;
}

public function runMigrations(): void {
logger()->info(sprintf("[Deferred Migrations] Running %d deferred migrations", count($this->deferred_migrations)));

foreach ($this->deferred_migrations as $migration){
$migration();
}
}
}
17 changes: 17 additions & 0 deletions src/ServicesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@

namespace Seat\Services;

use Illuminate\Database\Events\MigrationsEnded;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Seat\Services\Commands\Seat\Admin\Email;
use Seat\Services\Commands\Seat\Version;
use Seat\Services\Contracts\DeferredMigration;
use Seat\Services\Listeners\RunDeferredMigrations;
use Seat\Services\Services\DeferredMigrationRegistry;
use Seat\Services\Services\InjectedRelationRegistry;

class ServicesServiceProvider extends AbstractSeatPlugin
Expand Down Expand Up @@ -76,6 +81,9 @@ public function boot()

// Inform Laravel how to load migrations
$this->add_migrations();

// add event listener
$this->add_event_listeners();
}

/**
Expand All @@ -92,6 +100,10 @@ public function register()
$this->app->singleton(InjectedRelationRegistry::class, function () {
return new InjectedRelationRegistry();
});

$this->app->singleton(DeferredMigrationRegistry::class, function () {
return new DeferredMigrationRegistry();
});
}

private function addCommands()
Expand All @@ -102,6 +114,11 @@ private function addCommands()
]);
}

private function add_event_listeners(): void
{
Event::listen(MigrationsEnded::class,RunDeferredMigrations::class);
}

/**
* Set the path for migrations which should
* be migrated by laravel. More informations:
Expand Down
Loading