65 lines
2.3 KiB
PHP
65 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/../vendor/autoload.php';
|
|
require_once __DIR__ . '/../app/Core/helpers.php';
|
|
|
|
use App\Core\{Application, Database};
|
|
|
|
// Initialize app to load .env and configs
|
|
$app = new Application(dirname(__DIR__));
|
|
|
|
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
|
|
echo "🗄️ Musadaq Migration Tool\n";
|
|
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
|
|
|
|
try {
|
|
$db = Database::getInstance();
|
|
|
|
// Create migrations table if not exists
|
|
$db->exec("CREATE TABLE IF NOT EXISTS migrations (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
migration VARCHAR(255) NOT NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");
|
|
|
|
$stmt = $db->query("SELECT migration FROM migrations");
|
|
$executed = $stmt->fetchAll(PDO::FETCH_COLUMN);
|
|
|
|
$migrationsDir = dirname(__DIR__) . '/database/migrations';
|
|
$files = glob($migrationsDir . '/*.sql');
|
|
sort($files); // Ensure order
|
|
|
|
$count = 0;
|
|
foreach ($files as $file) {
|
|
$name = basename($file);
|
|
if (!in_array($name, $executed)) {
|
|
echo "🚀 Running: $name... ";
|
|
|
|
$sql = file_get_contents($file);
|
|
|
|
// Execute the SQL. Since it might contain multiple statements,
|
|
// and PDO::exec doesn't always handle them well in one go
|
|
// depending on the driver, we'll try to run it.
|
|
$db->exec($sql);
|
|
|
|
$stmt = $db->prepare("INSERT INTO migrations (migration) VALUES (?)");
|
|
$stmt->execute([$name]);
|
|
|
|
echo "✅ Done\n";
|
|
$count++;
|
|
}
|
|
}
|
|
|
|
if ($count === 0) {
|
|
echo "✨ Nothing to migrate. Database is up to date.\n";
|
|
} else {
|
|
echo "🎉 Migrations completed successfully ($count ran).\n";
|
|
}
|
|
} catch (Exception $e) {
|
|
echo "❌ Error: " . $e->getMessage() . "\n";
|
|
exit(1);
|
|
}
|
|
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
|