34394-vm/db/migrate.php
2025-09-26 19:57:10 +00:00

63 lines
2.0 KiB
PHP

<?php
require_once __DIR__ . '/config.php';
function run_migrations() {
$pdo = db();
$migrationsDir = __DIR__ . '/migrations';
// 1. Create migrations table if it doesn't exist
try {
$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
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");
} catch (PDOException $e) {
echo "Error creating migrations table: " . $e->getMessage() . "\n";
return false;
}
// 2. Get all migration files and executed migrations
$migrationFiles = glob($migrationsDir . '/*.sql');
sort($migrationFiles);
$stmt = $pdo->query("SELECT migration FROM migrations");
$executedMigrations = $stmt->fetchAll(PDO::FETCH_COLUMN);
// 3. Run migrations that have not been executed yet
foreach ($migrationFiles as $file) {
$migrationName = basename($file);
if (in_array($migrationName, $executedMigrations)) {
echo "Skipping already executed migration: {$migrationName}\n";
continue;
}
echo "Running migration: {$migrationName}...\n";
$sql = file_get_contents($file);
if ($sql === false) {
echo "Error: Could not read file {$migrationName}.\n";
continue;
}
try {
// DDL statements often have implicit commits, so transactions are not reliable.
$pdo->exec($sql);
$insertStmt = $pdo->prepare("INSERT INTO migrations (migration) VALUES (?)");
$insertStmt->execute([$migrationName]);
echo "Success.\n";
} catch (PDOException $e) {
echo "Error executing migration {$migrationName}: " . $e->getMessage() . "\n";
// Exit on first error
return false;
}
}
echo "All new migrations have been executed.\n";
return true;
}
if (php_sapi_name() === 'cli') {
run_migrations();
}
?>