41 lines
1.2 KiB
PHP
41 lines
1.2 KiB
PHP
<?php
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
try {
|
|
// 1. Connect to MySQL server without specifying a DB
|
|
$pdo_server = new PDO('mysql:host=' . DB_HOST, DB_USER, DB_PASS, [
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
]);
|
|
|
|
// 2. Create the database if it doesn't exist
|
|
$dbName = DB_NAME;
|
|
$pdo_server->exec("CREATE DATABASE IF NOT EXISTS `$dbName` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;");
|
|
echo "Database '$dbName' created or already exists.\n";
|
|
|
|
// 3. Now, connect to the specific database using the existing db() function
|
|
$pdo = db();
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
$migrationsDir = __DIR__ . '/migrations';
|
|
$files = glob($migrationsDir . '/*.sql');
|
|
sort($files);
|
|
|
|
if (empty($files)) {
|
|
echo "No migration files found.\n";
|
|
exit;
|
|
}
|
|
|
|
echo "Starting database migrations...\n";
|
|
|
|
foreach ($files as $file) {
|
|
echo "Applying: " . basename($file) . "... ";
|
|
$sql = file_get_contents($file);
|
|
$pdo->exec($sql);
|
|
echo "Done.\n";
|
|
}
|
|
|
|
echo "All migrations completed successfully!\n";
|
|
|
|
} catch (PDOException $e) {
|
|
die("Database operation failed: " . $e->getMessage() . "\n");
|
|
} |