34950-vm/db/migrate.php
Flatlogic Bot dbf75ca780 1 version
2025-10-16 14:27:03 +00:00

27 lines
731 B
PHP

<?php
require_once 'config.php';
try {
// Connect without specifying a database
$pdo = new PDO('mysql:host='.DB_HOST, DB_USER, DB_PASS, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);
$pdo->exec('CREATE DATABASE IF NOT EXISTS '.DB_NAME);
echo "Database created or already exists." . PHP_EOL;
// Now connect to the specific database
$pdo = db();
$migrations = glob(__DIR__ . '/migrations/*.sql');
sort($migrations);
foreach ($migrations as $migration) {
$sql = file_get_contents($migration);
$pdo->exec($sql);
echo "Executed migration: $migration" . PHP_EOL;
}
} catch (PDOException $e) {
die("Database migration failed: " . $e->getMessage());
}