From 2053fed8a78c4f01de6c3da3289390a02534da45 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Thu, 12 Dec 2024 10:04:12 +0100 Subject: [PATCH] fix: oauth seeder --- database/seeders/OauthSettingSeeder.php | 49 ++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/database/seeders/OauthSettingSeeder.php b/database/seeders/OauthSettingSeeder.php index 2b733120c..e004d1450 100644 --- a/database/seeders/OauthSettingSeeder.php +++ b/database/seeders/OauthSettingSeeder.php @@ -12,19 +12,56 @@ class OauthSettingSeeder extends Seeder */ public function run(): void { - $providers = [ + $providers = collect([ 'azure', 'bitbucket', 'github', 'gitlab', 'google', 'authentik', - ]; + ]); - foreach ($providers as $provider) { - OauthSetting::updateOrCreate( - ['provider' => $provider] - ); + $isOauthSeeded = OauthSetting::count() > 0; + $isOauthWithZeroId = OauthSetting::where('id', 0)->exists(); + if ($isOauthSeeded) { + if ($isOauthWithZeroId) { + $allProviders = OauthSetting::all(); + $notFoundProviders = $providers->diff($allProviders->pluck('provider')); + + $allProviders->each(function ($provider) use ($providers) { + $provider->delete(); + $providerName = $provider->provider; + + $foundProvider = $providers->first(function ($provider) use ($providerName) { + return $provider === $providerName; + }); + + if ($foundProvider) { + $newProvder = new OauthSetting; + $newProvder = $provider; + unset($newProvder->id); + $newProvder->save(); + } + }); + + foreach ($notFoundProviders as $provider) { + OauthSetting::create([ + 'provider' => $provider, + ]); + } + } else { + foreach ($providers as $provider) { + OauthSetting::updateOrCreate([ + 'provider' => $provider, + ]); + } + } + } else { + foreach ($providers as $provider) { + OauthSetting::create([ + 'provider' => $provider, + ]); + } } } }