<commit_before><?php

namespace AC\WebServicesBundle\Fixture;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\PDOConnection;
use Doctrine\DBAL\Driver\PDOSqlite\Driver as SqliteDriver;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Migrations\Version;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\ClassMetadataInfo;

use \Mockery as m;

abstract class CachedSqliteFixture extends CachedFixture
{
    private $baseSchemaPath = null;
    private $dbTemplatePath = null;
    private $migCodeFiles = null;
    private $templateEM = null;

    final protected function loadImpl($container)
    {
        $mainEM = $container->get('doctrine')->getManager();
        $conn = $mainEM->getConnection();
        if (!$conn->getDatabasePlatform() instanceof SqlitePlatform) {
            throw new \RuntimeException("Cannot load sqlite fixture into non-sqlite database");
        }

        if (!is_dir(".tmp")) {
            mkdir(".tmp");
        }
        if (is_null($this->baseSchemaPath)) {
            $this->setupSchemaTemplate();
        }
        if (is_null($this->dbTemplatePath)) {
            $this->dbTemplatePath = $this->setupFixtureTemplate($mainEM);
        }

        $dbPath = $conn->getDatabase();
        if (!is_dir(dirname($dbPath))) {
            mkdir(dirname($dbPath), 0777, true);
        }
        copy($this->dbTemplatePath, $dbPath);
    }

    final protected function getFixtureObjectManager()
    {
        return $this->templateEM;
    }

    private function setupSchemaTemplate()
    {
        $schemaSource = "";
        foreach ($this->migrationCodeFiles() as $codePath) {
            $schemaSource .= file_get_contents($codePath);
        }
        $schemaHash = md5($schemaSource);
        $this->baseSchemaPath = ".tmp/BaseSchema_" . $schemaHash . ".sqlite3";
        if (file_exists($this->baseSchemaPath)) {
            return;
        }

        $this->withLoadingMessage("building schema template", function () {
            try {
                $template = $this->connectDb($this->baseSchemaPath);

                $sql = [];
                foreach ($this->migrationCodeFiles() as $codePath) {
                    require_once($codePath);
                    $migName = pathinfo($codePath, PATHINFO_FILENAME);
                    $migClass = 'Application\Migrations\\' . $migName;

                    // We want to get at the up() method of the migration without
                    // having to pass its constructor our migration configuration.
                    $migMock = m::mock($migClass)->shouldDeferMissing();

                    $versionMock = m::mock('\Doctrine\DBAL\Migrations\Version');
                    $versionMock->shouldReceive('addSql')->andReturnUsing(
                        function ($statements, $params = [], $types = []) use (&$sql) {
                            if (count($params) > 0 || count($types) > 0) {
                                throw new \RuntimeException(
                                    "AliceSqliteFixtureCache can't do addSql calls with params"
                                );
                            }
                            if (!is_array($statements)) {
                                $statements = [$statements];
                            }
                            foreach ($statements as $s) {
                                $sql[] = $s;
                            }
                        }
                    );
                    $versionInserter = function ($mig, $ver) { $mig->version = $ver; };
                    $versionInserter = \Closure::bind($versionInserter, null, $migMock);
                    $versionInserter($migMock, $versionMock);

                    // up() can set up tables via Version->addSql and/or passed $schema
                    $dummySchema = new Schema;
                    $migMock->up($dummySchema);
                    $sql = array_merge($sql, $dummySchema->toSql(new SqlitePlatform));
                }
                $template->exec(implode(";\n", $sql));<commit_msg>In Sqlite schema template generation, now correctly handling schema changes not normally possible for sqlite<commit_after><?php

namespace AC\WebServicesBundle\Fixture;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\PDOConnection;
use Doctrine\DBAL\Driver\PDOSqlite\Driver as SqliteDriver;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Migrations\Version;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\ClassMetadataInfo;

use \Mockery as m;

abstract class CachedSqliteFixture extends CachedFixture
{
    private $baseSchemaPath = null;
    private $dbTemplatePath = null;
    private $migCodeFiles = null;
    private $templateEM = null;

    final protected function loadImpl($container)
    {
        $mainEM = $container->get('doctrine')->getManager();
        $conn = $mainEM->getConnection();
        if (!$conn->getDatabasePlatform() instanceof SqlitePlatform) {
            throw new \RuntimeException("Cannot load sqlite fixture into non-sqlite database");
        }

        if (!is_dir(".tmp")) {
            mkdir(".tmp");
        }
        if (is_null($this->baseSchemaPath)) {
            $this->setupSchemaTemplate();
        }
        if (is_null($this->dbTemplatePath)) {
            $this->dbTemplatePath = $this->setupFixtureTemplate($mainEM);
        }

        $dbPath = $conn->getDatabase();
        if (!is_dir(dirname($dbPath))) {
            mkdir(dirname($dbPath), 0777, true);
        }
        copy($this->dbTemplatePath, $dbPath);
    }

    final protected function getFixtureObjectManager()
    {
        return $this->templateEM;
    }

    private function setupSchemaTemplate()
    {
        $schemaSource = "";
        foreach ($this->migrationCodeFiles() as $codePath) {
            $schemaSource .= file_get_contents($codePath);
        }
        $schemaHash = md5($schemaSource);
        $this->baseSchemaPath = ".tmp/BaseSchema_" . $schemaHash . ".sqlite3";
        if (file_exists($this->baseSchemaPath)) {
            return;
        }

        $this->withLoadingMessage("building schema template", function () {
            try {
                $schema = new Schema;
                $addedSql = [];
                foreach ($this->migrationCodeFiles() as $codePath) {
                    require_once($codePath);
                    $migName = pathinfo($codePath, PATHINFO_FILENAME);
                    $migClass = 'Application\Migrations\\' . $migName;

                    // We want to get at the up() method of the migration without
                    // having to pass its constructor our migration configuration.
                    $migMock = m::mock($migClass)->shouldDeferMissing();

                    $versionMock = m::mock('\Doctrine\DBAL\Migrations\Version');
                    $versionMock->shouldReceive('addSql')->andReturnUsing(
                        function ($statements, $params = [], $types = []) use (&$addedSql) {
                            if (count($params) > 0 || count($types) > 0) {
                                throw new \RuntimeException(
                                    "AliceSqliteFixtureCache can't do addSql calls with params"
                                );
                            }
                            if (!is_array($statements)) {
                                $statements = [$statements];
                            }
                            foreach ($statements as $s) {
                                $addedSql[] = $s;
                            }
                        }
                    );
                    $versionInserter = function ($mig, $ver) { $mig->version = $ver; };
                    $versionInserter = \Closure::bind($versionInserter, null, $migMock);
                    $versionInserter($migMock, $versionMock);

                    $migMock->up($schema);
                }

                $sql = array_merge($schema->toSql(new SqlitePlatform), $addedSql);
                $template = $this->connectDb($this->baseSchemaPath);
                $template->exec(implode(";\n", $sql));
{'fraction_non_alphanumeric': 0.11978021978021978, 'fraction_numerical': 0.0019536019536019536, 'mean_word_length': 1.7672297297297297, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '338938', 'n_tokens_mistral': 2196, 'n_tokens_neox': 2085, 'n_words': 431}