migracja z szukaniem tabel o podobnych nazwach

<?php

declare(strict_types=1);

namespace Application\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Enp\Bundle\CoreBundle\Migration\AbstractEnpMigration;
use PDO;

final class Version20200819084526 extends AbstractEnpMigration
{
    public function up(Schema $schema): void
    {
        foreach ($this->getOrderExtensionTables() as $tableName) {
            if (empty($tableName)) {
                continue;
            }

            $this->addSql(sprintf('ALTER TABLE %s ADD payment_document_number VARCHAR(255) DEFAULT NULL', $tableName));
        }
    }

    public function down(Schema $schema): void
    {
        foreach ($this->getOrderExtensionTables() as $tableName) {
            if (empty($tableName)) {
                continue;
            }
            $this->addSql(sprintf('ALTER TABLE %s DROP payment_document_number', $tableName));
        }
    }

    private function getOrderExtensionTables(): array
    {
        $tables = $this->connection->executeQuery(
            "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = SCHEMA() AND TABLE_NAME LIKE '%\_order_extension';"
        )->fetchAll(PDO::FETCH_COLUMN);
        $tables[] = 'order_extension';

        return $tables;
    }
}
Komentarze wyłączone