forked from GNUsocial/gnu-social
[DATABASE] Change the way defaults are loaded, bulk insert, reload everything in debug mode, only on http requests (not command line)
This commit is contained in:
parent
ac68436b0b
commit
ccc0d7d401
@ -425,37 +425,37 @@ abstract class DefaultSettings
|
|||||||
'performance' => ['high' => false], // disable some features for higher performance; default false
|
'performance' => ['high' => false], // disable some features for higher performance; default false
|
||||||
];
|
];
|
||||||
|
|
||||||
if ($_ENV['APP_DEBUG']) {
|
self::loadDefaults(!$_ENV['APP_DEBUG']);
|
||||||
self::loadDefaults();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function loadDefaults(bool $optimize = false) {
|
public static function loadDefaults(bool $optimize = false)
|
||||||
$schemaManager = DB::getConnection()->getSchemaManager();
|
{
|
||||||
if (!$schemaManager->tablesExist(['config'])) {
|
if (!isset($_ENV['HTTPS']) || !isset($_ENV['HTTP_HOST']) || $optimize) {
|
||||||
if (!isset($_SERVER['TERM'])) {
|
return;
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$repo = DB::getRepository('\App\Entity\Config');
|
// So, since not all DBMSs support multi row inserts, doctrine doesn't implement it.
|
||||||
if (!empty($repo)) {
|
// The difference between this and the below version is that the one bellow does 221 queries in 30 to 50ms,
|
||||||
$config = $repo->findAll();
|
// this does 2 in 10 to 15 ms
|
||||||
if (!$optimize || count($config) < count(self::$defaults)) {
|
// In debug mode, delete everything and reinsert, in case defaults changed
|
||||||
foreach (self::$defaults as $section => $def) {
|
DB::getConnection()->executeQuery('delete from config;');
|
||||||
foreach ($def as $setting => $value) {
|
$sql = 'insert into config (section, setting, value) values';
|
||||||
if (!isset($config[$section][$setting])) {
|
foreach (self::$defaults as $section => $def) {
|
||||||
$config[$section][$setting]
|
foreach ($def as $setting => $value) {
|
||||||
= DB::getReference('\App\Entity\Config', ['section' => $section, 'setting' => $setting]);
|
$v = serialize($value);
|
||||||
DB::persist($config[$section][$setting]->setValue(serialize($value)));
|
$sql .= " ('{$section}', '{$setting}', '{$v}'),";
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
DB::flush();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -76,6 +76,13 @@ class Config
|
|||||||
|
|
||||||
// }}} Autocode
|
// }}} Autocode
|
||||||
|
|
||||||
|
public function __construct($sec, $set, $val)
|
||||||
|
{
|
||||||
|
$this->section = $sec;
|
||||||
|
$this->setting = $set;
|
||||||
|
$this->value = $val;
|
||||||
|
}
|
||||||
|
|
||||||
public static function schemaDef(): array
|
public static function schemaDef(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
Loading…
Reference in New Issue
Block a user