35 lines
1.0 KiB
PHP
35 lines
1.0 KiB
PHP
<?php
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
try {
|
|
// Connect without specifying a DB to create it if it doesn't exist
|
|
$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);
|
|
echo "Database '" . DB_NAME . "' created or already exists.\n";
|
|
|
|
// Now connect to the database to run migrations
|
|
$pdo = db();
|
|
|
|
$migration_files = glob(__DIR__ . '/migrations/*.sql');
|
|
sort($migration_files);
|
|
|
|
foreach ($migration_files as $file) {
|
|
echo "Running migration: " . basename($file) . "...\n";
|
|
$sql = file_get_contents($file);
|
|
try {
|
|
$pdo->exec($sql);
|
|
echo "Migration " . basename($file) . " completed.\n";
|
|
} catch (PDOException $e) {
|
|
echo "Error in migration " . basename($file) . ": " . $e->getMessage() . "\n";
|
|
}
|
|
}
|
|
|
|
echo "All migrations completed successfully.\n";
|
|
|
|
} catch (PDOException $e) {
|
|
die("DB ERROR: " . $e->getMessage());
|
|
}
|
|
|