35286-vm/db/migrate.php
Flatlogic Bot 6e132e0b38 0.2
2025-10-27 21:34:11 +00:00

35 lines
1.0 KiB
PHP

<?php
require_once __DIR__ . '/../db/config.php';
try {
// First, connect without a database name to create it
$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 . "` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;");
echo "Database '" . DB_NAME . "' created or already exists.\n";
// Now, connect to the specific database to run migrations
$pdo = db();
$migrationsDir = __DIR__ . '/migrations';
$files = glob($migrationsDir . '/*.sql');
if (empty($files)) {
echo "No migration files found.\n";
exit;
}
sort($files);
foreach ($files as $file) {
echo "Running migration: " . basename($file) . "...\n";
$sql = file_get_contents($file);
$pdo->exec($sql);
echo "Migration successful.\n";
}
echo "All migrations completed.\n";
} catch (PDOException $e) {
die("Database migration failed: " . $e->getMessage() . "\n");
}