From ec5a6fe7a1b9c7a8cde2f51bce887b47511e1381 Mon Sep 17 00:00:00 2001 From: Shrimadhav U K Date: Wed, 27 Nov 2024 13:40:06 +0530 Subject: [PATCH 1/2] cherry picking commit from other fork https://github.com/cavallium/faster-pyrogram/commit/12f9207 Co-authored-by: Andrea Cavalli --- pyrogram/storage/file_storage.py | 61 +++++++++++++++++++------------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/pyrogram/storage/file_storage.py b/pyrogram/storage/file_storage.py index e263b7eb0d..c6721e1775 100644 --- a/pyrogram/storage/file_storage.py +++ b/pyrogram/storage/file_storage.py @@ -21,6 +21,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . + import logging import os import sqlite3 @@ -31,14 +32,14 @@ log = logging.getLogger(__name__) USERNAMES_SCHEMA = """ -CREATE TABLE IF NOT EXISTS usernames +CREATE TABLE usernames ( id INTEGER, username TEXT, FOREIGN KEY (id) REFERENCES peers(id) ); -CREATE INDEX IF NOT EXISTS idx_usernames_username ON usernames (username); +CREATE INDEX idx_usernames_username ON usernames (username); """ UPDATE_STATE_SCHEMA = """ @@ -61,9 +62,33 @@ def __init__(self, name: str, workdir: Path): self.database = workdir / (self.name + self.FILE_EXTENSION) + def _vacuum(self): + with self.conn: + self.conn.execute("VACUUM") + + def _update_from_one_impl(self): + with self.conn: + self.conn.execute("DELETE FROM peers") + + def _update_from_two_impl(self): + with self.conn: + self.conn.execute("ALTER TABLE sessions ADD api_id INTEGER") + + def _update_from_three_impl(self): + with self.conn: + self.conn.executescript(USERNAMES_SCHEMA) + + def _update_from_four_impl(self): + with self.conn: + self.conn.executescript(UPDATE_STATE_SCHEMA) + + def _update_from_five_impl(self): + with self.conn: + self.conn.executescript("CREATE INDEX idx_usernames_id ON usernames (id);") + def _connect_impl(self, path): - self.conn = sqlite3.connect(path) - + self.conn = sqlite3.connect(str(path), timeout=1, check_same_thread=False) + with self.conn: self.conn.execute("PRAGMA journal_mode=WAL").close() self.conn.execute("PRAGMA synchronous=NORMAL").close() @@ -73,36 +98,23 @@ async def update(self): version = await self.version() if version == 1: - with self.conn: - self.conn.execute("DELETE FROM peers") - + await self.loop.run_in_executor(self.executor, self._update_from_one_impl) version += 1 if version == 2: - with self.conn: - try: - self.conn.execute("ALTER TABLE sessions ADD api_id INTEGER") - except Exception as e: - log.exception(e) - + await self.loop.run_in_executor(self.executor, self._update_from_two_impl) version += 1 if version == 3: - with self.conn: - self.conn.executescript(USERNAMES_SCHEMA) - + await self.loop.run_in_executor(self.executor, self._update_from_three_impl) version += 1 if version == 4: - with self.conn: - self.conn.executescript(UPDATE_STATE_SCHEMA) - + await self.loop.run_in_executor(self.executor, self._update_from_four_impl) version += 1 if version == 5: - with self.conn: - self.conn.executescript("CREATE INDEX IF NOT EXISTS idx_usernames_id ON usernames (id);") - + await self.loop.run_in_executor(self.executor, self._update_from_five_impl) version += 1 await self.version(version) @@ -111,15 +123,14 @@ async def open(self): path = self.database file_exists = path.is_file() - self.conn = sqlite3.connect(str(path), timeout=1, check_same_thread=False) + self.executor.submit(self._connect_impl, path).result() if not file_exists: await self.create() else: await self.update() - with self.conn: - self.conn.execute("VACUUM") + await self.loop.run_in_executor(self.executor, self._vacuum) async def delete(self): os.remove(self.database) From 9c749e9a719c31dfc89efe5f1958879e39456434 Mon Sep 17 00:00:00 2001 From: Ryuk <88324835+anonymousx97@users.noreply.github.com> Date: Thu, 28 Nov 2024 15:08:07 +0530 Subject: [PATCH 2/2] Update utils.py: make datetime object UTC timezone aware. datetime.utcnow() is deprecated in py 3.12+ and it is recommended to use datetime.now(UTC) it creates a timezone aware object and pyrogram's previous datetime object was timezone naive which resulted in: TypeError: can't subtract offset-naive and offset-aware datetimes. --- pyrogram/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyrogram/utils.py b/pyrogram/utils.py index e5541916fb..52a62d8aa5 100644 --- a/pyrogram/utils.py +++ b/pyrogram/utils.py @@ -454,7 +454,7 @@ def zero_datetime() -> datetime: def timestamp_to_datetime(ts: Optional[int]) -> Optional[datetime]: - return datetime.fromtimestamp(ts) if ts else None + return datetime.fromtimestamp(ts).replace(tzinfo=timezone.utc) if ts else None def datetime_to_timestamp(dt: Optional[datetime]) -> Optional[int]: