34692-vm/db/migrate.php
Flatlogic Bot 85893154df 2
2025-10-05 15:36:57 +00:00

33 lines
919 B
PHP

<?php
require_once __DIR__ . '/config.php';
try {
// Connect to MySQL server without specifying a database
$pdo_server = new PDO('mysql:host='.DB_HOST, DB_USER, DB_PASS, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);
// Create the database if it doesn't exist
$pdo_server->exec('CREATE DATABASE IF NOT EXISTS `'.DB_NAME.'`');
echo "Database `".DB_NAME."` created or already exists.\n";
// Now, connect to the specific database
$pdo_db = db();
// Run all migrations
$migration_files = glob(__DIR__ . '/migrations/*.sql');
sort($migration_files);
foreach ($migration_files as $file) {
$sql = file_get_contents($file);
$pdo_db->exec($sql);
echo "Executed migration: " . basename($file) . "\n";
}
echo "All migrations successful!\n";
} catch (PDOException $e) {
die("Migration failed: " . $e->getMessage() . "\n");
}