<commit_before><?php

namespace Mtt\BlogBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\ArrayInput;

class DatabasePrepareCommand extends ContainerAwareCommand
{
    /**
     * @var \Symfony\Component\Console\Output\OutputInterface
     */
    protected $output;

    protected function configure()
    {
        $this->setName('mtt:database:prepare')
            ->setDescription('Preparation DB from testing')
            ->addOption('without-fixtures', null, InputOption::VALUE_NONE, 'If set, fixtures is not loaded');
    }

    /**
     * @param \Symfony\Component\Console\Output\OutputInterface $output
     */
    public function setOutput(OutputInterface $output)
    {
        $this->output = $output;
    }

    /**
     * @return \Symfony\Component\Console\Output\OutputInterface
     */
    public function getOutput()
    {
        return $this->output;
    }

    /**
     * @param \Symfony\Component\Console\Input\InputInterface $input
     * @param \Symfony\Component\Console\Output\OutputInterface $output
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        if ($input->getOption('env') != 'test') {
            $output->writeln("<error>This operation cannot be executed in a test environment.</error>");
        } else {
            $this->getApplication()->setAutoExit(false);
            $this->setOutput($output);

            $this->dropDb();

            $connection = $this->getContainer()->get('doctrine')->getConnection();
            if ($connection->isConnected()) {
                $connection->close();
            }

            $this->createDb();
            $this->schemaDb();

            if (!$input->getOption('without-fixtures')) {
                $this->fixturesDb();
            }

        }
    }

    /**
     * @return int|string
     */
    protected function dropDb()
    {
        $command = $this->getApplication()->find('doctrine:database:drop');

        $arguments = array(
            'command' => 'doctrine:database:drop',
            '--force' => true,
        );

        return $command->run(new ArrayInput($arguments), $this->getOutput());
    }

    /**
     * @return int|string
     */
    protected function createDb()
    {
        $command = $this->getApplication()->find('doctrine:database:create');

        $arguments = array(
            'command' => 'doctrine:database:create',
        );

        return $command->run(new ArrayInput($arguments), $this->getOutput());
    }

    /**
     * @return int|string
     */
    protected function schemaDb()
    {
        $command = $this->getApplication()->find('doctrine:schema:create');

        $arguments = array(
            'command' => 'doctrine:schema:create',
        );

        return $command->run(new ArrayInput($arguments), $this->getOutput());
    }

    /**
     * @return int|string
     */
    protected function fixturesDb()
    {
        $command = $this->getApplication()->find('doctrine:fixtures:load');

        $arguments = array(
            'command' => 'doctrine:fixtures:load'
        );

        $input = new ArrayInput($arguments);
        $input->setInteractive(false);
        $command->run($input, $this->getOutput());
    }

}<commit_msg>task 36 - update_count_comments procedure in DB<commit_after><?php

namespace Mtt\BlogBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\ArrayInput;

class DatabasePrepareCommand extends ContainerAwareCommand
{
    /**
     * @var \Symfony\Component\Console\Output\OutputInterface
     */
    protected $output;

    protected function configure()
    {
        $this->setName('mtt:database:prepare')
            ->setDescription('Preparation DB from testing')
            ->addOption('without-fixtures', null, InputOption::VALUE_NONE, 'If set, fixtures is not loaded');
    }

    /**
     * @param \Symfony\Component\Console\Output\OutputInterface $output
     */
    public function setOutput(OutputInterface $output)
    {
        $this->output = $output;
    }

    /**
     * @return \Symfony\Component\Console\Output\OutputInterface
     */
    public function getOutput()
    {
        return $this->output;
    }

    /**
     * @param \Symfony\Component\Console\Input\InputInterface $input
     * @param \Symfony\Component\Console\Output\OutputInterface $output
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        if ($input->getOption('env') != 'test') {
            $output->writeln("<error>This operation cannot be executed in a test environment.</error>");
        } else {
            $this->getApplication()->setAutoExit(false);
            $this->setOutput($output);

            $this->dropDb();

            $connection = $this->getContainer()->get('doctrine')->getConnection();
            if ($connection->isConnected()) {
                $connection->close();
            }

            $this->createDb();
            $this->schemaDb();
            $this->addRoutines();

            if (!$input->getOption('without-fixtures')) {
                $this->fixturesDb();
            }

        }
    }

    /**
     * @return int|string
     */
    protected function dropDb()
    {
        $command = $this->getApplication()->find('doctrine:database:drop');

        $arguments = array(
            'command' => 'doctrine:database:drop',
            '--force' => true,
        );

        return $command->run(new ArrayInput($arguments), $this->getOutput());
    }

    /**
     * @return int|string
     */
    protected function createDb()
    {
        $command = $this->getApplication()->find('doctrine:database:create');

        $arguments = array(
            'command' => 'doctrine:database:create',
        );

        return $command->run(new ArrayInput($arguments), $this->getOutput());
    }

    /**
     * @return int|string
     */
    protected function schemaDb()
    {
        $command = $this->getApplication()->find('doctrine:schema:create');

        $arguments = array(
            'command' => 'doctrine:schema:create',
        );

        return $command->run(new ArrayInput($arguments), $this->getOutput());
    }

    /**
     * @return int|string
     */
    protected function fixturesDb()
    {
        $command = $this->getApplication()->find('doctrine:fixtures:load');

        $arguments = array(
            'command' => 'doctrine:fixtures:load'
        );

        $input = new ArrayInput($arguments);
        $input->setInteractive(false);
        $command->run($input, $this->getOutput());
    }

    /**
     * add routines
     */
    protected function addRoutines()
    {
        $connection = $this->getContainer()->get('doctrine')->getConnection();

        $rawSql = <<<RAWSQL
CREATE PROCEDURE `update_comments_count`(IN topicID INT UNSIGNED)
BEGIN
    DECLARE count_comments INT DEFAULT 0;

    SELECT COUNT( id ) INTO count_comments
        FROM `comments`
        WHERE (`post_id` = topicID) AND (`deleted` = 0);

    UPDATE `posts_counts`
        SET `comments` = count_comments
        WHERE `posts_counts`.`post_id` = topicID;

    SELECT count_comments;
END;

RAWSQL;

        $connection->exec($rawSql);

        $this->getOutput()->writeln("Create update_comments_count routine");
    }

}
{'fraction_non_alphanumeric': 0.1469631941734946, 'fraction_numerical': 0.0005202236961893615, 'mean_word_length': 2.4376396960214572, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 98, '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': '5320895', 'n_tokens_mistral': 2157, 'n_tokens_neox': 2079, 'n_words': 370}