py.lib
2022-02-23
Parent:26a5160d6b6b
py.lib/db/migrator.py
. Рефакторинг бессмысленный и беспощадный
1.1 --- a/db/migrator.py Sat Nov 27 12:29:59 2021 +0300 1.2 +++ b/db/migrator.py Wed Feb 23 19:27:33 2022 +0300 1.3 @@ -9,7 +9,8 @@ 1.4 from os import listdir 1.5 1.6 1.7 -class MigrateError(Exception): pass 1.8 +class MigrateError(Exception): 1.9 + pass 1.10 1.11 1.12 class MigrateManager(object): 1.13 @@ -21,11 +22,11 @@ 1.14 1.15 self.schema = p_join(migrate_env, 'schema.sql') 1.16 if not exists(self.schema): 1.17 - raise MigrateError('Schema file not found: %s' % self.schema) 1.18 + raise MigrateError(f'Schema file not found: {self.schema}') 1.19 1.20 self.patch_dir = p_join(migrate_env, 'patch') 1.21 if not isdir(self.patch_dir): 1.22 - raise MigrateError('Patch dir not found or not directory: %s' % self.patch_dir) 1.23 + raise MigrateError(f'Patch dir not found or not directory: {self.patch_dir}') 1.24 1.25 def get_patch_files(self, ver: int): 1.26 res = {} 1.27 @@ -39,17 +40,13 @@ 1.28 _ver = int(_f[0]) 1.29 1.30 except (TypeError, ValueError) as e: 1.31 - raise MigrateError('Error on parse version "%(ver)s" of file "%(f)s": %(e)s' % { 1.32 - 'ver': _f[0], 1.33 - 'f': f, 1.34 - 'e': e, 1.35 - }) 1.36 + raise MigrateError(f'Error on parse version "{_f[0]}" of file "{f}": {e}') 1.37 1.38 except IndexError: 1.39 - raise MigrateError('Error on get version from filename: %s' % f) 1.40 + raise MigrateError(f'Error on get version from filename: {f}') 1.41 1.42 if _ver in res: 1.43 - raise MigrateError('Version duplicates on parse file: %s' % f) 1.44 + raise MigrateError(f'Version duplicates on parse file: {f}') 1.45 1.46 res[_ver] = p_join(self.patch_dir, f) 1.47 1.48 @@ -83,7 +80,7 @@ 1.49 1.50 def check(self, db): 1.51 cursor = db.cursor() 1.52 - cursor.execute("SELECT version FROM %s" % self.control_table) 1.53 + cursor.execute(f"SELECT version FROM {self.control_table}") 1.54 q = cursor.fetchone() 1.55 del cursor 1.56 1.57 @@ -100,12 +97,12 @@ 1.58 cursor.execute(cmd) 1.59 db.commit() 1.60 1.61 - cursor.execute("DELETE FROM %s" % self.control_table) 1.62 + cursor.execute(f"DELETE FROM {self.control_table}") 1.63 1.64 - cursor.execute(""" 1.65 - INSERT INTO %s (version) 1.66 - VALUES (%s) 1.67 - """ % (self.control_table, new_ver)) 1.68 + cursor.execute(f""" 1.69 + INSERT INTO {self.control_table} (version) 1.70 + VALUES ({new_ver}) 1.71 + """) 1.72 db.commit() 1.73 1.74 @staticmethod