34849-vm/run_migrations.php
2026-02-03 01:43:03 +00:00

62 lines
2.1 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
// Simple migration runner
try {
$pdo = db();
// 1. Create migrations table if it doesn't exist
$pdo->exec("CREATE TABLE IF NOT EXISTS `migrations` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`migration` VARCHAR(255) NOT NULL,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)");
// 2. Get all migrations that have been run
$run_migrations_stmt = $pdo->query("SELECT `migration` FROM `migrations`");
$run_migrations = $run_migrations_stmt->fetchAll(PDO::FETCH_COLUMN);
// 3. Find all migration files
$migration_files = glob('db/migrations/*.sql');
sort($migration_files);
echo "<h1>Actualizando base de datos...</h1>";
$migrations_run = 0;
foreach ($migration_files as $file) {
$migration_name = basename($file);
if (!in_array($migration_name, $run_migrations)) {
// Execute the migration
$sql = file_get_contents($file);
$pdo->exec($sql);
// Record the migration
$stmt = $pdo->prepare("INSERT INTO `migrations` (`migration`) VALUES (?)");
$stmt->execute([$migration_name]);
echo "<p>✔️ Migración aplicada: " . htmlspecialchars($migration_name) . "</p>";
$migrations_run++;
}
}
if ($migrations_run === 0) {
echo "<p>✅ ¡Tu base de datos ya está actualizada! No se necesitaron cambios.</p>";
} else {
echo "<p>✅ ¡Base de datos actualizada correctamente!</p>";
}
echo "<hr>";
echo "<p><strong>Pasos a seguir:</strong></p>";
echo "<ol>";
echo "<li><a href='logout.php'>Haz clic aquí para cerrar sesión.</a></li>";
echo "<li>Después de cerrar sesión, <a href='login.php'>inicia sesión de nuevo</a>.</li>";
echo "<li>Una vez que hayas iniciado sesión, el dashboard y todas las opciones deberían funcionar correctamente.</li>";
echo "</ol>";
echo "<p>Si tienes algún problema, no dudes en decírmelo.</p>";
} catch (PDOException $e) {
die("Error de base de datos: " . $e->getMessage());
}
?>