diff --git a/app/Models/Settings.php b/app/Models/Settings.php index 5c028faa0..773ec3ee5 100644 --- a/app/Models/Settings.php +++ b/app/Models/Settings.php @@ -25,6 +25,7 @@ declare(strict_types=1); namespace App\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\QueryException; use Illuminate\Support\Facades\Cache; /** @@ -149,12 +150,33 @@ class Settings extends Model public static function settingValue(mixed $setting): mixed { - $value = self::query()->where('name', $setting)->value('value'); + try { + $value = self::query()->where('name', $setting)->value('value'); + } catch (QueryException $e) { + // The settings table does not exist yet on a fresh install. Console + // boot eagerly builds commands whose service constructors read + // settings, so this must degrade gracefully instead of crashing. + if (! self::isMissingTableError($e)) { + throw $e; + } + + return null; + } // Apply the same conversion logic as the accessor return self::convertValue($value); } + /** + * Determine if the exception was caused by a missing table. + */ + private static function isMissingTableError(QueryException $e): bool + { + // MySQL/MariaDB: 42S02, PostgreSQL: 42P01, SQLite: HY000 with message. + return in_array($e->getCode(), ['42S02', '42P01'], true) + || ($e->getCode() === 'HY000' && str_contains($e->getMessage(), 'no such table')); + } + /** * Convert setting value: numeric strings to numbers, preserve empty strings. * diff --git a/tests/Feature/SettingsMissingTableTest.php b/tests/Feature/SettingsMissingTableTest.php new file mode 100644 index 000000000..7d5e01403 --- /dev/null +++ b/tests/Feature/SettingsMissingTableTest.php @@ -0,0 +1,129 @@ + + */ + private array $originalEnvironment = []; + + public function createApplication() + { + $this->databasePath = sys_get_temp_dir().'/nntmux-settings-missing-table-test.sqlite'; + + $this->originalEnvironment = [ + 'APP_ENV' => getenv('APP_ENV'), + 'DB_CONNECTION' => getenv('DB_CONNECTION'), + 'DB_DATABASE' => getenv('DB_DATABASE'), + ]; + + if (file_exists($this->databasePath)) { + unlink($this->databasePath); + } + + // Deliberately create an empty database file: no settings table. + new PDO('sqlite:'.$this->databasePath); + + $this->setEnvironmentValue('APP_ENV', 'testing'); + $this->setEnvironmentValue('DB_CONNECTION', 'sqlite'); + $this->setEnvironmentValue('DB_DATABASE', $this->databasePath); + + $app = require __DIR__.'/../../bootstrap/app.php'; + + $app->make(Kernel::class)->bootstrap(); + + return $app; + } + + protected function setUp(): void + { + parent::setUp(); + + config([ + 'database.default' => 'sqlite', + 'database.connections.sqlite.database' => $this->databasePath, + ]); + } + + protected function tearDown(): void + { + if ($this->databasePath !== '' && file_exists($this->databasePath)) { + unlink($this->databasePath); + } + + parent::tearDown(); + + foreach ($this->originalEnvironment as $key => $value) { + $this->setEnvironmentValue($key, $value === false ? null : $value); + } + } + + public function test_setting_value_returns_null_when_settings_table_is_missing(): void + { + $this->assertNull(Settings::settingValue('categorizeforeign')); + } + + public function test_setting_value_returns_converted_value_when_table_exists(): void + { + $pdo = new PDO('sqlite:'.$this->databasePath); + $pdo->exec('CREATE TABLE settings (name VARCHAR PRIMARY KEY, value TEXT NULL)'); + $pdo->exec("INSERT INTO settings (name, value) VALUES ('delaytime', '42')"); + + $this->assertSame(42, Settings::settingValue('delaytime')); + } + + public function test_setting_value_rethrows_unrelated_query_errors(): void + { + $pdo = new PDO('sqlite:'.$this->databasePath); + $pdo->exec('CREATE TABLE settings (name VARCHAR PRIMARY KEY, value TEXT NULL)'); + $pdo->exec("INSERT INTO settings (name, value) VALUES ('delaytime', '5')"); + + // A locked database must not be masked as a missing table. + // Keep the default 60s sqlite busy timeout from slowing the suite down. + DB::connection()->getPdo()->setAttribute(PDO::ATTR_TIMEOUT, 1); + + $locker = new PDO('sqlite:'.$this->databasePath); + $locker->exec('BEGIN EXCLUSIVE TRANSACTION'); + $locker->exec("INSERT INTO settings (name, value) VALUES ('lockholder', '1')"); + + try { + $this->expectException(QueryException::class); + + Settings::settingValue('delaytime'); + } finally { + $locker->exec('ROLLBACK'); + } + } + + private function setEnvironmentValue(string $key, ?string $value): void + { + if ($value === null) { + putenv($key); + unset($_ENV[$key], $_SERVER[$key]); + + return; + } + + putenv($key.'='.$value); + $_ENV[$key] = $value; + $_SERVER[$key] = $value; + } +}