34918-vm/setup.php
2025-10-13 09:55:19 +00:00

29 lines
812 B
PHP

<?php
require_once 'db/config.php';
try {
// Connect to MySQL without specifying a database
$pdo = new PDO('mysql:host='.DB_HOST, DB_USER, DB_PASS, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);
// Create the database
$pdo->exec('CREATE DATABASE IF NOT EXISTS '.DB_NAME);
// Now connect to the newly created database
$pdo = db();
// Get all migration files and sort them
$migration_files = glob('db/migrations/*.sql');
sort($migration_files);
// Execute each migration
foreach ($migration_files as $file) {
$sql = file_get_contents($file);
$pdo->exec($sql);
}
echo "Database setup and all migrations completed successfully.";
} catch (PDOException $e) {
die("Database setup failed: " . $e->getMessage());
}