mirror of
https://github.com/NNTmux/newznab-tmux.git
synced 2026-08-28 22:01:33 +00:00
Fix issue preventing schema import. Closes #1866
This commit is contained in:
+23
-1
@@ -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.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Settings;
|
||||
use Illuminate\Contracts\Console\Kernel;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use PDO;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* On a fresh install the settings table does not exist yet, while console boot
|
||||
* eagerly builds commands whose service constructors read settings. The model
|
||||
* must degrade to null instead of crashing (see NNTmux/newznab-tmux#1866).
|
||||
*/
|
||||
final class SettingsMissingTableTest extends TestCase
|
||||
{
|
||||
private string $databasePath = '';
|
||||
|
||||
/**
|
||||
* @var array<string, string|false>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user