mirror of
https://github.com/danog/MadelineProto.git
synced 2024-11-27 04:35:12 +01:00
Use integer keys in SQL databases where appropriate, migrate legacy keys
This commit is contained in:
parent
a745959224
commit
559a0cd209
@ -20,6 +20,7 @@ use danog\MadelineProto\Magic;
|
||||
use danog\MadelineProto\Settings\Database\DriverDatabaseAbstract;
|
||||
use danog\MadelineProto\Settings\Database\Mysql;
|
||||
use danog\MadelineProto\Settings\Database\SerializerType;
|
||||
use danog\MadelineProto\Settings\Database\SqlAbstract;
|
||||
use danog\MadelineProto\Settings\DatabaseAbstract;
|
||||
|
||||
/**
|
||||
@ -47,8 +48,13 @@ final class DbPropertiesFactory
|
||||
),
|
||||
'innerMadelineProto' => false,
|
||||
'cacheTtl' => $dbSettings->getCacheTtl(),
|
||||
'intKey' => false,
|
||||
], $config);
|
||||
|
||||
if ($dbSettings instanceof SqlAbstract) {
|
||||
$dbSettings->intKey = $config['intKey'];
|
||||
}
|
||||
|
||||
if ($config['innerMadelineProto']
|
||||
&& $config['serializer'] !== SerializerType::IGBINARY
|
||||
&& $config['serializer'] !== SerializerType::SERIALIZE
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
namespace danog\MadelineProto\Db;
|
||||
|
||||
use Amp\Sql\Result;
|
||||
use Amp\Sql\SqlResult;
|
||||
use danog\MadelineProto\Db\Driver\Mysql;
|
||||
use danog\MadelineProto\Exception;
|
||||
use danog\MadelineProto\Logger;
|
||||
@ -88,7 +88,7 @@ final class MysqlArray extends SqlArray
|
||||
*
|
||||
* @psalm-param self::STATEMENT_* $stmt
|
||||
*/
|
||||
protected function execute(string $sql, array $params = []): Result
|
||||
protected function execute(string $sql, array $params = []): SqlResult
|
||||
{
|
||||
foreach ($params as $key => $value) {
|
||||
$value = $this->pdo->quote($value);
|
||||
@ -115,10 +115,13 @@ final class MysqlArray extends SqlArray
|
||||
protected function prepareTable(): void
|
||||
{
|
||||
//Logger::log("Creating/checking table {$this->table}", Logger::WARNING);
|
||||
\assert($this->dbSettings instanceof DatabaseMysql);
|
||||
$keyType = $this->dbSettings->intKey ? 'BIGINT' : 'VARCHAR(255)';
|
||||
|
||||
$this->db->query("
|
||||
CREATE TABLE IF NOT EXISTS `{$this->table}`
|
||||
(
|
||||
`key` VARCHAR(255) NOT NULL,
|
||||
`key` $keyType NOT NULL,
|
||||
`value` LONGBLOB NULL,
|
||||
`ts` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`key`)
|
||||
@ -127,7 +130,9 @@ final class MysqlArray extends SqlArray
|
||||
CHARACTER SET 'utf8mb4'
|
||||
COLLATE 'utf8mb4_general_ci'
|
||||
");
|
||||
\assert($this->dbSettings instanceof DatabaseMysql);
|
||||
if ($this->dbSettings->intKey) {
|
||||
$this->db->query("ALTER TABLE `{$this->table}` MODIFY `key` BIGINT");
|
||||
}
|
||||
if ($this->dbSettings->getOptimizeIfWastedGtMb() !== null) {
|
||||
try {
|
||||
$database = $this->dbSettings->getDatabase();
|
||||
|
@ -19,6 +19,7 @@ namespace danog\MadelineProto\Db;
|
||||
use danog\MadelineProto\Db\Driver\Postgres;
|
||||
use danog\MadelineProto\Exception;
|
||||
use danog\MadelineProto\Logger;
|
||||
use danog\MadelineProto\Settings\Database\Postgres as DatabasePostgres;
|
||||
use danog\MadelineProto\Settings\Database\SerializerType;
|
||||
|
||||
/**
|
||||
@ -90,14 +91,18 @@ final class PostgresArray extends PostgresArrayBytea
|
||||
protected function prepareTable(): void
|
||||
{
|
||||
//Logger::log("Creating/checking table {$this->table}", Logger::WARNING);
|
||||
|
||||
\assert($this->dbSettings instanceof DatabasePostgres);
|
||||
$keyType = $this->dbSettings->intKey ? 'BIGINT' : 'VARCHAR(255)';
|
||||
$this->db->query("
|
||||
CREATE TABLE IF NOT EXISTS \"{$this->table}\"
|
||||
(
|
||||
\"key\" VARCHAR(255) PRIMARY KEY NOT NULL,
|
||||
\"key\" $keyType PRIMARY KEY NOT NULL,
|
||||
\"value\" BYTEA NOT NULL
|
||||
);
|
||||
");
|
||||
if ($this->dbSettings->intKey) {
|
||||
$this->db->query("ALTER TABLE \"{$this->table}\" MODIFY \"key\" BIGINT");
|
||||
}
|
||||
}
|
||||
|
||||
protected function moveDataFromTableToTable(string $from, string $to): void
|
||||
|
@ -16,8 +16,8 @@
|
||||
|
||||
namespace danog\MadelineProto\Db;
|
||||
|
||||
use Amp\Sql\Pool;
|
||||
use Amp\Sql\Result;
|
||||
use Amp\Sql\SqlConnectionPool;
|
||||
use Amp\Sql\SqlResult;
|
||||
|
||||
/**
|
||||
* Generic SQL database backend.
|
||||
@ -30,7 +30,7 @@ use Amp\Sql\Result;
|
||||
*/
|
||||
abstract class SqlArray extends DriverArray
|
||||
{
|
||||
protected Pool $db;
|
||||
protected SqlConnectionPool $db;
|
||||
|
||||
protected const SQL_GET = 0;
|
||||
protected const SQL_SET = 1;
|
||||
@ -148,7 +148,7 @@ abstract class SqlArray extends DriverArray
|
||||
*
|
||||
* @psalm-param self::STATEMENT_* $stmt
|
||||
*/
|
||||
protected function execute(string $sql, array $params = []): Result
|
||||
protected function execute(string $sql, array $params = []): SqlResult
|
||||
{
|
||||
return $this->db->prepare($sql)->execute($params);
|
||||
}
|
||||
|
@ -388,9 +388,9 @@ final class MTProto implements TLCallback, LoggerGetter, SettingsGetter
|
||||
* @see DbPropertiesFactory
|
||||
*/
|
||||
protected static array $dbProperties = [
|
||||
'sponsoredMessages' => ['innerMadelineProto' => true],
|
||||
'sponsoredMessages' => ['innerMadelineProto' => true, 'intKey' => true],
|
||||
'channelParticipants' => ['innerMadelineProto' => true],
|
||||
'getUpdatesQueue' => ['innerMadelineProto' => true],
|
||||
'getUpdatesQueue' => ['innerMadelineProto' => true, 'intKey' => true],
|
||||
'session' => ['innerMadelineProto' => true, 'enableCache' => false, 'optimizeIfWastedGtMb' => 1],
|
||||
];
|
||||
|
||||
|
@ -67,7 +67,7 @@ final class MinDatabase implements TLCallback
|
||||
* @see DbPropertiesFactory
|
||||
*/
|
||||
protected static array $dbProperties = [
|
||||
'db' => ['innerMadelineProto' => true],
|
||||
'db' => ['innerMadelineProto' => true, 'intKey' => true],
|
||||
];
|
||||
|
||||
private LocalKeyedMutex $localMutex;
|
||||
|
@ -86,10 +86,12 @@ final class PeerDatabase implements TLCallback
|
||||
'db' => [
|
||||
'innerMadelineProto' => true,
|
||||
'table' => 'MTProto_chats',
|
||||
'intKey' => true,
|
||||
],
|
||||
'fullDb' => [
|
||||
'innerMadelineProto' => true,
|
||||
'table' => 'MTProto_full_chats',
|
||||
'intKey' => true,
|
||||
],
|
||||
'usernames' => [
|
||||
'innerMadelineProto' => true,
|
||||
|
@ -380,7 +380,7 @@ trait PeerHandler
|
||||
if (is_numeric($id)) {
|
||||
$id = (int) $id;
|
||||
Assert::true($id !== 0, "An invalid ID was specified!");
|
||||
if (DialogId::getType($id) === DialogId::SECRET_CHAT) {
|
||||
if (DialogId::isSecretChat($id)) {
|
||||
$id = $this->getSecretChat($id)->otherID;
|
||||
}
|
||||
if (!$this->peerDatabase->isset($id)) {
|
||||
@ -645,7 +645,7 @@ trait PeerHandler
|
||||
public function getFullInfo(mixed $id): array
|
||||
{
|
||||
$partial = $this->getInfo($id);
|
||||
if (DialogId::getType($partial['bot_api_id']) === DialogId::SECRET_CHAT) {
|
||||
if (DialogId::isSecretChat($partial['bot_api_id'])) {
|
||||
return $partial;
|
||||
}
|
||||
$full = $this->peerDatabase->getFull($partial['bot_api_id']);
|
||||
|
@ -61,9 +61,9 @@ final class SecretChatController implements Stringable
|
||||
* @see DbPropertiesFactory
|
||||
*/
|
||||
protected static array $dbProperties = [
|
||||
'incoming' => ['innerMadelineProto' => true],
|
||||
'outgoing' => ['innerMadelineProto' => true],
|
||||
'randomIdMap' => ['innerMadelineProto' => true],
|
||||
'incoming' => ['innerMadelineProto' => true, 'intKey' => true],
|
||||
'outgoing' => ['innerMadelineProto' => true, 'intKey' => true],
|
||||
'randomIdMap' => ['innerMadelineProto' => true, 'intKey' => true],
|
||||
];
|
||||
|
||||
/**
|
||||
|
@ -49,6 +49,11 @@ abstract class SqlAbstract extends DriverDatabaseAbstract
|
||||
*/
|
||||
protected string $uri = 'tcp://127.0.0.1';
|
||||
|
||||
/**
|
||||
* @internal Internal value used by inner settings, do not change.
|
||||
*/
|
||||
public bool $intKey = false;
|
||||
|
||||
/**
|
||||
* Get maximum connection limit.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user