Update lib/Database.php
Update busytimeout to prevent locks, some more pragmas
This commit is contained in:
parent
597e9c8dfb
commit
e02a0b4e50
1 changed files with 125 additions and 121 deletions
246
lib/Database.php
246
lib/Database.php
|
@ -1,121 +1,125 @@
|
||||||
<?php
|
<?php
|
||||||
class Database
|
class Database
|
||||||
{
|
{
|
||||||
public static $handle = null;
|
public static $handle = null;
|
||||||
|
|
||||||
public function __construct($createDatabase = false)
|
public function __construct($createDatabase = false)
|
||||||
{
|
{
|
||||||
if (self::$handle === null) {
|
if (self::$handle === null) {
|
||||||
self::$handle = new SQLite3(SQLITE_DB);
|
self::$handle = new SQLite3(SQLITE_DB);
|
||||||
self::$handle->enableExceptions(true);
|
self::$handle->enableExceptions(true);
|
||||||
|
self::$handle->busyTimeout(60000);
|
||||||
$initialQueries = [
|
|
||||||
"PRAGMA journal_mode = WAL",
|
$initialQueries = [
|
||||||
"PRAGMA synchronous = OFF",
|
"PRAGMA journal_mode = WAL",
|
||||||
"PRAGMA cache_size = 10000",
|
"PRAGMA synchronous = OFF",
|
||||||
"PRAGMA temp_store = MEMORY"
|
"PRAGMA cache_size = 10000",
|
||||||
];
|
"PRAGMA mmap_size = 30000000000",
|
||||||
|
"PRAGMA temp_store = MEMORY",
|
||||||
self::$handle->exec(implode(";", $initialQueries) . ";");
|
"PRAGMA automatic_index = ON",
|
||||||
|
"PRAGMA optimize"
|
||||||
|
];
|
||||||
if ($createDatabase === true) {
|
|
||||||
$this->createDatabase();
|
self::$handle->exec(implode(";", $initialQueries) . ";");
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
if ($createDatabase === true) {
|
||||||
|
$this->createDatabase();
|
||||||
public function createDatabase()
|
}
|
||||||
{
|
}
|
||||||
$schemaFiles = [
|
}
|
||||||
LIB_MIGRATIONS_DIR . "/_schema.sql",
|
|
||||||
MIGRATIONS_DIR . "/_schema.sql"
|
public function createDatabase()
|
||||||
];
|
{
|
||||||
|
$schemaFiles = [
|
||||||
foreach ($schemaFiles as $schemaFile) {
|
LIB_MIGRATIONS_DIR . "/_schema.sql",
|
||||||
if (file_exists($schemaFile)) {
|
MIGRATIONS_DIR . "/_schema.sql"
|
||||||
$schema = file_get_contents($schemaFile);
|
];
|
||||||
self::$handle->exec($schema);
|
|
||||||
}
|
foreach ($schemaFiles as $schemaFile) {
|
||||||
}
|
if (file_exists($schemaFile)) {
|
||||||
}
|
$schema = file_get_contents($schemaFile);
|
||||||
|
self::$handle->exec($schema);
|
||||||
public function runMigrations()
|
}
|
||||||
{
|
}
|
||||||
$currentTableVersions = [];
|
}
|
||||||
|
|
||||||
$result = self::$handle->query("SELECT `table`,`version` FROM `migrations`");
|
public function runMigrations()
|
||||||
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
|
{
|
||||||
$currentTableVersions[$row["table"]] = $row["version"];
|
$currentTableVersions = [];
|
||||||
}
|
|
||||||
|
$result = self::$handle->query("SELECT `table`,`version` FROM `migrations`");
|
||||||
$migrations = [];
|
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
|
||||||
$migrationPaths = [LIB_MIGRATIONS_DIR, MIGRATIONS_DIR];
|
$currentTableVersions[$row["table"]] = $row["version"];
|
||||||
foreach ($migrationPaths as $migrationPath) {
|
}
|
||||||
if (is_dir($migrationPath)) {
|
|
||||||
$migrations = array_merge($migrations, scandir($migrationPath));
|
$migrations = [];
|
||||||
}
|
$migrationPaths = [LIB_MIGRATIONS_DIR, MIGRATIONS_DIR];
|
||||||
}
|
foreach ($migrationPaths as $migrationPath) {
|
||||||
|
if (is_dir($migrationPath)) {
|
||||||
$migrationCount = 0;
|
$migrations = array_merge($migrations, scandir($migrationPath));
|
||||||
|
}
|
||||||
foreach ($migrations as $migration) {
|
}
|
||||||
if ($migration == "." || $migration == ".." || $migration == "_schema.sql") {
|
|
||||||
continue;
|
$migrationCount = 0;
|
||||||
}
|
|
||||||
|
foreach ($migrations as $migration) {
|
||||||
$migrationParts = explode("-", str_replace(".sql", "", $migration));
|
if ($migration == "." || $migration == ".." || $migration == "_schema.sql") {
|
||||||
$migrationTable = $migrationParts[0];
|
continue;
|
||||||
$migrationVersion = $migrationParts[1];
|
}
|
||||||
|
|
||||||
$run = false;
|
$migrationParts = explode("-", str_replace(".sql", "", $migration));
|
||||||
|
$migrationTable = $migrationParts[0];
|
||||||
if (!isset($currentTableVersions[$migrationTable])) {
|
$migrationVersion = $migrationParts[1];
|
||||||
$run = true;
|
|
||||||
} else {
|
$run = false;
|
||||||
if ($migrationVersion > $currentTableVersions[$migrationTable]) {
|
|
||||||
$run = true;
|
if (!isset($currentTableVersions[$migrationTable])) {
|
||||||
}
|
$run = true;
|
||||||
}
|
} else {
|
||||||
|
if ($migrationVersion > $currentTableVersions[$migrationTable]) {
|
||||||
if ($run === true) {
|
$run = true;
|
||||||
Logger::log("Found migration for table " . $migrationTable . " version " . $migrationVersion, LOGGER::INFO, "Database");
|
}
|
||||||
$possiblePaths = [LIB_MIGRATIONS_DIR, MIGRATIONS_DIR];
|
}
|
||||||
$migrationFile = null;
|
|
||||||
foreach ($possiblePaths as $possiblePath) {
|
if ($run === true) {
|
||||||
if (file_exists($possiblePath . "/" . $migration)) {
|
Logger::log("Found migration for table " . $migrationTable . " version " . $migrationVersion, LOGGER::INFO, "Database");
|
||||||
$migrationFile = $possiblePath . "/" . $migration;
|
$possiblePaths = [LIB_MIGRATIONS_DIR, MIGRATIONS_DIR];
|
||||||
break;
|
$migrationFile = null;
|
||||||
}
|
foreach ($possiblePaths as $possiblePath) {
|
||||||
}
|
if (file_exists($possiblePath . "/" . $migration)) {
|
||||||
|
$migrationFile = $possiblePath . "/" . $migration;
|
||||||
if ($migrationFile === null) {
|
break;
|
||||||
Logger::log("Could not find migration file for table " . $migrationTable . " version " . $migrationVersion, LOGGER::ERROR, "Database");
|
}
|
||||||
continue;
|
}
|
||||||
}
|
|
||||||
|
if ($migrationFile === null) {
|
||||||
$migrationContents = file_get_contents($migrationFile);
|
Logger::log("Could not find migration file for table " . $migrationTable . " version " . $migrationVersion, LOGGER::ERROR, "Database");
|
||||||
if (@self::$handle->exec($migrationContents)) {
|
continue;
|
||||||
self::$handle->exec("INSERT INTO `migrations` (`table`,`version`, `schemafile`, `created`) VALUES ('" . $migrationTable . "'," . $migrationVersion . ",'" . $migration . "', strftime('%Y-%m-%d %H:%M:%S','now'))");
|
}
|
||||||
$migrationCount++;
|
|
||||||
} else {
|
$migrationContents = file_get_contents($migrationFile);
|
||||||
Logger::log("Failed to run migration for table " . $migrationTable . " version " . $migrationVersion . " with error: " . self::$handle->lastErrorMsg(), LOGGER::ERROR, "Database");
|
if (@self::$handle->exec($migrationContents)) {
|
||||||
}
|
self::$handle->exec("INSERT INTO `migrations` (`table`,`version`, `schemafile`, `created`) VALUES ('" . $migrationTable . "'," . $migrationVersion . ",'" . $migration . "', strftime('%Y-%m-%d %H:%M:%S','now'))");
|
||||||
}
|
$migrationCount++;
|
||||||
}
|
} else {
|
||||||
|
Logger::log("Failed to run migration for table " . $migrationTable . " version " . $migrationVersion . " with error: " . self::$handle->lastErrorMsg(), LOGGER::ERROR, "Database");
|
||||||
if ($migrationCount > 0) {
|
}
|
||||||
Logger::log("Ran " . $migrationCount . " migrations", LOGGER::DEBUG, "Database");
|
}
|
||||||
|
}
|
||||||
self::$handle->exec("PRAGMA optimize;");
|
|
||||||
Logger::log("Optimized database", LOGGER::DEBUG, "Database");
|
if ($migrationCount > 0) {
|
||||||
} else {
|
Logger::log("Ran " . $migrationCount . " migrations", LOGGER::DEBUG, "Database");
|
||||||
Logger::log("No migrations to run", LOGGER::DEBUG, "Database");
|
|
||||||
}
|
self::$handle->exec("PRAGMA optimize;");
|
||||||
}
|
Logger::log("Optimized database", LOGGER::DEBUG, "Database");
|
||||||
|
} else {
|
||||||
public function close()
|
Logger::log("No migrations to run", LOGGER::DEBUG, "Database");
|
||||||
{
|
}
|
||||||
self::$handle->close();
|
}
|
||||||
}
|
|
||||||
}
|
public function close()
|
||||||
|
{
|
||||||
|
self::$handle->close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue