<commit_before><?php

namespace PetrKnap\Php\MigrationTool;

use PetrKnap\Php\MigrationTool\Exception\DatabaseException;
use PetrKnap\Php\MigrationTool\Exception\MigrationFileException;

/**
 * SQL migration tool
 *
 * @author   <NAME> <<EMAIL>>
 * @since    2016-06-22
 * @license  https://github.com/petrknap/php-migrationtool/blob/master/LICENSE MIT
 */
abstract class SqlMigrationTool extends AbstractMigrationTool
{
    const MIGRATION_FILE_PATTERN = '/\.sql$/i';

    const MESSAGE__COULD_NOT_CREATE_TABLE__TABLE = "Could not create migration table {table}";
    const MESSAGE__CREATED_MIGRATION_TABLE__TABLE = "Created migration table {table}";
    const MESSAGE__COULD_NOT_REGISTER_MIGRATION__ID = "Could not register migration {id}";
    const MESSAGE__COULD_NOT_READ_MIGRATION_FILE__PATH = "Could not read migration file {path}";
    const MESSAGE__COULD_NOT_READ_FROM_TABLE__TABLE = "Could not read from table {table}";
    const MESSAGE__YOU_HAVE_AN_ERROR_IN_YOUR_SQL_SYNTAX__PATH = "You have an error in your SQL syntax in {path}";

    /**
     * @inheritdoc
     */
    public function migrate()
    {
        $this->createMigrationTable();

        parent::migrate();
    }

    /**
     * @throws DatabaseException
     */
    protected function createMigrationTable()
    {
        $errmode = $this->getPhpDataObject()->getAttribute(\PDO::ATTR_ERRMODE);
        $this->getPhpDataObject()->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        try {
            /** @noinspection SqlNoDataSourceInspection,SqlDialectInspection */
            $result = $this->getPhpDataObject()->query("SELECT null FROM {$this->getNameOfMigrationTable()} LIMIT 1")->fetchAll();
        } catch (\PDOException $e) {
            if ($e->getCode() === '42S02') {
               $result = false;
            } else {
                throw $e;
            }
        }
        $this->getPhpDataObject()->setAttribute(\PDO::ATTR_ERRMODE, $errmode);

        if (false === $result) {
            /** @noinspection SqlNoDataSourceInspection,SqlDialectInspection */
            $result = $this->getPhpDataObject()->exec(
                "CREATE TABLE IF NOT EXISTS {$this->getNameOfMigrationTable()}" .
                "(" .
                "id VARCHAR(16) NOT NULL," .
                "applied DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP," .
                "PRIMARY KEY (id)" .
                ")"
            );

            if ($result === false) {
                $context = array(
                    "table" => $this->getNameOfMigrationTable()
                );
<commit_msg>Removed PDO attribute change<commit_after><?php

namespace PetrKnap\Php\MigrationTool;

use PetrKnap\Php\MigrationTool\Exception\DatabaseException;
use PetrKnap\Php\MigrationTool\Exception\MigrationFileException;

/**
 * SQL migration tool
 *
 * @author   <NAME> <<EMAIL>>
 * @since    2016-06-22
 * @license  https://github.com/petrknap/php-migrationtool/blob/master/LICENSE MIT
 */
abstract class SqlMigrationTool extends AbstractMigrationTool
{
    const MIGRATION_FILE_PATTERN = '/\.sql$/i';

    const MESSAGE__COULD_NOT_CREATE_TABLE__TABLE = "Could not create migration table {table}";
    const MESSAGE__CREATED_MIGRATION_TABLE__TABLE = "Created migration table {table}";
    const MESSAGE__COULD_NOT_REGISTER_MIGRATION__ID = "Could not register migration {id}";
    const MESSAGE__COULD_NOT_READ_MIGRATION_FILE__PATH = "Could not read migration file {path}";
    const MESSAGE__COULD_NOT_READ_FROM_TABLE__TABLE = "Could not read from table {table}";
    const MESSAGE__YOU_HAVE_AN_ERROR_IN_YOUR_SQL_SYNTAX__PATH = "You have an error in your SQL syntax in {path}";

    /**
     * @inheritdoc
     */
    public function migrate()
    {
        $this->createMigrationTable();

        parent::migrate();
    }

    /**
     * @throws DatabaseException
     */
    protected function createMigrationTable()
    {
        try {
            /** @noinspection SqlNoDataSourceInspection,SqlDialectInspection */
            $result = $this->getPhpDataObject()->query("SELECT null FROM {$this->getNameOfMigrationTable()} LIMIT 1");
            if ($result) {
                $result->closeCursor();
            }
        } catch (\PDOException $e) {
            if ($e->getCode() === '42S02') {
               $result = false;
            } else {
                throw $e;
            }
        }

        if (false === $result) {
            /** @noinspection SqlNoDataSourceInspection,SqlDialectInspection */
            $result = $this->getPhpDataObject()->exec(
                "CREATE TABLE IF NOT EXISTS {$this->getNameOfMigrationTable()}" .
                "(" .
                "id VARCHAR(16) NOT NULL," .
                "applied DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP," .
                "PRIMARY KEY (id)" .
                ")"
            );

            if ($result === false) {
                $context = array(
                    "table" => $this->getNameOfMigrationTable()
                );

{'fraction_non_alphanumeric': 0.11757648470305938, 'fraction_numerical': 0.00599880023995201, 'mean_word_length': 2.459197786998617, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 37, 'https://': 2, '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': '6424243', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1483, 'n_words': 292}