@@ -215,7 +215,7 @@ def _get_sqlite_version(self) -> str:
215
215
216
216
def _sqlite_table_has_rowid (self , table : str ) -> bool :
217
217
try :
218
- self ._sqlite_cur .execute (""" SELECT rowid FROM "{}" LIMIT 1""" . format ( table ) )
218
+ self ._sqlite_cur .execute (f' SELECT rowid FROM "{ table } " LIMIT 1' )
219
219
self ._sqlite_cur .fetchall ()
220
220
return True
221
221
except sqlite3 .OperationalError :
@@ -224,15 +224,11 @@ def _sqlite_table_has_rowid(self, table: str) -> bool:
224
224
def _create_database (self ) -> None :
225
225
try :
226
226
self ._mysql_cur .execute (
227
- """
228
- CREATE DATABASE IF NOT EXISTS `{database}`
229
- DEFAULT CHARACTER SET {charset}
230
- DEFAULT COLLATE {collation}
231
- """ .format (
232
- database = self ._mysql_database ,
233
- charset = self ._mysql_charset ,
234
- collation = self ._mysql_collation ,
235
- )
227
+ f"""
228
+ CREATE DATABASE IF NOT EXISTS `{ self ._mysql_database } `
229
+ DEFAULT CHARACTER SET { self ._mysql_charset }
230
+ DEFAULT COLLATE { self ._mysql_collation }
231
+ """
236
232
)
237
233
self ._mysql_cur .close ()
238
234
self ._mysql .commit ()
@@ -300,23 +296,21 @@ def _column_type_length(cls, column_type: str, default: t.Optional[t.Union[str,
300
296
if suffix :
301
297
return suffix .group (0 )
302
298
if default :
303
- return "({})" . format ( default )
299
+ return f "({ default } )"
304
300
return ""
305
301
306
302
def _create_table (self , table_name : str , transfer_rowid : bool = False ) -> None :
307
303
primary_keys : t .List [t .Dict [str , str ]] = []
308
304
309
- sql : str = "CREATE TABLE IF NOT EXISTS `{}` ( " . format ( safe_identifier_length ( table_name ))
305
+ sql : str = f "CREATE TABLE IF NOT EXISTS `{ safe_identifier_length ( table_name ) } ` ( "
310
306
311
307
if transfer_rowid :
312
308
sql += " `rowid` BIGINT NOT NULL, "
313
309
314
- self ._sqlite_cur .execute (
315
- 'PRAGMA {command}("{table_name}")' .format (
316
- command = "table_xinfo" if self ._sqlite_table_xinfo_support else "table_info" ,
317
- table_name = table_name ,
318
- )
319
- )
310
+ if self ._sqlite_table_xinfo_support :
311
+ self ._sqlite_cur .execute (f'PRAGMA table_xinfo("{ table_name } ")' )
312
+ else :
313
+ self ._sqlite_cur .execute (f'PRAGMA table_info("{ table_name } ")' )
320
314
321
315
rows : t .List [t .Any ] = self ._sqlite_cur .fetchall ()
322
316
compound_primary_key : bool = len (tuple (True for row in rows if dict (row )["pk" ] > 0 )) > 1
@@ -364,12 +358,9 @@ def _create_table(self, table_name: str, transfer_rowid: bool = False) -> None:
364
358
)
365
359
366
360
if transfer_rowid :
367
- sql += ", CONSTRAINT `{}_rowid` UNIQUE (`rowid`)" . format ( safe_identifier_length ( table_name ))
361
+ sql += f ", CONSTRAINT `{ safe_identifier_length ( table_name ) } _rowid` UNIQUE (`rowid`)"
368
362
369
- sql += " ) ENGINE=InnoDB DEFAULT CHARSET={charset} COLLATE={collation}" .format (
370
- charset = self ._mysql_charset ,
371
- collation = self ._mysql_collation ,
372
- )
363
+ sql += f" ) ENGINE=InnoDB DEFAULT CHARSET={ self ._mysql_charset } COLLATE={ self ._mysql_collation } "
373
364
374
365
try :
375
366
self ._mysql_cur .execute (sql )
@@ -395,27 +386,23 @@ def _truncate_table(self, table_name: str) -> None:
395
386
)
396
387
if len (self ._mysql_cur .fetchall ()) > 0 :
397
388
self ._logger .info ("Truncating table %s" , safe_identifier_length (table_name ))
398
- self ._mysql_cur .execute (
399
- "TRUNCATE TABLE `{}`" .format (
400
- safe_identifier_length (table_name ),
401
- ),
402
- )
389
+ self ._mysql_cur .execute (f"TRUNCATE TABLE `{ safe_identifier_length (table_name )} `" )
403
390
404
391
def _add_indices (self , table_name : str ) -> None :
405
- self ._sqlite_cur .execute ('PRAGMA table_info("{}")' . format ( table_name ) )
392
+ self ._sqlite_cur .execute (f 'PRAGMA table_info("{ table_name } ")' )
406
393
table_columns : t .Dict [str , str ] = {}
407
394
for row in self ._sqlite_cur .fetchall ():
408
395
column : t .Dict [str , t .Any ] = dict (row )
409
396
table_columns [column ["name" ]] = column ["type" ]
410
397
411
- self ._sqlite_cur .execute ('PRAGMA index_list("{}")' . format ( table_name ) )
398
+ self ._sqlite_cur .execute (f 'PRAGMA index_list("{ table_name } ")' )
412
399
indices : t .Tuple [t .Dict [str , t .Any ], ...] = tuple (dict (row ) for row in self ._sqlite_cur .fetchall ())
413
400
414
401
for index in indices :
415
402
if index ["origin" ] == "pk" :
416
403
continue
417
404
418
- self ._sqlite_cur .execute ('PRAGMA index_info("{}")' . format ( index ["name" ]) )
405
+ self ._sqlite_cur .execute (f 'PRAGMA index_info("{ index ["name" ]} ")' )
419
406
index_infos : t .Tuple [t .Dict [str , t .Any ], ...] = tuple (dict (row ) for row in self ._sqlite_cur .fetchall ())
420
407
421
408
index_type : str = "UNIQUE" if int (index ["unique" ]) == 1 else "INDEX"
@@ -428,14 +415,14 @@ def _add_indices(self, table_name: str) -> None:
428
415
# Use fulltext if requested and available
429
416
index_type = "FULLTEXT"
430
417
index_columns : str = "," .join (
431
- "`{}`" . format ( safe_identifier_length (index_info ["name" ])) for index_info in index_infos
418
+ f'` { safe_identifier_length (index_info ["name" ])} `' for index_info in index_infos
432
419
)
433
420
else :
434
421
# Limit the max TEXT field index length to 255
435
422
index_columns = ", " .join (
436
423
"`{column}`{length}" .format (
437
424
column = safe_identifier_length (index_info ["name" ]),
438
- length = "({})" . format ( 255 )
425
+ length = "(255)"
439
426
if table_columns [index_info ["name" ]].upper () in MYSQL_TEXT_COLUMN_TYPES_WITH_JSON
440
427
else "" ,
441
428
)
@@ -447,19 +434,14 @@ def _add_indices(self, table_name: str) -> None:
447
434
index_length : str = ""
448
435
# Limit the max BLOB field index length to 255
449
436
if table_columns [index_info ["name" ]].upper () in MYSQL_BLOB_COLUMN_TYPES :
450
- index_length = "({})" . format ( 255 )
437
+ index_length = "(255)"
451
438
else :
452
439
suffix : t .Optional [t .Match [str ]] = self .COLUMN_LENGTH_PATTERN .search (
453
440
table_columns [index_info ["name" ]]
454
441
)
455
442
if suffix :
456
443
index_length = suffix .group (0 )
457
- column_list .append (
458
- "`{column}`{length}" .format (
459
- column = safe_identifier_length (index_info ["name" ]),
460
- length = index_length ,
461
- )
462
- )
444
+ column_list .append (f'`{ safe_identifier_length (index_info ["name" ])} `{ index_length } ' )
463
445
index_columns = ", " .join (column_list )
464
446
465
447
try :
@@ -480,7 +462,7 @@ def _add_indices(self, table_name: str) -> None:
480
462
index_columns = ", " .join (
481
463
"`{column}`{length}" .format (
482
464
column = safe_identifier_length (index_info ["name" ]),
483
- length = "({})" . format ( 255 )
465
+ length = "(255)"
484
466
if table_columns [index_info ["name" ]].upper () in MYSQL_TEXT_COLUMN_TYPES_WITH_JSON
485
467
else "" ,
486
468
)
@@ -508,10 +490,7 @@ def _add_index(
508
490
index_type = index_type ,
509
491
name = safe_identifier_length (index ["name" ])
510
492
if index_iteration == 0
511
- else "{name}_{index_iteration}" .format (
512
- name = safe_identifier_length (index ["name" ], max_length = 60 ),
513
- index_iteration = index_iteration ,
514
- ),
493
+ else f'{ safe_identifier_length (index ["name" ], max_length = 60 )} _{ index_iteration } ' ,
515
494
columns = index_columns ,
516
495
)
517
496
@@ -567,7 +546,7 @@ def _add_index(
567
546
raise
568
547
569
548
def _add_foreign_keys (self , table_name : str ) -> None :
570
- self ._sqlite_cur .execute ('PRAGMA foreign_key_list("{}")' . format ( table_name ) )
549
+ self ._sqlite_cur .execute (f 'PRAGMA foreign_key_list("{ table_name } ")' )
571
550
572
551
for row in self ._sqlite_cur .fetchall ():
573
552
foreign_key : t .Dict [str , t .Any ] = dict (row )
@@ -640,14 +619,12 @@ def transfer(self) -> None:
640
619
if len (self ._sqlite_tables ) > 0 :
641
620
# transfer only specific tables
642
621
self ._sqlite_cur .execute (
643
- """
622
+ f """
644
623
SELECT name FROM sqlite_master
645
624
WHERE type='table'
646
625
AND name NOT LIKE 'sqlite_%'
647
- AND name IN({placeholders})
648
- """ .format (
649
- placeholders = ("?, " * len (self ._sqlite_tables )).rstrip (" ," )
650
- ),
626
+ AND name IN({ ("?, " * len (self ._sqlite_tables )).rstrip (" ," )} )
627
+ """ ,
651
628
self ._sqlite_tables ,
652
629
)
653
630
else :
@@ -676,17 +653,15 @@ def transfer(self) -> None:
676
653
self ._truncate_table (table ["name" ])
677
654
678
655
# get the size of the data
679
- self ._sqlite_cur .execute ('SELECT COUNT(*) AS total_records FROM "{}"' . format ( table ["name" ]) )
656
+ self ._sqlite_cur .execute (f 'SELECT COUNT(*) AS total_records FROM "{ table ["name" ]} "' )
680
657
total_records = int (dict (self ._sqlite_cur .fetchone ())["total_records" ])
681
658
682
659
# only continue if there is anything to transfer
683
660
if total_records > 0 :
684
661
# populate it
685
662
self ._logger .info ("Transferring table %s" , table ["name" ])
686
663
self ._sqlite_cur .execute (
687
- """
688
- SELECT {rowid} * FROM "{table_name}"
689
- """ .format (
664
+ '''SELECT {rowid} * FROM "{table_name}"''' .format (
690
665
rowid = 'rowid as "rowid",' if transfer_rowid else "" ,
691
666
table_name = table ["name" ],
692
667
)
0 commit comments