setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Create migrations table if it doesn't exist $pdo->exec("CREATE TABLE IF NOT EXISTS `migrations` ( `id` INT AUTO_INCREMENT PRIMARY KEY, `migration` VARCHAR(255) NOT NULL, `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP );"); // Get all executed migrations $executed_migrations = $pdo->query("SELECT migration FROM migrations")->fetchAll(PDO::FETCH_COLUMN); // Get all migration files $migration_files = glob(__DIR__ . '/migrations/*.sql'); foreach ($migration_files as $file) { $migration_name = basename($file); if (!in_array($migration_name, $executed_migrations)) { // Execute the migration $sql = file_get_contents($file); $pdo->exec($sql); // Record the migration $stmt = $pdo->prepare("INSERT INTO migrations (migration) VALUES (?)"); $stmt->execute([$migration_name]); echo "Executed migration: {$migration_name}\n"; } } echo "Database migrations completed successfully!\n"; } catch (PDOException $e) { die("Database migration failed: " . $e->getMessage() . "\n"); }