Mid-Game-Join + DB-Upgrade (Migration V11, Foreign Keys)
- V11: late_join-Flag, sticker-Spalte, gtl_groups(+members), gtl_fake_ads, last_seen/current_room - FK-Re-Engineering: user_id -> gtl_users.id ON DELETE CASCADE fuer gtl_players & gtl_round_players (defensiv: Orphan-Cleanup + try/catch, blockiert Migrationen nie) - Nachzuegler: player_join bei laufender Runde -> Zuschauer (answer/vote_locked/late_join gesetzt) - Nachzuegler aus Voting-/Reveal-Zaehlung, Auswertung, Verlauf und Statistik exkludiert - new_round/assign_questions setzen late_join zurueck; Host-Liste zeigt Zuschauer-Icon Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
91e9470a63
commit
182e3bfb2e
3 changed files with 130 additions and 14 deletions
96
migrate.php
96
migrate.php
|
|
@ -6,7 +6,7 @@
|
|||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
const LATEST_MIGRATION = 10;
|
||||
const LATEST_MIGRATION = 11;
|
||||
|
||||
function runMigrations(PDO $pdo, string $dbname): void
|
||||
{
|
||||
|
|
@ -193,6 +193,70 @@ function runMigrations(PDO $pdo, string $dbname): void
|
|||
'TINYINT(1) NOT NULL DEFAULT 0');
|
||||
markMigration($pdo, 10);
|
||||
}
|
||||
|
||||
// ── V11: Mid-Game-Join, Custom-Sticker, Gruppen, Fake-Ads, Foreign Keys ──
|
||||
if ($current < 11) {
|
||||
// Nachzügler-Flag (rundenbezogen): Spieler, die mitten in einer Runde joinen
|
||||
addColumnIfMissing($pdo, $dbname, 'gtl_players', 'late_join',
|
||||
'TINYINT(1) NOT NULL DEFAULT 0 AFTER vote_request');
|
||||
|
||||
// Custom-Profil-Sticker (Dateiname in uploads/sticker/)
|
||||
addColumnIfMissing($pdo, $dbname, 'gtl_users', 'sticker',
|
||||
'VARCHAR(255) NULL DEFAULT NULL AFTER avatar');
|
||||
|
||||
// Herden / Gang-System
|
||||
$pdo->exec("CREATE TABLE IF NOT EXISTS `gtl_groups` (
|
||||
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
`group_name` VARCHAR(60) NOT NULL,
|
||||
`created_by` INT UNSIGNED NULL,
|
||||
`created_at` INT UNSIGNED NOT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_creator` (`created_by`),
|
||||
CONSTRAINT `fk_group_creator` FOREIGN KEY (`created_by`)
|
||||
REFERENCES `gtl_users`(`id`) ON DELETE SET NULL
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
|
||||
|
||||
// Mitgliedschaften (Mapping User ↔ Gruppe) – nötig für Online-/Lobby-Übersicht
|
||||
$pdo->exec("CREATE TABLE IF NOT EXISTS `gtl_group_members` (
|
||||
`group_id` INT UNSIGNED NOT NULL,
|
||||
`user_id` INT UNSIGNED NOT NULL,
|
||||
`joined_at`INT UNSIGNED NOT NULL,
|
||||
PRIMARY KEY (`group_id`,`user_id`),
|
||||
KEY `idx_member_user` (`user_id`),
|
||||
CONSTRAINT `fk_gm_group` FOREIGN KEY (`group_id`) REFERENCES `gtl_groups`(`id`) ON DELETE CASCADE,
|
||||
CONSTRAINT `fk_gm_user` FOREIGN KEY (`user_id`) REFERENCES `gtl_users`(`id`) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
|
||||
|
||||
// Fake-Werbung (Admin-Joke)
|
||||
$pdo->exec("CREATE TABLE IF NOT EXISTS `gtl_fake_ads` (
|
||||
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
`image_url` VARCHAR(500) NOT NULL,
|
||||
`target_url` VARCHAR(500) NULL,
|
||||
`is_active` TINYINT(1) NOT NULL DEFAULT 1,
|
||||
`created_at` INT UNSIGNED NOT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_active` (`is_active`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
|
||||
|
||||
// Re-Engineering auf echte Foreign Keys (user_id → gtl_users.id, ON DELETE CASCADE).
|
||||
// Defensiv: erst verwaiste Verweise auf NULL setzen, dann FK anlegen.
|
||||
addForeignKeyIfMissing($pdo, $dbname, 'gtl_players', 'fk_player_user',
|
||||
'user_id', 'gtl_users', 'id', 'ON DELETE CASCADE');
|
||||
addForeignKeyIfMissing($pdo, $dbname, 'gtl_round_players', 'fk_rp_user',
|
||||
'user_id', 'gtl_users', 'id', 'ON DELETE CASCADE');
|
||||
|
||||
// Online-Tracking für die Herden-Übersicht
|
||||
addColumnIfMissing($pdo, $dbname, 'gtl_users', 'last_seen',
|
||||
'INT UNSIGNED NULL DEFAULT NULL AFTER last_login');
|
||||
addColumnIfMissing($pdo, $dbname, 'gtl_users', 'current_room',
|
||||
'VARCHAR(12) NULL DEFAULT NULL AFTER last_seen');
|
||||
|
||||
// Sticker-Upload-Verzeichnis
|
||||
$sdir = __DIR__ . '/uploads/sticker';
|
||||
if (!is_dir($sdir)) { @mkdir($sdir, 0755, true); }
|
||||
|
||||
markMigration($pdo, 11);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Hilfsfunktionen ──────────────────────────────────────────
|
||||
|
|
@ -214,6 +278,36 @@ function addColumnIfMissing(PDO $pdo, string $db, string $table, string $col, st
|
|||
}
|
||||
}
|
||||
|
||||
function foreignKeyExists(PDO $pdo, string $db, string $table, string $constraint): bool
|
||||
{
|
||||
$stmt = $pdo->prepare(
|
||||
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
|
||||
WHERE CONSTRAINT_SCHEMA=? AND TABLE_NAME=? AND CONSTRAINT_NAME=? AND CONSTRAINT_TYPE='FOREIGN KEY'"
|
||||
);
|
||||
$stmt->execute([$db, $table, $constraint]);
|
||||
return (int) $stmt->fetchColumn() > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Legt einen Foreign Key an, falls noch nicht vorhanden.
|
||||
* Räumt vorher verwaiste Verweise auf (→ NULL) und kapselt Fehler,
|
||||
* damit eine fehlschlagende FK-Anlage die Migrationen niemals blockiert.
|
||||
*/
|
||||
function addForeignKeyIfMissing(PDO $pdo, string $db, string $table, string $constraint,
|
||||
string $col, string $refTable, string $refCol, string $onDelete): void
|
||||
{
|
||||
if (foreignKeyExists($pdo, $db, $table, $constraint)) return;
|
||||
if (!columnExists($pdo, $db, $table, $col)) return;
|
||||
try {
|
||||
$pdo->exec("UPDATE `$table` SET `$col`=NULL
|
||||
WHERE `$col` IS NOT NULL AND `$col` NOT IN (SELECT `$refCol` FROM `$refTable`)");
|
||||
$pdo->exec("ALTER TABLE `$table` ADD CONSTRAINT `$constraint`
|
||||
FOREIGN KEY (`$col`) REFERENCES `$refTable`(`$refCol`) $onDelete");
|
||||
} catch (Throwable $e) {
|
||||
// Daten-Inkonsistenz o. ä. → FK nicht erzwingen, Migration trotzdem abschließen
|
||||
}
|
||||
}
|
||||
|
||||
function dropColumnIfExists(PDO $pdo, string $db, string $table, string $col): void
|
||||
{
|
||||
if (columnExists($pdo, $db, $table, $col)) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue