rpg-westreiche/westreiche_website/app/migrations.php

105 lines
4.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
/**
* Westreiche – Schema-Migrationen.
*
* Wird bei jedem Verbindungsaufbau (db()) einmal pro Request angewendet: Die
* Tabelle schema_migrations merkt sich, was bereits gelaufen ist. Damit
* erreichen Spalten-Erweiterungen auch Installationen, die vor der Änderung
* angelegt wurden (der Installer läuft nur einmal).
*
* Regeln:
* - Jede Migration bekommt eine neue, fortlaufende Version.
* - Statements müssen in sich abgeschlossen und wiederholbar formuliert sein
* (IF NOT EXISTS), damit ein abgebrochener Lauf nachholbar bleibt.
* - Bestehende Einträge nie ändern – nur hinten anhängen.
*/
const WS_MIGRATIONS = [
// 1: Vollständiger Regeltext der Zauber für den Detail-Schirm im Bogen.
1 => [
'ALTER TABLE spells ADD COLUMN IF NOT EXISTS body LONGTEXT NULL AFTER summary',
],
// 2: Eigene Fertigkeiten tragen ihr Bezugsattribut direkt am Charakterbogen.
2 => [
"ALTER TABLE character_skills ADD COLUMN IF NOT EXISTS attribute VARCHAR(3) NOT NULL DEFAULT '' AFTER name",
],
// 3: Katalog für Ausrüstung (Selektor im Inventar). Inhalt kommt aus
// item_catalog_sync()/sql/seed_items.sql, hier nur die Tabelle.
3 => [
"CREATE TABLE IF NOT EXISTS item_catalog (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(120) NOT NULL,
kind VARCHAR(20) NOT NULL DEFAULT 'Ausrüstung',
cost DECIMAL(8,2) NOT NULL DEFAULT 0.00,
weight VARCHAR(20) NOT NULL DEFAULT '',
availability ENUM('methaven', 'garana', 'selten') NOT NULL DEFAULT 'garana',
note VARCHAR(255) NOT NULL DEFAULT '',
sort_order INT NOT NULL DEFAULT 0,
PRIMARY KEY (id),
UNIQUE KEY uq_item_catalog_name (name),
KEY idx_item_catalog_filter (kind, availability)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci",
],
// 4: Geschlecht je Charakter; steuert das Platzhalterporträt.
4 => [
"ALTER TABLE characters ADD COLUMN IF NOT EXISTS gender VARCHAR(20) NOT NULL DEFAULT 'männlich' AFTER title",
],
// 5: Attributkürzel deutsch (GES/KON/WEI statt DEX/CON/WIS). Der ENUM
// muss erst die neuen Werte zulassen, dann die Daten umschlüsseln,
// danach fallen die alten Werte wieder raus.
5 => [
"ALTER TABLE skills MODIFY attribute ENUM('STR', 'DEX', 'CON', 'INT', 'WIS', 'CHA', 'GES', 'KON', 'WEI', '') NOT NULL DEFAULT ''",
"UPDATE skills SET attribute = 'GES' WHERE attribute = 'DEX'",
"UPDATE skills SET attribute = 'KON' WHERE attribute = 'CON'",
"UPDATE skills SET attribute = 'WEI' WHERE attribute = 'WIS'",
"UPDATE character_skills SET attribute = 'GES' WHERE attribute = 'DEX'",
"UPDATE character_skills SET attribute = 'KON' WHERE attribute = 'CON'",
"UPDATE character_skills SET attribute = 'WEI' WHERE attribute = 'WIS'",
"ALTER TABLE skills MODIFY attribute ENUM('STR', 'GES', 'KON', 'INT', 'WEI', 'CHA', '') NOT NULL DEFAULT ''",
],
// 6: Einzelne Trefferpunkt-Würfe (Stufe 0: 1W4, dann je Stufe 1W<Klassen-
// würfel>). hp_max wird daraus berechnet; leer = Altdaten, hp_max gilt
// weiter als manueller Wert.
6 => [
"ALTER TABLE characters ADD COLUMN IF NOT EXISTS hp_rolls VARCHAR(160) NOT NULL DEFAULT '' AFTER hp_max",
],
// 7: Praxispunkte je Fertigkeit (Bogen-Spalte „PP", 0 … 10 ankreuzbar).
7 => [
'ALTER TABLE character_skills ADD COLUMN IF NOT EXISTS pp TINYINT UNSIGNED NOT NULL DEFAULT 0 AFTER note',
],
];
function ws_migrations_apply(PDO $pdo): void
{
static $applied = false;
if ($applied) {
return;
}
$applied = true;
try {
$pdo->exec(
'CREATE TABLE IF NOT EXISTS schema_migrations (
version INT UNSIGNED NOT NULL,
applied_at DATETIME NOT NULL,
PRIMARY KEY (version)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci'
);
$done = array_map('intval', $pdo->query('SELECT version FROM schema_migrations')->fetchAll(PDO::FETCH_COLUMN));
foreach (WS_MIGRATIONS as $version => $statements) {
if (in_array((int) $version, $done, true)) {
continue;
}
foreach ((array) $statements as $sql) {
$pdo->exec($sql);
}
$pdo->prepare('INSERT INTO schema_migrations (version, applied_at) VALUES (?, ?)')
->execute([(int) $version, gmdate('Y-m-d H:i:s')]);
}
} catch (Throwable $error) {
// Migration darf die Seite nicht lahmlegen; Fehler landet im Log.
error_log('Westreiche-Migration: ' . $error->getMessage());
}
}