<commit_before><?php

namespace Oro\Bundle\MigrationBundle\Migration;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Oro\Bundle\MigrationBundle\Exception\InvalidNameException;

class MigrationQueryBuilder
{
    const MAX_TABLE_NAME_LENGTH = 30;

    /**
     * @var Connection
     */
    protected $connection;

    /**
     * @param Connection $connection
     */
    public function __construct(Connection $connection)
    {
        $this->connection = $connection;
    }

    /**
     * Gets a connection object this migration query builder works with
     *
     * @return Connection
     */
    public function getConnection()
    {
        return $this->connection;
    }

    /**
     * Gets a list of SQL queries can be used to apply database changes
     *
     * @param Migration[] $migrations
     * @return array
     *   'migration' => class name of a migration
     *   'queries'   => a list of sql queries (a query can be a string or instance of MigrationQuery)
     * @throws InvalidNameException if invalid table or column name is detected
     */
    public function getQueries(array $migrations)
    {
        $result = [];

        $platform   = $this->connection->getDatabasePlatform();
        $fromSchema = $this->getSchema();
        $queryBag   = new QueryBag();
        foreach ($migrations as $migration) {
            $toSchema = clone $fromSchema;

            $this->setMigrationHelpers($migration);
            $migration->up($toSchema, $queryBag);

            $comparator = new Comparator();
            $schemaDiff = $comparator->compare($fromSchema, $toSchema);

            $this->checkTableNameLengths($schemaDiff->newTables, $migration);

            $changedTables = $schemaDiff->changedTables;
            foreach ($changedTables as $tableName => $diff) {
                $this->checkColumnsNameLength(
                    $tableName,
                    array_values($diff->addedColumns),
                    $migration
                );
            }

            $queries = array_merge(
                $queryBag->getPreSqls(),
                $schemaDiff->toSql($platform),
                $queryBag->getPostSqls()
            );

            $result[] = [
                'migration' => get_class($migration),
                'queries'   => $queries
            ];

            $fromSchema = $toSchema;
            $queryBag->clear();
        }

        return $result;
    }

    /**
     * Gets a database schema
     *
     * @return Schema
     */
    protected function getSchema()
    {
        return $this->connection->getSchemaManager()->createSchema();
    }

    /**
     * Sets helpers to the given migration
     *
     * @param Migration $migration
     */
    protected function setMigrationHelpers(Migration $migration)
    {
    }

    /**
     * @param Table[]   $tables
     * @param Migration $migration
     * @throws InvalidNameException
     */
    protected function checkTableNameLengths($tables, Migration $migration)
    {
        foreach ($tables as $table) {
            if (strlen($table->getName()) > self::MAX_TABLE_NAME_LENGTH) {
                throw new InvalidNameException(
                    sprintf(
                        'Max table name length is %s. Please correct "%s" table in "%s" migration',
                        self::MAX_TABLE_NAME_LENGTH,
                        $table->getName(),
                        get_class($migration)
                    )<commit_msg>BAP-3339: Migrations for extended entities<commit_after><?php

namespace Oro\Bundle\MigrationBundle\Migration;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Oro\Bundle\MigrationBundle\Exception\InvalidNameException;

class MigrationQueryBuilder
{
    const MAX_TABLE_NAME_LENGTH = 30;

    /**
     * @var Connection
     */
    protected $connection;

    /**
     * @param Connection $connection
     */
    public function __construct(Connection $connection)
    {
        $this->connection = $connection;
    }

    /**
     * Gets a connection object this migration query builder works with
     *
     * @return Connection
     */
    public function getConnection()
    {
        return $this->connection;
    }

    /**
     * Gets a list of SQL queries can be used to apply database changes
     *
     * @param Migration[] $migrations
     * @return array
     *   'migration' => class name of a migration
     *   'queries'   => a list of sql queries (a query can be a string or instance of MigrationQuery)
     * @throws InvalidNameException if invalid table or column name is detected
     */
    public function getQueries(array $migrations)
    {
        $result = [];

        $platform   = $this->connection->getDatabasePlatform();
        $fromSchema = $this->getSchema();
        $queryBag   = new QueryBag();
        foreach ($migrations as $migration) {
            $toSchema = clone $fromSchema;

            $this->setExtensions($migration);
            $migration->up($toSchema, $queryBag);

            $comparator = new Comparator();
            $schemaDiff = $comparator->compare($fromSchema, $toSchema);

            $this->checkTableNameLengths($schemaDiff->newTables, $migration);

            $changedTables = $schemaDiff->changedTables;
            foreach ($changedTables as $tableName => $diff) {
                $this->checkColumnsNameLength(
                    $tableName,
                    array_values($diff->addedColumns),
                    $migration
                );
            }

            $queries = array_merge(
                $queryBag->getPreSqls(),
                $schemaDiff->toSql($platform),
                $queryBag->getPostSqls()
            );

            $result[] = [
                'migration' => get_class($migration),
                'queries'   => $queries
            ];

            $fromSchema = $toSchema;
            $queryBag->clear();
        }

        return $result;
    }

    /**
     * Gets a database schema
     *
     * @return Schema
     */
    protected function getSchema()
    {
        return $this->connection->getSchemaManager()->createSchema();
    }

    /**
     * Sets extensions for the given migration
     *
     * @param Migration $migration
     */
    protected function setExtensions(Migration $migration)
    {
    }

    /**
     * @param Table[]   $tables
     * @param Migration $migration
     * @throws InvalidNameException
     */
    protected function checkTableNameLengths($tables, Migration $migration)
    {
        foreach ($tables as $table) {
            if (strlen($table->getName()) > self::MAX_TABLE_NAME_LENGTH) {
                throw new InvalidNameException(
                    sprintf(
                        'Max table name length is %s. Please correct "%s" table in "%s" migration',
                        self::MAX_TABLE_NAME_LENGTH,
                        $table->getName(),
                        get_class($migration)
                    )
{'fraction_non_alphanumeric': 0.10735641774440273, 'fraction_numerical': 0.001112501738283966, 'mean_word_length': 1.9023405972558516, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5755499', 'n_tokens_mistral': 1913, 'n_tokens_neox': 1857, 'n_words': 444}