From a794c28e751de43a32bec000c1a8be9b57ea2eac Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Sat, 16 May 2020 12:33:19 +0000 Subject: [PATCH] [DATABASE] Change the way defaults are loaded, bulk insert, reload everything in debug mode, only on http requests (not command line) --- src/Core/DefaultSettings.php | 52 ++++++++++++++++++------------------ src/Entity/Config.php | 7 +++++ 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/Core/DefaultSettings.php b/src/Core/DefaultSettings.php index b5f89218f0..a30b620f52 100644 --- a/src/Core/DefaultSettings.php +++ b/src/Core/DefaultSettings.php @@ -425,37 +425,37 @@ abstract class DefaultSettings 'performance' => ['high' => false], // disable some features for higher performance; default false ]; - if ($_ENV['APP_DEBUG']) { - self::loadDefaults(); - } + self::loadDefaults(!$_ENV['APP_DEBUG']); } - public static function loadDefaults(bool $optimize = false) { - $schemaManager = DB::getConnection()->getSchemaManager(); - if (!$schemaManager->tablesExist(['config'])) { - if (!isset($_SERVER['TERM'])) { - throw new Exception("Table config doesn't exist, make sure the schema " . - "is correctly created with `bin/console doctrine:schema:create`"); - } else { - return; // We seem to be running form the command line, probably running the above command - } + public static function loadDefaults(bool $optimize = false) + { + if (!isset($_ENV['HTTPS']) || !isset($_ENV['HTTP_HOST']) || $optimize) { + return; } - $repo = DB::getRepository('\App\Entity\Config'); - if (!empty($repo)) { - $config = $repo->findAll(); - if (!$optimize || count($config) < count(self::$defaults)) { - foreach (self::$defaults as $section => $def) { - foreach ($def as $setting => $value) { - if (!isset($config[$section][$setting])) { - $config[$section][$setting] - = DB::getReference('\App\Entity\Config', ['section' => $section, 'setting' => $setting]); - DB::persist($config[$section][$setting]->setValue(serialize($value))); - } - } - } - DB::flush(); + // So, since not all DBMSs support multi row inserts, doctrine doesn't implement it. + // The difference between this and the below version is that the one bellow does 221 queries in 30 to 50ms, + // this does 2 in 10 to 15 ms + // In debug mode, delete everything and reinsert, in case defaults changed + DB::getConnection()->executeQuery('delete from config;'); + $sql = 'insert into config (section, setting, value) values'; + foreach (self::$defaults as $section => $def) { + foreach ($def as $setting => $value) { + $v = serialize($value); + $sql .= " ('{$section}', '{$setting}', '{$v}'),"; } } + $sql = preg_replace('/,$/', ';', $sql); + DB::getConnection()->executeQuery($sql); + + // $repo = DB::getRepository('\App\Entity\Config'); + // $repo->findAll(); + // foreach (self::$defaults as $section => $def) { + // foreach ($def as $setting => $value) { + // DB::persist(new Config($section, $setting, serialize($value))); + // } + // } + // DB::flush(); } } diff --git a/src/Entity/Config.php b/src/Entity/Config.php index 46870d2c69..5bfbef3703 100644 --- a/src/Entity/Config.php +++ b/src/Entity/Config.php @@ -76,6 +76,13 @@ class Config // }}} Autocode + public function __construct($sec, $set, $val) + { + $this->section = $sec; + $this->setting = $set; + $this->value = $val; + } + public static function schemaDef(): array { return [