34352-vm/db/migrate.php
Flatlogic Bot 354c764c97 1.3
2025-09-24 18:59:48 +00:00

56 lines
1.9 KiB
PHP

<?php
require_once __DIR__ . '/config.php';
try {
$pdo = db();
// Alter table engine to InnoDB
$pdo->exec("ALTER TABLE scores ENGINE=InnoDB");
echo "Table 'scores' engine converted to InnoDB.<br>";
// Check if player_id column exists
$stmt = $pdo->query("SHOW COLUMNS FROM scores LIKE 'player_id'");
if ($stmt->rowCount() == 0) {
// Add player_id column
$pdo->exec("ALTER TABLE scores ADD COLUMN player_id INT NOT NULL AFTER course_id");
// Add foreign key constraint
$pdo->exec("ALTER TABLE scores ADD FOREIGN KEY (player_id) REFERENCES players(id)");
echo "Column 'player_id' added to 'scores' table and foreign key created.<br>";
} else {
echo "Column 'player_id' already exists in 'scores' table.<br>";
}
// Check if team_id column exists
$stmt = $pdo->query("SHOW COLUMNS FROM scores LIKE 'team_id'");
if ($stmt->rowCount() == 0) {
// Add team_id column
$pdo->exec("ALTER TABLE scores ADD COLUMN team_id INT NOT NULL AFTER player_id");
// Add foreign key constraint
$pdo->exec("ALTER TABLE scores ADD FOREIGN KEY (team_id) REFERENCES teams(id)");
echo "Column 'team_id' added to 'scores' table and foreign key created.<br>";
} else {
echo "Column 'team_id' already exists in 'scores' table.<br>";
}
// Safely remove player_name and team_name if they exist
$stmt = $pdo->query("SHOW COLUMNS FROM scores LIKE 'player_name'");
if ($stmt->rowCount() > 0) {
$pdo->exec("ALTER TABLE scores DROP COLUMN player_name");
echo "Column 'player_name' removed from 'scores' table.<br>";
}
$stmt = $pdo->query("SHOW COLUMNS FROM scores LIKE 'team_name'");
if ($stmt->rowCount() > 0) {
$pdo->exec("ALTER TABLE scores DROP COLUMN team_name");
echo "Column 'team_name' removed from 'scores' table.<br>";
}
} catch (PDOException $e) {
die("DB ERROR: " . $e->getMessage());
}
?>