@@ -56,10 +56,30 @@ def __init__(self, name: str, workdir: Path):
5656
5757 self .database = workdir / (self .name + self .FILE_EXTENSION )
5858
59+ def _vacuum (self ):
60+ with self .conn :
61+ self .conn .execute ("VACUUM" )
62+
5963 def _update_from_one_impl (self ):
6064 with self .conn :
6165 self .conn .execute ("DELETE FROM peers" )
6266
67+ def _update_from_two_impl (self ):
68+ with self .conn :
69+ self .conn .execute ("ALTER TABLE sessions ADD api_id INTEGER" )
70+
71+ def _update_from_three_impl (self ):
72+ with self .conn :
73+ self .conn .executescript (USERNAMES_SCHEMA )
74+
75+ def _update_from_four_impl (self ):
76+ with self .conn :
77+ self .conn .executescript (UPDATE_STATE_SCHEMA )
78+
79+ def _update_from_five_impl (self ):
80+ with self .conn :
81+ self .conn .executescript ("CREATE INDEX idx_usernames_id ON usernames (id);" )
82+
6383 def _connect_impl (self , path ):
6484 self .conn = sqlite3 .connect (str (path ), timeout = 1 , check_same_thread = False )
6585
@@ -72,33 +92,23 @@ async def update(self):
7292 version = await self .version ()
7393
7494 if version == 1 :
75- with self .conn :
76- await self .conn .execute ("DELETE FROM peers" )
77-
95+ await self .loop .run_in_executor (self .executor , self ._update_from_one_impl )
7896 version += 1
7997
8098 if version == 2 :
81- with self .conn :
82- await self .conn .execute ("ALTER TABLE sessions ADD api_id INTEGER" )
83-
99+ await self .loop .run_in_executor (self .executor , self ._update_from_two_impl )
84100 version += 1
85101
86102 if version == 3 :
87- with self .conn :
88- await self .conn .executescript (USERNAMES_SCHEMA )
89-
103+ await self .loop .run_in_executor (self .executor , self ._update_from_three_impl )
90104 version += 1
91105
92106 if version == 4 :
93- with self .conn :
94- await self .conn .executescript (UPDATE_STATE_SCHEMA )
95-
107+ await self .loop .run_in_executor (self .executor , self ._update_from_four_impl )
96108 version += 1
97109
98110 if version == 5 :
99- with self .conn :
100- await self .conn .execute ("CREATE INDEX idx_usernames_id ON usernames (id);" )
101-
111+ await self .loop .run_in_executor (self .executor , self ._update_from_five_impl )
102112 version += 1
103113
104114 await self .version (version )
@@ -114,8 +124,7 @@ async def open(self):
114124 else :
115125 await self .update ()
116126
117- with self .conn :
118- await self .conn .execute ("VACUUM" )
127+ await self .loop .run_in_executor (self .executor , self ._vacuum )
119128
120129 async def delete (self ):
121130 os .remove (self .database )
0 commit comments