You've already forked Atomcms-edit
125 lines
3.5 KiB
PHP
Executable File
125 lines
3.5 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Services\Diagnostics;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
class DatabaseDiagnostic
|
|
{
|
|
/**
|
|
* @return array<DiagnosticResult>
|
|
*/
|
|
public function runAll(): array
|
|
{
|
|
return [
|
|
$this->checkConnection(),
|
|
$this->checkMigrations(),
|
|
$this->checkRequiredTables(),
|
|
$this->checkRadioTables(),
|
|
];
|
|
}
|
|
|
|
public function checkConnection(): DiagnosticResult
|
|
{
|
|
try {
|
|
DB::connection()->getPdo();
|
|
|
|
return DiagnosticResult::ok('Database Connection', 'Connected to ' . DB::connection()->getDatabaseName());
|
|
} catch (\Exception $e) {
|
|
return DiagnosticResult::error('Database Connection', $e->getMessage(), 'Check DB credentials in .env');
|
|
}
|
|
}
|
|
|
|
public function checkMigrations(): DiagnosticResult
|
|
{
|
|
try {
|
|
$pending = DB::table('migrations')->exists()
|
|
? count($this->getPendingMigrations())
|
|
: 'unknown';
|
|
|
|
if ($pending === 'unknown') {
|
|
return DiagnosticResult::warning('Migrations', 'Migrations table not found');
|
|
}
|
|
|
|
if ($pending > 0) {
|
|
return DiagnosticResult::warning('Migrations', "{$pending} pending migrations", 'Run: php artisan migrate');
|
|
}
|
|
|
|
return DiagnosticResult::ok('Migrations', 'All migrations up to date');
|
|
} catch (\Exception $e) {
|
|
return DiagnosticResult::error('Migrations', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function checkRequiredTables(): DiagnosticResult
|
|
{
|
|
$requiredTables = [
|
|
'users', 'permissions', 'website_settings', 'website_articles',
|
|
'website_shop_articles', 'website_shop_categories',
|
|
];
|
|
|
|
$missing = [];
|
|
foreach ($requiredTables as $table) {
|
|
if (! Schema::hasTable($table)) {
|
|
$missing[] = $table;
|
|
}
|
|
}
|
|
|
|
if ($missing !== []) {
|
|
return DiagnosticResult::error(
|
|
'Required Tables',
|
|
'Missing: ' . implode(', ', $missing),
|
|
'Run: php artisan migrate',
|
|
);
|
|
}
|
|
|
|
return DiagnosticResult::ok('Required Tables', 'All required tables exist');
|
|
}
|
|
|
|
public function checkRadioTables(): DiagnosticResult
|
|
{
|
|
$radioTables = [
|
|
'radio_ranks', 'radio_banners', 'radio_schedules',
|
|
'radio_shouts', 'radio_history',
|
|
];
|
|
|
|
$missing = [];
|
|
foreach ($radioTables as $table) {
|
|
if (! Schema::hasTable($table)) {
|
|
$missing[] = $table;
|
|
}
|
|
}
|
|
|
|
if ($missing !== []) {
|
|
return DiagnosticResult::warning(
|
|
'Radio Tables',
|
|
'Missing: ' . implode(', ', $missing),
|
|
'Run radio migration seeder',
|
|
);
|
|
}
|
|
|
|
return DiagnosticResult::ok('Radio Tables', 'All radio tables exist');
|
|
}
|
|
|
|
/**
|
|
* @return array<string>
|
|
*/
|
|
private function getPendingMigrations(): array
|
|
{
|
|
$migrated = DB::table('migrations')->pluck('migration')->toArray();
|
|
$allMigrations = [];
|
|
|
|
$migrationPath = database_path('migrations');
|
|
if (is_dir($migrationPath)) {
|
|
foreach (scandir($migrationPath) as $file) {
|
|
if (str_ends_with($file, '.php')) {
|
|
$allMigrations[] = pathinfo($file, PATHINFO_FILENAME);
|
|
}
|
|
}
|
|
}
|
|
|
|
return array_diff($allMigrations, $migrated);
|
|
}
|
|
}
|