Load site settings from dotenv and store OAuth credentials

This commit is contained in:
2026-11-23 16:53:29 +08:00
parent 1bb0554608
commit 01ede89dcd
27 changed files with 730 additions and 76 deletions
+11 -2
View File
@@ -4,26 +4,32 @@ declare(strict_types=1);
namespace App\Content;
use App\Config\Environment;
use PDO;
use RuntimeException;
use Throwable;
final class Database
{
private const SCHEMA_VERSION = 1;
private const SCHEMA_VERSION = 2;
// Avoid reusing a deleted ID that may still be open in an editing form.
public const NEXT_ARTICLE_ID = 'MAX(COALESCE((SELECT MAX(id) FROM articles), 0),
COALESCE((SELECT MAX(article_id) FROM article_deletions), 0)) + 1';
private static ?PDO $connection = null;
public static function path(): string
{
return Environment::path('LAYSITE_DB', 'storage/laysite.sqlite');
}
public static function connect(): PDO
{
if (self::$connection !== null) {
return self::$connection;
}
$path = getenv('LAYSITE_DB') ?: dirname(__DIR__, 2) . '/storage/laysite.sqlite';
$path = self::path();
if (!is_dir(dirname($path)) && !mkdir(dirname($path), 0750, true) && !is_dir(dirname($path))) {
throw new RuntimeException('无法创建 SQLite 存放目录。');
}
@@ -65,6 +71,9 @@ final class Database
foreach (Repository::CATEGORIES as $slug => $name) {
$category->execute([$slug, $name]);
}
if ($version < 2) {
OAuthCredentials::migrateLegacy($pdo);
}
if ($version < self::SCHEMA_VERSION) {
$pdo->exec('PRAGMA user_version = ' . self::SCHEMA_VERSION);
}