Improve error handling for Installer (#532)

* make sure migrations ran

* add loading indicator to finish button

* make error notification persistent

* fix migration checker

* cleanup traits
This commit is contained in:
Boy132 2024-08-09 08:23:03 +02:00 committed by GitHub
parent 7f8fb3f650
commit 1fba700096
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 65 additions and 40 deletions

View File

@ -2,8 +2,8 @@
namespace App\Console\Commands\Environment; namespace App\Console\Commands\Environment;
use App\Traits\EnvironmentWriterTrait;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use App\Traits\Commands\EnvironmentWriterTrait;
use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Artisan;
class AppSettingsCommand extends Command class AppSettingsCommand extends Command

View File

@ -2,10 +2,10 @@
namespace App\Console\Commands\Environment; namespace App\Console\Commands\Environment;
use App\Traits\Commands\RequestRedisSettingsTrait;
use App\Traits\EnvironmentWriterTrait;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Contracts\Console\Kernel; use Illuminate\Contracts\Console\Kernel;
use App\Traits\Commands\EnvironmentWriterTrait;
use App\Traits\Commands\RequestRedisSettingsTrait;
class CacheSettingsCommand extends Command class CacheSettingsCommand extends Command
{ {

View File

@ -2,10 +2,10 @@
namespace App\Console\Commands\Environment; namespace App\Console\Commands\Environment;
use App\Traits\EnvironmentWriterTrait;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Contracts\Console\Kernel; use Illuminate\Contracts\Console\Kernel;
use Illuminate\Database\DatabaseManager; use Illuminate\Database\DatabaseManager;
use App\Traits\Commands\EnvironmentWriterTrait;
class DatabaseSettingsCommand extends Command class DatabaseSettingsCommand extends Command
{ {

View File

@ -2,8 +2,8 @@
namespace App\Console\Commands\Environment; namespace App\Console\Commands\Environment;
use App\Traits\EnvironmentWriterTrait;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use App\Traits\Commands\EnvironmentWriterTrait;
class EmailSettingsCommand extends Command class EmailSettingsCommand extends Command
{ {

View File

@ -2,10 +2,10 @@
namespace App\Console\Commands\Environment; namespace App\Console\Commands\Environment;
use App\Traits\Commands\RequestRedisSettingsTrait;
use App\Traits\EnvironmentWriterTrait;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Contracts\Console\Kernel; use Illuminate\Contracts\Console\Kernel;
use App\Traits\Commands\EnvironmentWriterTrait;
use App\Traits\Commands\RequestRedisSettingsTrait;
class QueueSettingsCommand extends Command class QueueSettingsCommand extends Command
{ {

View File

@ -2,10 +2,10 @@
namespace App\Console\Commands\Environment; namespace App\Console\Commands\Environment;
use App\Traits\Commands\RequestRedisSettingsTrait;
use App\Traits\EnvironmentWriterTrait;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Contracts\Console\Kernel; use Illuminate\Contracts\Console\Kernel;
use App\Traits\Commands\EnvironmentWriterTrait;
use App\Traits\Commands\RequestRedisSettingsTrait;
class SessionSettingsCommand extends Command class SessionSettingsCommand extends Command
{ {

View File

@ -2,7 +2,7 @@
namespace App\Console\Commands\Overrides; namespace App\Console\Commands\Overrides;
use App\Console\RequiresDatabaseMigrations; use App\Traits\Commands\RequiresDatabaseMigrations;
use Illuminate\Database\Console\Seeds\SeedCommand as BaseSeedCommand; use Illuminate\Database\Console\Seeds\SeedCommand as BaseSeedCommand;
class SeedCommand extends BaseSeedCommand class SeedCommand extends BaseSeedCommand

View File

@ -2,7 +2,7 @@
namespace App\Console\Commands\Overrides; namespace App\Console\Commands\Overrides;
use App\Console\RequiresDatabaseMigrations; use App\Traits\Commands\RequiresDatabaseMigrations;
use Illuminate\Foundation\Console\UpCommand as BaseUpCommand; use Illuminate\Foundation\Console\UpCommand as BaseUpCommand;
class UpCommand extends BaseUpCommand class UpCommand extends BaseUpCommand

View File

@ -8,7 +8,8 @@ use App\Filament\Pages\Installer\Steps\EnvironmentStep;
use App\Filament\Pages\Installer\Steps\RedisStep; use App\Filament\Pages\Installer\Steps\RedisStep;
use App\Filament\Pages\Installer\Steps\RequirementsStep; use App\Filament\Pages\Installer\Steps\RequirementsStep;
use App\Services\Users\UserCreationService; use App\Services\Users\UserCreationService;
use App\Traits\Commands\EnvironmentWriterTrait; use App\Traits\CheckMigrationsTrait;
use App\Traits\EnvironmentWriterTrait;
use Exception; use Exception;
use Filament\Facades\Filament; use Filament\Facades\Filament;
use Filament\Forms\Components\Wizard; use Filament\Forms\Components\Wizard;
@ -29,6 +30,7 @@ use Illuminate\Support\HtmlString;
*/ */
class PanelInstaller extends SimplePage implements HasForms class PanelInstaller extends SimplePage implements HasForms
{ {
use CheckMigrationsTrait;
use EnvironmentWriterTrait; use EnvironmentWriterTrait;
use HasUnsavedDataChangesAlert; use HasUnsavedDataChangesAlert;
use InteractsWithForms; use InteractsWithForms;
@ -73,8 +75,10 @@ class PanelInstaller extends SimplePage implements HasForms
<x-filament::button <x-filament::button
type="submit" type="submit"
size="sm" size="sm"
wire:loading.attr="disabled"
> >
Finish Finish
<span wire:loading><x-filament::loading-indicator class="h-4 w-4" /></span>
</x-filament::button> </x-filament::button>
BLADE))), BLADE))),
]; ];
@ -105,13 +109,17 @@ class PanelInstaller extends SimplePage implements HasForms
'--seed' => true, '--seed' => true,
]); ]);
if (!$this->hasCompletedMigrations()) {
throw new Exception('Migrations didn\'t run successfully. Double check your database configuration.');
}
// Create first admin user // Create first admin user
$userData = array_get($inputs, 'user'); $userData = array_get($inputs, 'user');
$userData['root_admin'] = true; $userData['root_admin'] = true;
app(UserCreationService::class)->handle($userData); app(UserCreationService::class)->handle($userData);
// Install setup complete // Install setup complete
$this->writeToEnvironment(['APP_INSTALLED' => true]); $this->writeToEnvironment(['APP_INSTALLED' => 'true']);
$this->rememberData(); $this->rememberData();
@ -122,10 +130,13 @@ class PanelInstaller extends SimplePage implements HasForms
redirect()->intended(Filament::getUrl()); redirect()->intended(Filament::getUrl());
} catch (Exception $exception) { } catch (Exception $exception) {
report($exception);
Notification::make() Notification::make()
->title('Installation Failed') ->title('Installation Failed')
->body($exception->getMessage()) ->body($exception->getMessage())
->danger() ->danger()
->persistent()
->send(); ->send();
} }
} }

