From 32c54178955cf10dd9d03a040204bffc1c66ca2e Mon Sep 17 00:00:00 2001 From: Alexander Pankratov Date: Mon, 16 Aug 2021 03:42:18 +0300 Subject: [PATCH] Fix: Don`t connect to previous instance of same db. --- src/danog/MadelineProto/Db/DbArray.php | 2 +- src/danog/MadelineProto/Db/DriverArray.php | 28 ++++++++++++---------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/danog/MadelineProto/Db/DbArray.php b/src/danog/MadelineProto/Db/DbArray.php index 801dc1285..4e9be71cd 100644 --- a/src/danog/MadelineProto/Db/DbArray.php +++ b/src/danog/MadelineProto/Db/DbArray.php @@ -48,7 +48,7 @@ interface DbArray extends DbType, \ArrayAccess, \Countable * * @psalm-param T $value * - * @return void + * @return Promise */ public function offsetSet($index, $value); /** diff --git a/src/danog/MadelineProto/Db/DriverArray.php b/src/danog/MadelineProto/Db/DriverArray.php index 30d56ea0e..bc5b9aa7c 100644 --- a/src/danog/MadelineProto/Db/DriverArray.php +++ b/src/danog/MadelineProto/Db/DriverArray.php @@ -84,12 +84,8 @@ abstract class DriverArray implements DbArray */ public static function getInstance(string $table, $previous, $settings): Promise { - if ($previous && \get_class($previous) === static::class && $previous->getTable() === $table) { - $instance = &$previous; - } else { - $instance = new static(); - $instance->setTable($table); - } + $instance = new static(); + $instance->setTable($table); /** @psalm-suppress UndefinedPropertyAssignment */ $instance->dbSettings = $settings; @@ -101,7 +97,7 @@ abstract class DriverArray implements DbArray yield from $instance->initConnection($settings); yield from $instance->prepareTable(); - if ($instance !== $previous) { + if (static::getClassName($previous) !== static::getClassName($instance)) { if ($previous instanceof DriverArray) { yield from $previous->initStartup(); } @@ -125,7 +121,7 @@ abstract class DriverArray implements DbArray */ protected static function renameTmpTable(self $new, $old): \Generator { - if ($old && \str_replace('NullCache\\', '', \get_class($old)) === \str_replace('NullCache\\', '', \get_class($new)) && $old->getTable()) { + if ($old instanceof SqlArray && $old->getTable()) { if ( $old->getTable() !== $new->getTable() && \mb_strpos($new->getTable(), 'tmp') !== 0 @@ -146,10 +142,7 @@ abstract class DriverArray implements DbArray */ protected static function migrateDataToDb(self $new, $old): \Generator { - if (!empty($old) && !$old instanceof static) { - if (\str_replace('NullCache\\', '', \get_class($old)) === \str_replace('NullCache\\', '', \get_class($new))) { - return; - } + if (!empty($old) && static::getClassName($old) !== static::getClassName($new)) { Logger::log('Converting '.\get_class($old).' to '.\get_class($new), Logger::ERROR); if (!$old instanceof DbArray) { @@ -217,4 +210,15 @@ abstract class DriverArray implements DbArray { throw new \RuntimeException('Native isset not support promises. Use isset method'); } + + protected static function getClassName($instance): ?string + { + if ($instance === null) { + return null; + } elseif (is_array($instance)) { + return 'Array'; + } else { + return \str_replace('NullCache\\', '', \get_class($instance)); + } + } }