36018-vm/db/migrate.php
2025-11-22 14:03:37 +00:00

41 lines
1.2 KiB
PHP

<?php
require_once __DIR__ . '/config.php';
function run_migrations() {
$pdo = db();
$log_file = __DIR__ . '/migrations.log';
$migrations_dir = __DIR__ . '/migrations/';
if (!file_exists($log_file)) {
touch($log_file);
}
$executed_migrations = file($log_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$all_migration_files = scandir($migrations_dir);
echo "Checking for new migrations...\n";
foreach ($all_migration_files as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) === 'sql') {
if (!in_array($file, $executed_migrations)) {
echo "Executing migration: $file...\n";
$sql = file_get_contents($migrations_dir . $file);
try {
$pdo->exec($sql);
file_put_contents($log_file, $file . PHP_EOL, FILE_APPEND);
echo "Migration $file executed successfully.\n";
} catch (PDOException $e) {
echo "Error executing migration $file: " . $e->getMessage() . "\n";
// Stop on error
return;
}
}
}
}
echo "All migrations are up to date.\n";
}
run_migrations();