49 lines
1.6 KiB
PHP
49 lines
1.6 KiB
PHP
<?php
|
|
namespace Misuzu;
|
|
|
|
use Index\Config\Config;
|
|
use Index\Db\{DbBackends,DbConnection};
|
|
use Index\Db\Migration\{DbMigrationManager,DbMigrationRepo,FsDbMigrationRepo};
|
|
use Index\Http\Routing\{RouteHandler,RouteHandlerCommon};
|
|
use Index\Http\Routing\Filters\PrefixFilter;
|
|
|
|
class DatabaseContext implements RouteHandler {
|
|
use RouteHandlerCommon;
|
|
|
|
public private(set) DbConnection $conn;
|
|
|
|
public function __construct(
|
|
#[\SensitiveParameter] string $dsn
|
|
) {
|
|
$this->conn = DbBackends::create($dsn);
|
|
$this->conn->execute(<<<SQL
|
|
SET SESSION
|
|
time_zone = '+00:00',
|
|
sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
|
|
SQL);
|
|
}
|
|
|
|
public function getQueryCount(): int {
|
|
$result = $this->conn->query("SHOW SESSION STATUS LIKE 'Questions'");
|
|
return $result->next() ? $result->getInteger(1) : 0;
|
|
}
|
|
|
|
public function createMigrationManager(): DbMigrationManager {
|
|
return new DbMigrationManager($this->conn, sprintf('msz_%s', DbMigrationManager::DEFAULT_TABLE));
|
|
}
|
|
|
|
public function createMigrationRepo(): DbMigrationRepo {
|
|
return new FsDbMigrationRepo(Misuzu::PATH_DATABASE);
|
|
}
|
|
|
|
public function getMigrateLockPath(): string {
|
|
return sys_get_temp_dir() . '/misuzu-migration-' . hash('sha256', Misuzu::PATH_ROOT) . '.lock';
|
|
}
|
|
|
|
/** @return void|int */
|
|
#[PrefixFilter('/')]
|
|
public function filterMigrate() {
|
|
if(is_file($this->getMigrateLockPath()))
|
|
return 503;
|
|
}
|
|
}
|