| 12345678910111213141516171819202122232425262728293031323334 |
- <?php
- declare(strict_types=1);
- // Beispielmigration mit Datenbank.
- //
- // $context["pdo"] ist die Verbindung aus MANAGE_BACKUP_DATABASE.
- // Idempotent: prüft erst, ob der Index bereits existiert. MySQL kennt kein
- // "ADD INDEX IF NOT EXISTS", deshalb die Abfrage über information_schema.
- return function (array $context): void {
- if (!isset($context["pdo"])) {
- throw new RuntimeException(
- "Diese Migration benötigt eine Datenbank. MANAGE_BACKUP_DATABASE ist nicht konfiguriert.",
- );
- }
- /** @var PDO $pdo */
- $pdo = $context["pdo"];
- $statement = $pdo->prepare(
- "SELECT COUNT(*) FROM information_schema.statistics
- WHERE table_schema = DATABASE()
- AND table_name = :table
- AND index_name = :index",
- );
- $statement->execute(["table" => "orders", "index" => "idx_orders_created"]);
- if ((int) $statement->fetchColumn() > 0) {
- return;
- }
- $pdo->exec("ALTER TABLE orders ADD INDEX idx_orders_created (created_at)");
- };
|