<commit_before><?php
/**
 * Created by IntelliJ IDEA.
 * User: natronite
 * Date: 19/07/14
 * Time: 19:27
 */

namespace Natronite\Caribou\Controller;

use Natronite\Caribou\Model\Descriptor;
use Natronite\Caribou\Model\Table;
use Natronite\Caribou\Util\Connection;

abstract class TableMigration
{
    /** @var  Table */
    private $table;

    final public function getTableName()
    {
        return $this->table->getName();
    }

    final public function migrateTable()
    {
        // Test if table exists
        if (Connection::tableExists($this->table->getName())) {

            $current = Connection::getTable($this->table->getName());
            $alter = Table::computeAlter($current, $this->table);
            if ($alter) {
                echo "Migrating table " . $this->table->getName() . "\n";
                Connection::query($alter);
            }

        } else {
            // create table
            echo "Creating table " . $this->table->getName() . "\n";
            Connection::query($this->table->getCreateSql());
        }
    }

    final public function createIndexes()
    {
        $current = Connection::getTableIndices($this->table->getName());
        $new = $this->table->getIndexes();

        $this->create($new, $current, $this->table->getName());
    }

    final public function createReferences()
    {
        $current = Connection::getTableReferences($this->table->getName());
        $new = $this->table->getReferences();

        $this->create($new, $current, $this->table->getName());
    }

    final public function dropIndexes()
    {
        $current = Connection::getTableIndices($this->table->getName());
        $new = $this->table->getIndexes();

        $this->drop($current, $new, $this->table->getName(), 'INDEX');
    }

    final public function dropReferences()
    {
        $current = Connection::getTableReferences($this->table->getName());
        $new = $this->table->getReferences();

        $this->drop($current, $new, $this->table->getName(), 'FOREIGN KEY');
    }

    final protected function query($query)
    {
        return Connection::query($query);
    }

    /**
     * @param mixed $table
     */
    final public function setTable($table)
    {
        $this->table = $table;
    }

    private function create(array $array1, array $array2, $tableName)
    {
        $drop = $this->arrayDiffNamed($array1, $array2);

        if (!empty($drop)) {
            // Prepare sql statement
            $query = "ALTER TABLE `" . $tableName . "`\n\t";

            /** @var Descriptor $d */
            $statements = [];
            foreach ($drop as $d) {
                $statements[] = $d->getCreateSql();
            }

            $query .= implode(",\n\t", $statements);

            Connection::query($query);
        }
    }

    private function drop(array $array1, array $array2, $tableName, $dropType)
    {
        $drop = $this->arrayDiffNamed($array1, $array2);

        if (!empty($drop)) {
            // Prepare sql statement
            $query = "ALTER TABLE `" . $tableName . "`\n\t";

            /** @var Descriptor $d */
            $statements = [];
            foreach ($drop as $d) {
                $statements[] = "DROP $dropType `" . $d->getName() . "`";
            }

            $query .= implode(",\n\t", $statements);

            Connection::query($query);
        }
    }

    private function arrayDiffNamed(array $array1, array $array2)
    {
        return array_udiff(
            $array1,
            $array2,
            function ($val1, $val2) {
                /** @var Descriptor $val1 */
                /** @var Descriptor $val2 */
                if ($val1->getName() == $val2->getName()) {
                    return 0;
                }
                return $val1->getName() > $val2->getName() ? 1 : -1;
            }
        );
    }
} <commit_msg>fix error where indexes and foreign keys of table where only updated if name of key changed<commit_after><?php
/**
 * Created by IntelliJ IDEA.
 * User: natronite
 * Date: 19/07/14
 * Time: 19:27
 */

namespace Natronite\Caribou\Controller;

use Natronite\Caribou\Model\Descriptor;
use Natronite\Caribou\Model\Table;
use Natronite\Caribou\Util\Connection;

abstract class TableMigration
{
    /** @var  Table */
    private $table;

    final public function getTableName()
    {
        return $this->table->getName();
    }

    final public function migrateTable()
    {
        // Test if table exists
        if (Connection::tableExists($this->table->getName())) {

            $current = Connection::getTable($this->table->getName());
            $alter = Table::computeAlter($current, $this->table);
            if ($alter) {
                echo "Migrating table " . $this->table->getName() . "\n";
                Connection::query($alter);
            }

        } else {
            // create table
            echo "Creating table " . $this->table->getName() . "\n";
            Connection::query($this->table->getCreateSql());
        }
    }

    final public function createIndexes()
    {
        $current = Connection::getTableIndices($this->table->getName());
        $new = $this->table->getIndexes();

        $this->create($new, $current, $this->table->getName());
    }

    final public function createReferences()
    {
        $current = Connection::getTableReferences($this->table->getName());
        $new = $this->table->getReferences();

        $this->create($new, $current, $this->table->getName());
    }

    final public function dropIndexes()
    {
        $current = Connection::getTableIndices($this->table->getName());
        $new = $this->table->getIndexes();

        $this->drop($current, $new, $this->table->getName(), 'INDEX');
    }

    final public function dropReferences()
    {
        $current = Connection::getTableReferences($this->table->getName());
        $new = $this->table->getReferences();

        $this->drop($current, $new, $this->table->getName(), 'FOREIGN KEY');
    }

    final protected function query($query)
    {
        return Connection::query($query);
    }

    /**
     * @param mixed $table
     */
    final public function setTable($table)
    {
        $this->table = $table;
    }

    private function create(array $array1, array $array2, $tableName)
    {
        //$drop = $this->arrayDiffNamed($array1, $array2);
        $drop = $this->arrayDiffSql($array1, $array2);

        if (!empty($drop)) {
            // Prepare sql statement
            $query = "ALTER TABLE `" . $tableName . "`\n\t";

            /** @var Descriptor $d */
            $statements = [];
            foreach ($drop as $d) {
                $statements[] = $d->getCreateSql();
            }

            $query .= implode(",\n\t", $statements);

            Connection::query($query);
        }
    }

    private function drop(array $array1, array $array2, $tableName, $dropType)
    {
        //$drop = $this->arrayDiffNamed($array1, $array2);
        $drop = $this->arrayDiffSql($array1, $array2);

        if (!empty($drop)) {
            // Prepare sql statement
            $query = "ALTER TABLE `" . $tableName . "`\n\t";

            /** @var Descriptor $d */
            $statements = [];
            foreach ($drop as $d) {
                $statements[] = "DROP $dropType `" . $d->getName() . "`";
            }

            $query .= implode(",\n\t", $statements);

            Connection::query($query);
        }
    }

    private function arrayDiffNamed(array $array1, array $array2)
    {
        return array_udiff(
            $array1,
            $array2,
            function ($val1, $val2) {
                /** @var Descriptor $val1 */
                /** @var Descriptor $val2 */
                if ($val1->getName() == $val2->getName()) {
                    return 0;
                }
                return $val1->getName() > $val2->getName() ? 1 : -1;
            }
        );
    }

    private function arrayDiffSql(array $array1, array $array2)
    {
        return array_udiff(
            $array1,
            $array2,
            function ($val1, $val2) {
                if($val1->getCreateSql() == $val2->getCreateSql()){
                    return 0;
                }
                return $val1->getCreateSql() > $val2->getCreateSql() ? 1 : -1;
            }
        );
    }
}
{'fraction_non_alphanumeric': 0.16208133971291866, 'fraction_numerical': 0.00992822966507177, 'mean_word_length': 1.8831034482758622, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 112, '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': '5825773', 'n_tokens_mistral': 2474, 'n_tokens_neox': 2353, 'n_words': 496}