chore: snapshot before branding & feature pass
This commit is contained in:
commit
44e5b48c3c
16 changed files with 3372 additions and 0 deletions
178
migrate.php
Normal file
178
migrate.php
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
<?php
|
||||
/**
|
||||
* Automatische DB-Migrationen.
|
||||
* Wird von api.php und auth.php eingebunden.
|
||||
* Führt nur fehlende Versionen aus — ein SELECT pro Request nach dem Setup.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
const LATEST_MIGRATION = 5;
|
||||
|
||||
function runMigrations(PDO $pdo, string $dbname): void
|
||||
{
|
||||
// Migrations-Tabelle anlegen (immer sicher)
|
||||
$pdo->exec("CREATE TABLE IF NOT EXISTS `gtl_migrations` (
|
||||
`version` INT UNSIGNED NOT NULL,
|
||||
`applied_at` INT UNSIGNED NOT NULL,
|
||||
PRIMARY KEY (`version`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
|
||||
|
||||
$current = (int) $pdo->query("SELECT COALESCE(MAX(version),0) FROM gtl_migrations")->fetchColumn();
|
||||
if ($current >= LATEST_MIGRATION) return;
|
||||
|
||||
// ── V1: Basis-Tabellen ────────────────────────────────────
|
||||
if ($current < 1) {
|
||||
$pdo->exec("CREATE TABLE IF NOT EXISTS `gtl_rooms` (
|
||||
`code` VARCHAR(12) NOT NULL,
|
||||
`pass_hash` VARCHAR(255) NOT NULL,
|
||||
`revealed` TINYINT(1) NOT NULL DEFAULT 0,
|
||||
`created_at` INT UNSIGNED NOT NULL,
|
||||
`updated_at` INT UNSIGNED NOT NULL,
|
||||
PRIMARY KEY (`code`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
|
||||
|
||||
$pdo->exec("CREATE TABLE IF NOT EXISTS `gtl_players` (
|
||||
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
`room_code` VARCHAR(12) NOT NULL,
|
||||
`pkey` VARCHAR(40) NOT NULL,
|
||||
`name` VARCHAR(40) NOT NULL,
|
||||
`question` TEXT NULL,
|
||||
`answer` TEXT NULL,
|
||||
`answered` TINYINT(1) NOT NULL DEFAULT 0,
|
||||
`score` INT NOT NULL DEFAULT 0,
|
||||
`updated_at`INT UNSIGNED NOT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `uniq_room_player` (`room_code`,`pkey`),
|
||||
CONSTRAINT `fk_player_room` FOREIGN KEY (`room_code`)
|
||||
REFERENCES `gtl_rooms`(`code`) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
|
||||
|
||||
markMigration($pdo, 1);
|
||||
}
|
||||
|
||||
// ── V2: Neue Spalten + User/Statistik/Verlauf-Tabellen ───
|
||||
if ($current < 2) {
|
||||
// gtl_rooms
|
||||
addColumnIfMissing($pdo, $dbname, 'gtl_rooms', 'liar_keys', 'TEXT NULL AFTER revealed');
|
||||
addColumnIfMissing($pdo, $dbname, 'gtl_rooms', 'normal_question', 'TEXT NULL AFTER liar_keys');
|
||||
addColumnIfMissing($pdo, $dbname, 'gtl_rooms', 'imposter_question', 'TEXT NULL AFTER normal_question');
|
||||
addColumnIfMissing($pdo, $dbname, 'gtl_rooms', 'round_num', 'INT UNSIGNED NOT NULL DEFAULT 1 AFTER imposter_question');
|
||||
addColumnIfMissing($pdo, $dbname, 'gtl_rooms', 'voted_out_keys', 'TEXT NULL AFTER round_num');
|
||||
addColumnIfMissing($pdo, $dbname, 'gtl_rooms', 'round_outcome', 'VARCHAR(20) NULL AFTER voted_out_keys');
|
||||
dropColumnIfExists($pdo, $dbname, 'gtl_rooms', 'liar_key'); // legacy
|
||||
|
||||
// gtl_players
|
||||
addColumnIfMissing($pdo, $dbname, 'gtl_players', 'user_id', 'INT UNSIGNED NULL AFTER name');
|
||||
addColumnIfMissing($pdo, $dbname, 'gtl_players', 'vote_for', 'VARCHAR(40) NULL AFTER score');
|
||||
|
||||
// gtl_users
|
||||
$pdo->exec("CREATE TABLE IF NOT EXISTS `gtl_users` (
|
||||
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
`username` VARCHAR(40) NOT NULL,
|
||||
`credential_hash` VARCHAR(255) NULL,
|
||||
`avatar` VARCHAR(255) NULL,
|
||||
`role` ENUM('admin','user') NOT NULL DEFAULT 'user',
|
||||
`created_at` INT UNSIGNED NOT NULL,
|
||||
`last_login` INT UNSIGNED NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `uniq_username` (`username`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
|
||||
|
||||
// gtl_user_stats
|
||||
$pdo->exec("CREATE TABLE IF NOT EXISTS `gtl_user_stats` (
|
||||
`user_id` INT UNSIGNED NOT NULL,
|
||||
`games_played` INT UNSIGNED NOT NULL DEFAULT 0,
|
||||
`games_won` INT UNSIGNED NOT NULL DEFAULT 0,
|
||||
`games_lost` INT UNSIGNED NOT NULL DEFAULT 0,
|
||||
`times_imposter` INT UNSIGNED NOT NULL DEFAULT 0,
|
||||
`imposter_wins` INT UNSIGNED NOT NULL DEFAULT 0,
|
||||
`imposter_losses` INT UNSIGNED NOT NULL DEFAULT 0,
|
||||
`innocent_wins` INT UNSIGNED NOT NULL DEFAULT 0,
|
||||
`innocent_losses` INT UNSIGNED NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY (`user_id`),
|
||||
CONSTRAINT `fk_stats_user` FOREIGN KEY (`user_id`) REFERENCES `gtl_users`(`id`) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
|
||||
|
||||
// gtl_game_rounds
|
||||
$pdo->exec("CREATE TABLE IF NOT EXISTS `gtl_game_rounds` (
|
||||
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
`room_code` VARCHAR(12) NOT NULL,
|
||||
`round_num` INT UNSIGNED NOT NULL DEFAULT 1,
|
||||
`played_at` INT UNSIGNED NOT NULL,
|
||||
`outcome` VARCHAR(20) NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_room` (`room_code`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
|
||||
|
||||
// gtl_round_players
|
||||
$pdo->exec("CREATE TABLE IF NOT EXISTS `gtl_round_players` (
|
||||
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
`round_id` INT UNSIGNED NOT NULL,
|
||||
`user_id` INT UNSIGNED NULL,
|
||||
`player_name` VARCHAR(40) NOT NULL,
|
||||
`pkey` VARCHAR(40) NOT NULL,
|
||||
`was_imposter`TINYINT(1) NOT NULL DEFAULT 0,
|
||||
`voted_out` TINYINT(1) NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `uniq_round_player` (`round_id`,`pkey`),
|
||||
CONSTRAINT `fk_rp_round` FOREIGN KEY (`round_id`) REFERENCES `gtl_game_rounds`(`id`) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
|
||||
|
||||
markMigration($pdo, 2);
|
||||
}
|
||||
|
||||
// ── V3: Antworten + Fragen im Verlauf speichern ──────────
|
||||
if ($current < 3) {
|
||||
addColumnIfMissing($pdo, $dbname, 'gtl_round_players', 'question', 'TEXT NULL AFTER voted_out');
|
||||
addColumnIfMissing($pdo, $dbname, 'gtl_round_players', 'answer', 'TEXT NULL AFTER question');
|
||||
markMigration($pdo, 3);
|
||||
}
|
||||
|
||||
// ── V4: uploads-Verzeichnis anlegen ──────────────────────
|
||||
if ($current < 4) {
|
||||
$dir = __DIR__ . '/uploads/avatars';
|
||||
if (!is_dir($dir)) {
|
||||
mkdir($dir, 0755, true);
|
||||
}
|
||||
markMigration($pdo, 4);
|
||||
}
|
||||
|
||||
// ── V5: Voting-Steuerung ──────────────────────────────────
|
||||
if ($current < 5) {
|
||||
addColumnIfMissing($pdo, $dbname, 'gtl_rooms', 'voting_open',
|
||||
'TINYINT(1) NOT NULL DEFAULT 0 AFTER round_outcome');
|
||||
markMigration($pdo, 5);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Hilfsfunktionen ──────────────────────────────────────────
|
||||
|
||||
function columnExists(PDO $pdo, string $db, string $table, string $col): bool
|
||||
{
|
||||
$stmt = $pdo->prepare(
|
||||
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
|
||||
WHERE TABLE_SCHEMA=? AND TABLE_NAME=? AND COLUMN_NAME=?"
|
||||
);
|
||||
$stmt->execute([$db, $table, $col]);
|
||||
return (int) $stmt->fetchColumn() > 0;
|
||||
}
|
||||
|
||||
function addColumnIfMissing(PDO $pdo, string $db, string $table, string $col, string $def): void
|
||||
{
|
||||
if (!columnExists($pdo, $db, $table, $col)) {
|
||||
$pdo->exec("ALTER TABLE `$table` ADD COLUMN `$col` $def");
|
||||
}
|
||||
}
|
||||
|
||||
function dropColumnIfExists(PDO $pdo, string $db, string $table, string $col): void
|
||||
{
|
||||
if (columnExists($pdo, $db, $table, $col)) {
|
||||
$pdo->exec("ALTER TABLE `$table` DROP COLUMN `$col`");
|
||||
}
|
||||
}
|
||||
|
||||
function markMigration(PDO $pdo, int $version): void
|
||||
{
|
||||
$pdo->prepare("INSERT IGNORE INTO gtl_migrations (version, applied_at) VALUES (?,?)")
|
||||
->execute([$version, time()]);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue