32 lines
1.0 KiB
PHP
32 lines
1.0 KiB
PHP
<?php
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
try {
|
|
// Connect without specifying a database to create it first
|
|
$pdo_admin = new PDO('mysql:host=' . DB_HOST . ';charset=utf8mb4', DB_USER, DB_PASS, [
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
]);
|
|
$pdo_admin->exec("CREATE DATABASE IF NOT EXISTS `" . DB_NAME . "`;");
|
|
echo "Database '" . DB_NAME . "' created or already exists.\n";
|
|
|
|
// Now connect to the specific database to run migrations
|
|
$pdo = db();
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
$migrationsDir = __DIR__ . '/migrations';
|
|
$files = glob($migrationsDir . '/*.sql');
|
|
sort($files);
|
|
|
|
foreach ($files as $file) {
|
|
echo "Running migration: " . basename($file) . "\n";
|
|
$sql = file_get_contents($file);
|
|
$pdo->exec($sql);
|
|
echo "Success.\n";
|
|
}
|
|
|
|
echo "All migrations completed.\n";
|
|
|
|
} catch (PDOException $e) {
|
|
die("Database migration failed: " . $e->getMessage());
|
|
} |