36 lines
1.1 KiB
PHP
36 lines
1.1 KiB
PHP
<?php
|
|
require_once __DIR__ . '/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 ".DB_NAME." created or already exists.\n";
|
|
} catch (PDOException $e) {
|
|
echo "Error creating database: " . $e->getMessage() . "\n";
|
|
// If the user already exists, we might not have permission to create a database.
|
|
// Let's assume the DB is there and try to connect anyway for the next step.
|
|
}
|
|
|
|
// Now, connect to the database and run migrations
|
|
$pdo = db();
|
|
$migrationsDir = __DIR__ . '/migrations';
|
|
$files = glob($migrationsDir . '/*.sql');
|
|
|
|
sort($files);
|
|
|
|
foreach ($files as $file) {
|
|
echo "Applying migration: " . basename($file) . "\n";
|
|
$sql = file_get_contents($file);
|
|
try {
|
|
$pdo->exec($sql);
|
|
echo "Success\n";
|
|
} catch (PDOException $e) {
|
|
echo "Error applying migration: " . $e->getMessage() . "\n";
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
echo "All migrations applied successfully.\n"; |