34853-vm/db/migrate.php
2025-10-12 22:40:55 +00:00

41 lines
1.1 KiB
PHP

<?php
// Simple migration runner
require_once __DIR__ . '/config.php';
// 1. Connect without a database selected to create it
try {
$pdo_admin = new PDO('mysql:host='.DB_HOST, DB_USER, DB_PASS, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);
$pdo_admin->exec("CREATE DATABASE IF NOT EXISTS `".DB_NAME."`");
echo "Database `".DB_NAME."` created or already exists.\n";
} catch (PDOException $e) {
echo "Error creating database: " . $e->getMessage() . "\n";
// If we can't create the DB, we probably can't do anything else.
exit(1);
}
// 2. Now connect to the database and run migrations
$pdo = db();
$migrations_dir = __DIR__ . '/migrations';
$files = glob($migrations_dir . '/*.sql');
sort($files);
foreach ($files as $file) {
echo "Running migration: " . basename($file) . "\n";
try {
$sql = file_get_contents($file);
$pdo->exec($sql);
echo "Success.\n";
} catch (PDOException $e) {
echo "Error: " . $e->getMessage() . "\n";
// Continue on error
}
}
echo "All migrations completed.\n";
?>