Skip to content

Commit

Permalink
deferred migrations system (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
recursivetree authored Feb 14, 2024
1 parent cc17fe2 commit 09d8b55
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
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

0 comments on commit 09d8b55

Please sign in to comment.