38471-vm/run_migrations.php
2026-02-21 08:24:48 +00:00

63 lines
2.0 KiB
PHP

<?php
require_once 'db/config.php';
try {
$pdo = db();
// Check if migrations table exists
$stmt = $pdo->query("SHOW TABLES LIKE 'migrations'");
if ($stmt->rowCount() == 0) {
$pdo->exec("CREATE TABLE migrations (
id INT AUTO_INCREMENT PRIMARY KEY,
migration VARCHAR(255) NOT NULL,
executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)");
echo "Created migrations table.\n";
}
$stmt = $pdo->query("SELECT migration FROM migrations");
$executed = $stmt->fetchAll(PDO::FETCH_COLUMN);
$files = glob('db/migrations/*.sql');
sort($files);
foreach ($files as $file) {
$migrationName = basename($file);
if (!in_array($migrationName, $executed)) {
echo "Executing: $migrationName...\n";
$sql = file_get_contents($file);
try {
$pdo->exec($sql);
$stmt = $pdo->prepare("INSERT INTO migrations (migration) VALUES (?)");
$stmt->execute([$migrationName]);
echo "Success: $migrationName\n";
} catch (PDOException $e) {
echo "Error executing $migrationName: " . $e->getMessage() . "\n";
}
}
}
} catch (Exception $e) {
echo "DB Error: " . $e->getMessage() . "\n";
}
$php_files = glob('db/migrations/*.php');
sort($php_files);
foreach ($php_files as $pfile) {
$migrationName = basename($pfile);
if (!in_array($migrationName, $executed)) {
echo "Executing PHP: $migrationName...\n";
try {
include $pfile;
$stmt = $pdo->prepare("INSERT INTO migrations (migration) VALUES (?)");
$stmt->execute([$migrationName]);
echo "Success: $migrationName\n";
} catch (Exception $e) {
echo "Error executing $migrationName: " . $e->getMessage() . "\n";
}
}
}
echo "Done checking migrations.\n";