Prepare LaySite 1.5.1 page routing and template updates

This commit is contained in:
2026-11-28 03:07:14 +08:00
parent 50fb30ff3b
commit 474cb766be
41 changed files with 354 additions and 146 deletions
+23 -2
View File
@@ -12,7 +12,7 @@ use Throwable;
final class Database
{
public const MIGRATION_REQUIRED = '数据库尚未完成升级。请在站点目录运行 php bin/install.php。';
private const SCHEMA_VERSION = 8;
private const SCHEMA_VERSION = 9;
// 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';
@@ -54,7 +54,8 @@ final class Database
if ($version > self::SCHEMA_VERSION) {
throw new RuntimeException('当前 SQLite 数据库需要更新版本的 LaySite。');
}
$pdo->exec(file_get_contents($root . '/resources/schema.sql'));
$schema = file_get_contents($root . '/resources/schema.sql');
$pdo->exec($schema);
if ($version < 3) {
foreach (['pages', 'sections'] as $table) {
$columns = $pdo->query('PRAGMA table_info(' . $table . ')')->fetchAll();
@@ -101,12 +102,32 @@ final class Database
if ($version < 5) {
LayoutPage::migrate($pdo);
}
if ($version < 9) {
self::migratePageRoutes($pdo, $schema);
DesignLibrary::ensureDefaultPalette($pdo);
}
if ($version < self::SCHEMA_VERSION) {
$pdo->exec('PRAGMA user_version = ' . self::SCHEMA_VERSION);
}
});
}
private static function migratePageRoutes(PDO $pdo, string $schema): void
{
// Renaming the parent also retargets the old child table's foreign key.
$pdo->exec('ALTER TABLE pages RENAME TO pages_before_routes');
$pdo->exec('ALTER TABLE sections RENAME TO sections_before_routes');
$pdo->exec($schema);
foreach (['pages', 'sections'] as $table) {
$columns = implode(', ', array_column($pdo->query("PRAGMA table_info($table)")->fetchAll(), 'name'));
$pdo->exec("INSERT INTO $table (rowid, $columns) SELECT rowid, $columns FROM {$table}_before_routes");
}
$pdo->exec('DROP TABLE sections_before_routes');
$pdo->exec('DROP TABLE pages_before_routes');
$pdo->exec('CREATE UNIQUE INDEX pages_layout_id ON pages(layout_id)');
$pdo->exec("UPDATE pages SET page_type = 'home' WHERE slug = 'home' AND page_type = 'page' AND layout_id IS NULL");
}
public static function write(callable $operation): mixed
{
$pdo = self::connect();