From 09d8b559bc4536432b16b4c9308a6838ba6d9936 Mon Sep 17 00:00:00 2001 From: recursivetree <60423027+recursivetree@users.noreply.github.com> Date: Wed, 14 Feb 2024 10:50:33 +0100 Subject: [PATCH] deferred migrations system (#175) --- src/Facades/DeferredMigration.php | 18 ++++++++++++++++ src/Listeners/RunDeferredMigrations.php | 19 ++++++++++++++++ src/Services/DeferredMigrationRegistry.php | 25 ++++++++++++++++++++++ src/ServicesServiceProvider.php | 17 +++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 src/Facades/DeferredMigration.php create mode 100644 src/Listeners/RunDeferredMigrations.php create mode 100644 src/Services/DeferredMigrationRegistry.php diff --git a/src/Facades/DeferredMigration.php b/src/Facades/DeferredMigration.php new file mode 100644 index 00000000..a0177af2 --- /dev/null +++ b/src/Facades/DeferredMigration.php @@ -0,0 +1,18 @@ +make(DeferredMigrationRegistry::class); + + $registry->runMigrations(); + } +} \ No newline at end of file diff --git a/src/Services/DeferredMigrationRegistry.php b/src/Services/DeferredMigrationRegistry.php new file mode 100644 index 00000000..d0ce0c71 --- /dev/null +++ b/src/Services/DeferredMigrationRegistry.php @@ -0,0 +1,25 @@ + + */ + 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(); + } + } +} \ No newline at end of file diff --git a/src/ServicesServiceProvider.php b/src/ServicesServiceProvider.php index 5b63afb4..05fdd677 100644 --- a/src/ServicesServiceProvider.php +++ b/src/ServicesServiceProvider.php @@ -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 @@ -76,6 +81,9 @@ public function boot() // Inform Laravel how to load migrations $this->add_migrations(); + + // add event listener + $this->add_event_listeners(); } /** @@ -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() @@ -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: