5252
5353from datetime import datetime
5454from multiprocessing import active_children
55- from os import sep as directory_separator
5655
57- import pymysql
58- import pymysql .cursors
5956from colorama import Fore , Style
6057from sqlalchemy .exc import IntegrityError
6158from sqlalchemy .orm .exc import MultipleResultsFound , NoResultFound
@@ -98,58 +95,25 @@ def authorized(self):
9895 [self .does_table_exists (x ) for x in self .old_tables ]
9996 )
10097
101- def __get_rows (self , statement , limit = 20 ):
98+ @classmethod
99+ def __get_rows (cls , statement , limit = 20 ):
102100 """
103101 Get the row of the database.
104102 """
105103
106104 statement += f" LIMIT { limit } "
107105
108106 while True :
109- fetcher_connection = self .get_old_connection ()
110- with fetcher_connection .cursor () as cursor :
111- cursor .execute (statement )
112-
113- db_result = cursor .fetchall ()
114-
115- fetcher_connection .close ()
107+ with PyFunceble .engine .database .loader .session .Session () as db_session :
108+ db_result = db_session .execute (statement )
109+ db_result = [dict (x ) for x in db_result .fetchall ()]
116110
117111 if not db_result :
118112 break
119113
120114 for result in db_result :
121115 yield result
122116
123- def get_old_connection (self ):
124- """
125- Provides a connection, the old way.
126- """
127-
128- if (
129- directory_separator not in self .credentials ["host" ]
130- or "/" not in self .credentials ["host" ]
131- ):
132- return pymysql .connect (
133- host = self .credentials ["host" ],
134- port = self .credentials ["port" ],
135- user = self .credentials ["username" ],
136- password = self .credentials ["password" ],
137- db = self .credentials ["name" ],
138- charset = self .credentials ["charset" ],
139- cursorclass = pymysql .cursors .DictCursor ,
140- autocommit = True ,
141- )
142-
143- return pymysql .connect (
144- unix_socket = self .credentials ["host" ],
145- user = self .credentials ["username" ],
146- password = self .credentials ["password" ],
147- db = self .credentials ["name" ],
148- charset = self .credentials ["charset" ],
149- cursorclass = pymysql .cursors .DictCursor ,
150- autocommit = True ,
151- )
152-
153117 def does_table_exists (self , table_name ):
154118 """
155119 Checks if the table exists.
@@ -158,25 +122,23 @@ def does_table_exists(self, table_name):
158122 The name of the table to check.
159123 """
160124
161- old_connection = self .get_old_connection ()
162- with old_connection .cursor () as cursor :
125+ with PyFunceble .engine .database .loader .session .Session () as db_session :
163126 statement = (
164127 "SELECT COUNT(*) "
165128 "FROM information_schema.tables "
166- "WHERE table_schema = %( database_name)s "
167- "AND table_name = %( table_name)s "
129+ "WHERE table_schema = : database_name "
130+ "AND table_name = : table_name "
168131 )
169132
170- cursor .execute (
133+ result = db_session .execute (
171134 statement ,
172135 {
173136 "database_name" : self .credentials ["name" ],
174137 "table_name" : table_name ,
175138 },
176139 )
177140
178- result = cursor .fetchone ()
179- old_connection .close ()
141+ result = dict (result .fetchone ())
180142
181143 if result ["COUNT(*)" ] != 1 :
182144 return False
@@ -287,12 +249,11 @@ def __tested_migration(self, data):
287249 except IntegrityError :
288250 pass
289251
290- old_connection = self . get_old_connection ()
291- with old_connection . cursor () as cursor :
292- statement = "DELETE FROM pyfunceble_tested WHERE id = %(status_id)s"
252+ with PyFunceble . engine . database . loader . session . Session () as db_session :
253+ statement = "DELETE FROM pyfunceble_tested WHERE id = :status_id"
254+
293255 # pylint: disable=no-member
294- cursor .execute (statement , {"status_id" : status .id })
295- old_connection .close ()
256+ db_session .execute (statement , {"status_id" : status .id })
296257
297258 if self .autosave .authorized or PyFunceble .CONFIGURATION .print_dots :
298259 PyFunceble .LOGGER .info (f'Switched { data ["tested" ]} to SQLAlchemy.' )
@@ -364,11 +325,10 @@ def __autocontinue_migration(self, data):
364325 except IntegrityError :
365326 pass
366327
367- old_connection = self .get_old_connection ()
368- with old_connection .cursor () as cursor :
369- statement = "DELETE FROM pyfunceble_auto_continue WHERE id = %(id)s"
370- cursor .execute (statement , {"id" : data ["id" ]})
371- old_connection .close ()
328+ with PyFunceble .engine .database .loader .session .Session () as db_session :
329+ statement = "DELETE FROM pyfunceble_auto_continue WHERE id = :id"
330+
331+ db_session .execute (statement , {"id" : data ["id" ]})
372332
373333 if self .autosave .authorized or PyFunceble .CONFIGURATION .print_dots :
374334 PyFunceble .LOGGER .info (
@@ -440,11 +400,10 @@ def __whois_migration(self, data):
440400 except IntegrityError :
441401 pass
442402
443- old_connection = self .get_old_connection ()
444- with old_connection .cursor () as cursor :
445- statement = "DELETE FROM pyfunceble_whois WHERE id = %(id)s"
446- cursor .execute (statement , {"id" : data ["id" ]})
447- old_connection .close ()
403+ with PyFunceble .engine .database .loader .session .Session () as db_session :
404+ statement = "DELETE FROM pyfunceble_whois WHERE id = :id"
405+
406+ db_session .execute (statement , {"id" : data ["id" ]})
448407
449408 if self .autosave .authorized or PyFunceble .CONFIGURATION .print_dots :
450409 PyFunceble .LOGGER .info (f'Switched { data ["subject" ]} (WHOIS) to SQLAlchemy.' )
@@ -482,12 +441,12 @@ def __delete_old_tables(self):
482441 for table in self .old_tables :
483442 if self .does_table_exists (table ):
484443 PyFunceble .LOGGER .info (f"Starting deletion of { table } ." )
485- old_connection = self . get_old_connection ()
486- with old_connection . cursor () as cursor :
444+
445+ with PyFunceble . engine . database . loader . session . Session () as db_session :
487446 statement = f"DROP TABLE { table } "
488- cursor . execute ( statement )
489- old_connection . close ( )
490- PyFunceble .LOGGER .info (f"Finished deletion of { table } ." )
447+
448+ db_session . execute ( statement )
449+ PyFunceble .LOGGER .info (f"Finished deletion of { table } ." )
491450
492451 if self .autosave .authorized or PyFunceble .CONFIGURATION .print_dots :
493452 print ("." , end = "" )
0 commit comments