| 12345678910111213141516171819202122232425262728293031323334 |
- <?php
- declare(strict_types=1);
- // Example migration with a database.
- //
- // $context["pdo"] is the connection from MANAGE_BACKUP_DATABASE.
- // Idempotent: checks first whether the index already exists. MySQL has no
- // "ADD INDEX IF NOT EXISTS", hence the query against information_schema.
- return function (array $context): void {
- if (!isset($context["pdo"])) {
- throw new RuntimeException(
- "This migration needs a database. MANAGE_BACKUP_DATABASE is not configured.",
- );
- }
- /** @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)");
- };
|