<commit_before><?php

namespace LoginCidadao\CoreBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Doctrine\ORM\EntityManager;

class UpgradeCommand extends ContainerAwareCommand
{

    protected function configure()
    {
        $this
            ->setName('lc:upgrade')
            ->setDescription('Perform basic upgrade commands.')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->clearMetadata($output);
        $this->cacheClear($output);
        //$this->installAssets($output);
    }

    /**
     *
     * @return EntityManager
     */
    private function getManager()
    {
        return $this->getContainer()->get('doctrine')->getManager();
    }

    private function clearMetadata(OutputInterface $output)
    {
        $output->writeln("Clearing Doctrine Metadata...");
        $command = $this->getApplication()->find('doctrine:cache:clear-metadata');

        $envs = $this->getEnvsInput();
        foreach ($envs as $env => $input) {
            $cmdOutput  = new BufferedOutput();
            $returnCode = $command->run($input, $cmdOutput);

            if ($returnCode === 0) {
                $out = explode("\n", trim($cmdOutput->fetch()));
                $output->writeln("[$env]\t- ".end($out));
            }
        }
    }

    private function installAssets(OutputInterface $output)
    {
        $output->write("Installing assets...\t");
        $input    = $this->getEnvsInput()['prod'];
        $commands = array('assets:install', 'assetic:dump');
        foreach ($commands as $command) {
            $cmdOutput  = new BufferedOutput();
            $returnCode = $this->getApplication()
                    ->find($command)->run($input, $cmdOutput);

            if ($returnCode !== 0) {
                $output->writeln("[FAIL]");
                $output->writeln("$command failed. Run it separately to find out why.");
                return;
            }
        }
        $output->writeln("[DONE]");
    }

    private function cacheClear(OutputInterface $output)
    {
        $output->write("Clearing cache...\t");
        $input   = new ArrayInput(array('--env' => 'dev', '--no-warmup'));
        $command = $this->getApplication()->find('cache:clear');

        $cmdOutput  = new BufferedOutput();
        $returnCode = $command->run($input, $cmdOutput);

        if ($returnCode !== 0) {
            $output->writeln("[FAIL]");
            $output->writeln("cache:clear command failed. You may need to manually delete the cache folders.");
            return;
        }

        $output->writeln("[DONE]");
        return;

        $envs = $this->getEnvsInput(array('--no-warmup'));
        foreach ($envs as $env => $input) {
            $cmdOutput = new BufferedOutput();
            try {
                $returnCode = $command->run($input, $cmdOutput);
            } catch (\Exception $e) {
                $returnCode = false;
            }

            if ($returnCode !== 0) {
                $output->writeln("[FAIL]");
                $output->writeln("cache:clear command failed. You may need to manually delete the cache folders.");
                return;
            }
            $output->writeln("$env done");
        }
        $output->writeln("[DONE]");
    }

    private function getEnvsInput($custom = array())
    {
        return array(
            'prod' => new ArrayInput(array_merge(array('--env' => 'prod'),
                    $custom)),
            'env' => new ArrayInput(array_merge(array('--env' => 'dev'), $custom))
        );
    }
}<commit_msg>checking for database changes #351<commit_after><?php

namespace LoginCidadao\CoreBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Style\SymfonyStyle;
use Doctrine\ORM\EntityManager;

class UpgradeCommand extends ContainerAwareCommand
{

    protected function configure()
    {
        $this
            ->setName('lc:upgrade')
            ->setDescription('Perform basic upgrade commands.')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $io = new SymfonyStyle($input, $output);
        $io->title("Running upgrade tasks...");
        $this->clearMetadata($io);
        $this->clearCache($io, 'prod');
        $this->checkDatabase($io);
        $this->installAssets($io);
    }

    /**
     *
     * @return EntityManager
     */
    private function getManager()
    {
        return $this->getContainer()->get('doctrine')->getManager();
    }

    private function clearMetadata(SymfonyStyle $io)
    {
        $io->section("Clearing Doctrine Metadata...");
        $command = $this->getApplication()->find('doctrine:cache:clear-metadata');

        $envs = $this->getEnvsInput();
        $io->progressStart(count($envs));
        foreach ($envs as $env => $input) {
            $cmdOutput  = new BufferedOutput();
            $returnCode = $command->run($input, $cmdOutput);

            if ($returnCode !== 0) {
                $io->newLine(2);
                $io->error("Couldn't clear metadata cache on $env");
                return;
            }
            $io->progressAdvance();
        }
        $io->progressFinish();
    }

    private function installAssets(SymfonyStyle $io)
    {
        $io->section("Installing assets...");
        $input    = $this->getEnvsInput('prod');
        $commands = array('assets:install', 'assetic:dump');
        $io->progressStart(count($commands));
        foreach ($commands as $command) {
            $cmdOutput  = new BufferedOutput();
            $returnCode = $this->getApplication()
                    ->find($command)->run($input, $cmdOutput);

            if ($returnCode !== 0) {
                $io->newLine(2);
                $io->error("$command failed. Run it separately to find out why.");
                return;
            }
            $io->progressAdvance();
        }
        $io->progressFinish();
    }

    private function clearCache(SymfonyStyle $io, $env)
    {
        $io->section("Clearing cache...");
        $io->progressStart(1);
        $input   = $this->getEnvsInput($env);
        $command = $this->getApplication()->find('cache:clear');

        $cmdOutput  = new BufferedOutput();
        $returnCode = $command->run($input, $cmdOutput);

        if ($returnCode !== 0) {
            $io->error("cache:clear command failed. You may need to manually delete the cache folders.");
            return;
        }

        $io->progressFinish();
    }

    private function getEnvsInput($env = null)
    {
        $envs = array(
            'prod' => new ArrayInput(array('--env' => 'prod')),
            'dev' => new ArrayInput(array('--env' => 'dev'))
        );

        if ($env === null) {
            return $envs;
        } else {
            return $envs[$env];
        }
    }

    private function checkDatabase(SymfonyStyle $io)
    {
        $io->section("Checking database schema...");
        $cmdOutput = new BufferedOutput();
        $command   = $this->getApplication()->find('doctrine:schema:update');
        $input     = new ArrayInput(array('--env' => 'dev', '--dump-sql' => true));

        $command->run($input, $cmdOutput);

        $output = $cmdOutput->fetch();
        if (strstr($output, 'Nothing to update') !== false) {
            $io->success(trim($output));
            return;
        }

        $this->updateSchema($io, explode("\n", trim($output)));
    }

    private function updateSchema(SymfonyStyle $io, $queries)
    {
        $io->caution("Your database schema needs to be updated. The following queries will be run:");
        $io->listing($queries);
        if ($io->confirm("Should we run this queries now?", false) === false) {
            return;
        }

        $cmdOutput = new BufferedOutput();
        $command   = $this->getApplication()->find('doctrine:schema:update');
        $force     = new ArrayInput(array('--env' => 'dev', '--dump-sql' => true,
            '--force' => true));
        $command->run($force, $cmdOutput);

        $result = $cmdOutput->fetch();
        if (strstr($result, 'Database schema updated successfully!') === false) {
            $io->error("Couldn't update the schema. Run 'doctrine:schema:update' separately to find out why");
        }
        $io->success("Database schema updated successfully!");
    }
}
{'fraction_non_alphanumeric': 0.15672990063233966, 'fraction_numerical': 0.0014679313459801265, 'mean_word_length': 2.220727272727273, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 110, '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': '7585717', 'n_tokens_mistral': 2487, 'n_tokens_neox': 2407, 'n_words': 496}