View File

@ -4,7 +4,7 @@ namespace App\Filament\Pages;
use App\Models\Backup; use App\Models\Backup;
use App\Notifications\MailTested; use App\Notifications\MailTested;
use App\Traits\Commands\EnvironmentWriterTrait; use App\Traits\EnvironmentWriterTrait;
use Exception; use Exception;
use Filament\Actions\Action; use Filament\Actions\Action;
use Filament\Forms\Components\Actions\Action as FormAction; use Filament\Forms\Components\Actions\Action as FormAction;

View File

@ -0,0 +1,29 @@
<?php
namespace App\Traits;
use Illuminate\Database\Migrations\Migrator;
trait CheckMigrationsTrait
{
/**
* Checks if the migrations have finished running by comparing the last migration file.
*/
protected function hasCompletedMigrations(): bool
{
/** @var Migrator $migrator */
$migrator = app()->make('migrator');
$files = $migrator->getMigrationFiles(database_path('migrations'));
if (!$migrator->repositoryExists()) {
return false;
}
if (array_diff(array_keys($files), $migrator->getRepository()->getRan())) {
return false;
}
return true;
}
}

View File

@ -1,32 +1,16 @@
<?php <?php
namespace App\Console; namespace App\Traits\Commands;
use App\Traits\CheckMigrationsTrait;
use Illuminate\Console\Command;
/** /**
* @mixin \Illuminate\Console\Command * @mixin Command
*/ */
trait RequiresDatabaseMigrations trait RequiresDatabaseMigrations
{ {
/** use CheckMigrationsTrait;
* Checks if the migrations have finished running by comparing the last migration file.
*/
protected function hasCompletedMigrations(): bool
{
/** @var \Illuminate\Database\Migrations\Migrator $migrator */
$migrator = $this->getLaravel()->make('migrator');
$files = $migrator->getMigrationFiles(database_path('migrations'));
if (!$migrator->repositoryExists()) {
return false;
}
if (array_diff(array_keys($files), $migrator->getRepository()->getRan())) {
return false;
}
return true;
}
/** /**
* Throw a massive error into the console to hopefully catch the users attention and get * Throw a massive error into the console to hopefully catch the users attention and get

View File

@ -1,8 +1,8 @@
<?php <?php
namespace App\Traits\Commands; namespace App\Traits;
use App\Exceptions\PanelException; use Exception;
trait EnvironmentWriterTrait trait EnvironmentWriterTrait
{ {
@ -22,12 +22,13 @@ trait EnvironmentWriterTrait
/** /**
* Update the .env file for the application using the passed in values. * Update the .env file for the application using the passed in values.
* @throws Exception
*/ */
public function writeToEnvironment(array $values = []): void public function writeToEnvironment(array $values = []): void
{ {
$path = base_path('.env'); $path = base_path('.env');
if (!file_exists($path)) { if (!file_exists($path)) {
throw new PanelException('Cannot locate .env file, was this software installed correctly?'); throw new Exception('Cannot locate .env file, was this software installed correctly?');
} }
$saveContents = file_get_contents($path); $saveContents = file_get_contents($path);

View File

@ -3,7 +3,7 @@
namespace App\Tests\Unit\Helpers; namespace App\Tests\Unit\Helpers;
use App\Tests\TestCase; use App\Tests\TestCase;
use App\Traits\Commands\EnvironmentWriterTrait; use App\Traits\EnvironmentWriterTrait;
class EnvironmentWriterTraitTest extends TestCase class EnvironmentWriterTraitTest extends TestCase
{ {