<commit_before><?php
class Vps_Test_SeparateDb
{
    private static $_dbName = null;

    public static function getDbName()
    {
        return Vps_Test_SeparateDb::$_dbName;
    }

    public static function setDbAndCreateCookie($dbName)
    {
        setcookie('test_special_db', $dbName, time()+(10*60), '/');
        Vps_Registry::set('db', Vps_Test::getTestDb($dbName));
        Vps_Model_Abstract::clearInstances();
    }

    public static function setDbFromCookie()
    {
        if (!empty($_COOKIE['test_special_db'])) {
            Vps_Registry::set('db', Vps_Test::getTestDb($_COOKIE['test_special_db']));
        }
    }

    /**
     * Muss im Test separat aufgerufen werden wenn für den Test eine eigene DB
     * benötigt wird
     */
    public static function createSeparateTestDb($bootstrapFile)
    {
        if (substr($bootstrapFile, 0, 1) != '/') {
            throw new Vps_Exception("First argument 'bootstrapFile' must be an absolute path to the file");
        }

        $section = preg_replace('/[^a-zA-Z0-9]/', '', Vps_Registry::get('config')->getSectionName());
        Vps_Test_SeparateDb::$_dbName = 'test_'.$section.date('_Ymd_His_').substr(uniqid(), -5);
        $testDb = Vps_Test::getTestDb();
        $testDb->query('CREATE DATABASE '.Vps_Test_SeparateDb::getDbName());

        Vps_Registry::set('db', Vps_Test::getTestDb(Vps_Test_SeparateDb::getDbName()));

        $ret = null;
        passthru('mysql '.Vps_Test_SeparateDb::getDbName().' < '.$bootstrapFile, $ret);
        if ($ret != 0) throw new Vps_Exception("bootstrap file could not be processed through mysql");

        Vps_Model_Abstract::clearInstances();
    }

    public static function restoreTestDb()
    {
        $dbName = Vps_Test_SeparateDb::getDbName();
        if ($dbName) {
            $testDb = Vps_Test::getTestDb();
            // alte test-dbs löschen
            $testDatabases = $testDb->query('SHOW DATABASES')->fetchAll();
            foreach ($testDatabases as $testDatabase) {
                if (preg_match('/^test_[^_]+_([0-9]+)_[0-9]+_[^_]+$/', $testDatabase['Database'], $matches)) {
                    // test-db löschen wenn sie älter als 2 tage is
                    if ((int)$matches[1] < (int)date('Ymd', time()-(2*86400))) {
                        $testDb->query('DROP DATABASE '.$testDatabase['Database']);
                    }
                }
            }
            $testDb->query('DROP DATABASE '.$dbName);
            Vps_Test_SeparateDb::$_dbName = null;
            Vps_Registry::set('db', $testDb);
        }
    }
}
<commit_msg>allow setting testDb per GET, for easier debugging<commit_after><?php
class Vps_Test_SeparateDb
{
    private static $_dbName = null;

    public static function getDbName()
    {
        return Vps_Test_SeparateDb::$_dbName;
    }

    public static function setDbAndCreateCookie($dbName)
    {
        setcookie('test_special_db', $dbName, time()+(10*60), '/');
        Vps_Registry::set('db', Vps_Test::getTestDb($dbName));
        Vps_Model_Abstract::clearInstances();
    }

    public static function setDbFromCookie()
    {
        if (!empty($_GET['test_special_db'])) {
            self::setDbAndCreateCookie($_GET['test_special_db']);
        }
        if (!empty($_COOKIE['test_special_db'])) {
            Vps_Registry::set('db', Vps_Test::getTestDb($_COOKIE['test_special_db']));
        }
    }

    /**
     * Muss im Test separat aufgerufen werden wenn für den Test eine eigene DB
     * benötigt wird
     */
    public static function createSeparateTestDb($bootstrapFile)
    {
        if (substr($bootstrapFile, 0, 1) != '/') {
            throw new Vps_Exception("First argument 'bootstrapFile' must be an absolute path to the file");
        }

        $section = preg_replace('/[^a-zA-Z0-9]/', '', Vps_Registry::get('config')->getSectionName());
        Vps_Test_SeparateDb::$_dbName = 'test_'.$section.date('_Ymd_His_').substr(uniqid(), -5);
        $testDb = Vps_Test::getTestDb();
        $testDb->query('CREATE DATABASE '.Vps_Test_SeparateDb::getDbName());

        Vps_Registry::set('db', Vps_Test::getTestDb(Vps_Test_SeparateDb::getDbName()));

        $ret = null;
        passthru('mysql '.Vps_Test_SeparateDb::getDbName().' < '.$bootstrapFile, $ret);
        if ($ret != 0) throw new Vps_Exception("bootstrap file could not be processed through mysql");

        Vps_Model_Abstract::clearInstances();
    }

    public static function restoreTestDb()
    {
        $dbName = Vps_Test_SeparateDb::getDbName();
        if ($dbName) {
            $testDb = Vps_Test::getTestDb();
            // alte test-dbs löschen
            $testDatabases = $testDb->query('SHOW DATABASES')->fetchAll();
            foreach ($testDatabases as $testDatabase) {
                if (preg_match('/^test_[^_]+_([0-9]+)_[0-9]+_[^_]+$/', $testDatabase['Database'], $matches)) {
                    // test-db löschen wenn sie älter als 2 tage is
                    if ((int)$matches[1] < (int)date('Ymd', time()-(2*86400))) {
                        $testDb->query('DROP DATABASE '.$testDatabase['Database']);
                    }
                }
            }
            $testDb->query('DROP DATABASE '.$dbName);
            Vps_Test_SeparateDb::$_dbName = null;
            Vps_Registry::set('db', $testDb);
        }
    }
}

{'fraction_non_alphanumeric': 0.15273342100319368, 'fraction_numerical': 0.00826601540484689, 'mean_word_length': 2.6094915254237288, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, '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': '2606726', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1673, 'n_words': 304}