2121# You should have received a copy of the GNU Lesser General Public License
2222# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
2323
24+
2425import logging
2526import os
2627import sqlite3
3132log = logging .getLogger (__name__ )
3233
3334USERNAMES_SCHEMA = """
34- CREATE TABLE IF NOT EXISTS usernames
35+ CREATE TABLE usernames
3536(
3637 id INTEGER,
3738 username TEXT,
3839 FOREIGN KEY (id) REFERENCES peers(id)
3940);
4041
41- CREATE INDEX IF NOT EXISTS idx_usernames_username ON usernames (username);
42+ CREATE INDEX idx_usernames_username ON usernames (username);
4243"""
4344
4445UPDATE_STATE_SCHEMA = """
@@ -61,9 +62,33 @@ def __init__(self, name: str, workdir: Path):
6162
6263 self .database = workdir / (self .name + self .FILE_EXTENSION )
6364
65+ def _vacuum (self ):
66+ with self .conn :
67+ self .conn .execute ("VACUUM" )
68+
69+ def _update_from_one_impl (self ):
70+ with self .conn :
71+ self .conn .execute ("DELETE FROM peers" )
72+
73+ def _update_from_two_impl (self ):
74+ with self .conn :
75+ self .conn .execute ("ALTER TABLE sessions ADD api_id INTEGER" )
76+
77+ def _update_from_three_impl (self ):
78+ with self .conn :
79+ self .conn .executescript (USERNAMES_SCHEMA )
80+
81+ def _update_from_four_impl (self ):
82+ with self .conn :
83+ self .conn .executescript (UPDATE_STATE_SCHEMA )
84+
85+ def _update_from_five_impl (self ):
86+ with self .conn :
87+ self .conn .executescript ("CREATE INDEX idx_usernames_id ON usernames (id);" )
88+
6489 def _connect_impl (self , path ):
65- self .conn = sqlite3 .connect (path )
66-
90+ self .conn = sqlite3 .connect (str ( path ), timeout = 1 , check_same_thread = False )
91+
6792 with self .conn :
6893 self .conn .execute ("PRAGMA journal_mode=WAL" ).close ()
6994 self .conn .execute ("PRAGMA synchronous=NORMAL" ).close ()
@@ -73,36 +98,23 @@ async def update(self):
7398 version = await self .version ()
7499
75100 if version == 1 :
76- with self .conn :
77- self .conn .execute ("DELETE FROM peers" )
78-
101+ await self .loop .run_in_executor (self .executor , self ._update_from_one_impl )
79102 version += 1
80103
81104 if version == 2 :
82- with self .conn :
83- try :
84- self .conn .execute ("ALTER TABLE sessions ADD api_id INTEGER" )
85- except Exception as e :
86- log .exception (e )
87-
105+ await self .loop .run_in_executor (self .executor , self ._update_from_two_impl )
88106 version += 1
89107
90108 if version == 3 :
91- with self .conn :
92- self .conn .executescript (USERNAMES_SCHEMA )
93-
109+ await self .loop .run_in_executor (self .executor , self ._update_from_three_impl )
94110 version += 1
95111
96112 if version == 4 :
97- with self .conn :
98- self .conn .executescript (UPDATE_STATE_SCHEMA )
99-
113+ await self .loop .run_in_executor (self .executor , self ._update_from_four_impl )
100114 version += 1
101115
102116 if version == 5 :
103- with self .conn :
104- self .conn .executescript ("CREATE INDEX IF NOT EXISTS idx_usernames_id ON usernames (id);" )
105-
117+ await self .loop .run_in_executor (self .executor , self ._update_from_five_impl )
106118 version += 1
107119
108120 await self .version (version )
@@ -111,15 +123,14 @@ async def open(self):
111123 path = self .database
112124 file_exists = path .is_file ()
113125
114- self .conn = sqlite3 . connect ( str ( path ), timeout = 1 , check_same_thread = False )
126+ self .executor . submit ( self . _connect_impl , path ). result ( )
115127
116128 if not file_exists :
117129 await self .create ()
118130 else :
119131 await self .update ()
120132
121- with self .conn :
122- self .conn .execute ("VACUUM" )
133+ await self .loop .run_in_executor (self .executor , self ._vacuum )
123134
124135 async def delete (self ):
125136 os .remove (self .database )
0 commit comments