<commit_before><?php

namespace yii\db;

use bizley\migration\table\TableChange;
use bizley\migration\table\TableStructure;
use ReflectionClass;
use ReflectionException;
use yii\base\Component;
use yii\base\InvalidConfigException;
use yii\base\NotSupportedException;
use yii\di\Instance;
use function array_key_exists;
use function get_class;
use function is_array;
use function preg_match;
use function preg_split;
use function str_replace;
use function strpos;
use function trim;

/**
 * Dummy Migration class.
 * This class is used to gather migration details instead of applying them.
 */
class Migration extends Component implements MigrationInterface
{
    use SchemaBuilderTrait;

    public $maxSqlOutputLength;
    public $compact = false;

    /**
     * @var array List of all migration actions in form of 'table' => [array of changes]
     */
    public $changes = [];

    /**
     * @var Connection|array|string
     */
    public $db = 'db';

    /**
     * @throws InvalidConfigException
     * @throws NotSupportedException
     */
    public function init()
    {
        parent::init();

        $this->db = Instance::ensure($this->db, Connection::class);
        $this->db->getSchema()->refresh();
        $this->db->enableSlaves = false;
    }

    /**
     * {@inheritdoc}
     */
    protected function getDb()
    {
        return $this->db;
    }

    public function up()
    {
        if ($this->safeUp() === false) {
            return false;
        }

        return null;
    }

    public function down()
    {
        return null;
    }

    public function safeUp()
    {
    }

    public function safeDown()
    {
    }

    /**
     * Returns extracted columns data.
     * @param array $columns
     * @return array
     * @throws ReflectionException
     */
    protected function extractColumns($columns)
    {
        $schema = [];

        foreach ($columns as $name => $data) {
            $schema[$name] = $this->extractColumn($data);
        }

        return $schema;
    }

    /**
     * Updates column properties based on schema type map.
     * @param string $type
     * @param array $keyToDb
     * @param array $dbToKey
     * @return array
     */
    public function fillTypeMapProperties($type, $keyToDb, $dbToKey)
    {
        $schema = [];

        if (!array_key_exists($type, $keyToDb)) {
            $schema['type'] = $type;
            return $schema;
        }

        $builder = $keyToDb[$type];

        if (strpos($builder, 'NOT NULL') !== false) {
            $schema['isNotNull'] = true;
            $builder = trim(str_replace('NOT NULL', '', $builder));
        }

        if (strpos($builder, 'AUTO_INCREMENT') !== false) {
            $schema['autoIncrement'] = true;
            $builder = trim(str_replace('AUTO_INCREMENT', '', $builder));
        }

        if (strpos($builder, 'AUTOINCREMENT') !== false) {
            $schema['autoIncrement'] = true;
            $builder = trim(str_replace('AUTOINCREMENT', '', $builder));
        }

        if (strpos($builder, 'IDENTITY PRIMARY KEY') !== false) {
            $schema['isPrimaryKey'] = true;
            $builder = trim(str_replace('IDENTITY PRIMARY KEY', '', $builder));
        }

        if (strpos($builder, 'PRIMARY KEY') !== false) {
            $schema['isPrimaryKey'] = true;
            $builder = trim(str_replace('PRIMARY KEY', '', $builder));
        }

        if (strpos($builder, 'UNSIGNED') !== false) {
            $schema['isUnsigned'] = true;
            $builder = trim(str_replace('UNSIGNED', '', $builder));
        }

        preg_match('/^([a-zA-Z ]+)(\(([0-9,]+)\))?$/', $builder, $matches);

        if (array_key_exists($matches[1], $dbToKey)) {
            if (!empty($matches[3])) {
                $schema['length'] = $matches[3];
            }

            $schema['type'] = $dbToKey[$matches[1]];
        }

        return $schema;
    }

    /**
     * Returns extracted column data.
     * @param ColumnSchemaBuilder $type
     * @return array
     * @throws ReflectionException
     */
    protected function extractColumn($type)
    {
        $reflectionClass = new ReflectionClass($type);
        $reflectionProperty = $reflectionClass->getProperty('type');
        $reflectionProperty->setAccessible(true);

        $schema = $this->fillTypeMapProperties(
            $reflectionProperty->getValue($type),
            $this->db->schema->createQueryBuilder()->typeMap,
            $this->db->schema->typeMap
        );

        foreach (['length', 'isNotNull', 'isUnique', 'check', 'default', 'append', 'isUnsigned'] as $property) {
            $reflectionProperty = $reflectionClass->getProperty($property);
            $reflectionProperty->setAccessible(true);

            $value = $reflectionProperty->getValue($type);
            if ($value !== null || !isset($schema[$property])) {
                $schema[$property] = $value;
            }
        }

        $schema['comment'] = empty($type->comment) ? '' : $type->comment;

        return $schema;
    }

    /**
     * Returns raw table name.
     * @param string $table
     * @return string
     */
    public function getRawTableName($table)<commit_msg>Only ColumnSchemaBuilder columns accepted<commit_after><?php

namespace yii\db;

use bizley\migration\table\TableChange;
use bizley\migration\table\TableStructure;
use ReflectionClass;
use ReflectionException;
use yii\base\Component;
use yii\base\InvalidArgumentException;
use yii\base\InvalidConfigException;
use yii\base\NotSupportedException;
use yii\di\Instance;
use function array_key_exists;
use function get_class;
use function is_array;
use function preg_match;
use function preg_split;
use function str_replace;
use function strpos;
use function trim;

/**
 * Dummy Migration class.
 * This class is used to gather migration details instead of applying them.
 */
class Migration extends Component implements MigrationInterface
{
    use SchemaBuilderTrait;

    public $maxSqlOutputLength;
    public $compact = false;

    /**
     * @var array List of all migration actions in form of 'table' => [array of changes]
     */
    public $changes = [];

    /**
     * @var Connection|array|string
     */
    public $db = 'db';

    /**
     * @throws InvalidConfigException
     * @throws NotSupportedException
     */
    public function init()
    {
        parent::init();

        $this->db = Instance::ensure($this->db, Connection::class);
        $this->db->getSchema()->refresh();
        $this->db->enableSlaves = false;
    }

    /**
     * {@inheritdoc}
     */
    protected function getDb()
    {
        return $this->db;
    }

    public function up()
    {
        if ($this->safeUp() === false) {
            return false;
        }

        return null;
    }

    public function down()
    {
        return null;
    }

    public function safeUp()
    {
    }

    public function safeDown()
    {
    }

    /**
     * Returns extracted columns data.
     * @param array $columns
     * @return array
     */
    protected function extractColumns($columns)
    {
        $schema = [];

        foreach ($columns as $name => $data) {
            $schema[$name] = $this->extractColumn($data);
        }

        return $schema;
    }

    /**
     * Updates column properties based on schema type map.
     * @param string $type
     * @param array $keyToDb
     * @param array $dbToKey
     * @return array
     */
    public function fillTypeMapProperties($type, $keyToDb, $dbToKey)
    {
        $schema = [];

        if (!array_key_exists($type, $keyToDb)) {
            $schema['type'] = $type;
            return $schema;
        }

        $builder = $keyToDb[$type];

        if (strpos($builder, 'NOT NULL') !== false) {
            $schema['isNotNull'] = true;
            $builder = trim(str_replace('NOT NULL', '', $builder));
        }

        if (strpos($builder, 'AUTO_INCREMENT') !== false) {
            $schema['autoIncrement'] = true;
            $builder = trim(str_replace('AUTO_INCREMENT', '', $builder));
        }

        if (strpos($builder, 'AUTOINCREMENT') !== false) {
            $schema['autoIncrement'] = true;
            $builder = trim(str_replace('AUTOINCREMENT', '', $builder));
        }

        if (strpos($builder, 'IDENTITY PRIMARY KEY') !== false) {
            $schema['isPrimaryKey'] = true;
            $builder = trim(str_replace('IDENTITY PRIMARY KEY', '', $builder));
        }

        if (strpos($builder, 'PRIMARY KEY') !== false) {
            $schema['isPrimaryKey'] = true;
            $builder = trim(str_replace('PRIMARY KEY', '', $builder));
        }

        if (strpos($builder, 'UNSIGNED') !== false) {
            $schema['isUnsigned'] = true;
            $builder = trim(str_replace('UNSIGNED', '', $builder));
        }

        preg_match('/^([a-zA-Z ]+)(\(([0-9,]+)\))?$/', $builder, $matches);

        if (array_key_exists($matches[1], $dbToKey)) {
            if (!empty($matches[3])) {
                $schema['length'] = $matches[3];
            }

            $schema['type'] = $dbToKey[$matches[1]];
        }

        return $schema;
    }

    /**
     * Returns extracted column data.
     * Since 3.3.0 InvalidArgumentException is thrown for non-ColumnSchemaBuilder $columnData.
     * @param ColumnSchemaBuilder $columnData
     * @return array
     * @throws ReflectionException
     * @throws InvalidArgumentException in case column data is not an instance of ColumnSchemaBuilder
     */
    protected function extractColumn($columnData)
    {
        if (!$columnData instanceof ColumnSchemaBuilder) {
            throw new InvalidArgumentException('Column data must be provided as an instance of yii\db\ColumnSchemaBuilder.');
        }

        $reflectionClass = new ReflectionClass($columnData);
        $reflectionProperty = $reflectionClass->getProperty('type');
        $reflectionProperty->setAccessible(true);

        $schema = $this->fillTypeMapProperties(
            $reflectionProperty->getValue($columnData),
            $this->db->schema->createQueryBuilder()->typeMap,
            $this->db->schema->typeMap
        );

        foreach (['length', 'isNotNull', 'isUnique', 'check', 'default', 'append', 'isUnsigned'] as $property) {
            $reflectionProperty = $reflectionClass->getProperty($property);
            $reflectionProperty->setAccessible(true);

            $value = $reflectionProperty->getValue($columnData);
            if ($value !== null || !isset($schema[$property])) {
                $schema[$property] = $value;
            }
        }

        $schema['comment'] = empty($columnData->comment) ? '' : $columnData->comment;

        return $schema;
    }

    /**
     * Returns raw table name.
     * @param string $table
     * @return string
     */
    public function getRawTableName($table)
{'fraction_non_alphanumeric': 0.14254082479933572, 'fraction_numerical': 0.0013838915029061722, 'mean_word_length': 2.331284572833436, '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': '321172', 'n_tokens_mistral': 3117, 'n_tokens_neox': 3031, 'n_words': 700}