py.lib

Yohn Y. 2020-12-25 Parent:f3e62028e5d3 Child:10227cc154fa

16:6c38121a0c86 Browse Files

+ Добавления инструмента миграций

db/migrator.py

     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/db/migrator.py	Fri Dec 25 10:46:37 2020 +0300
     1.3 @@ -0,0 +1,108 @@
     1.4 +# coding: utf-8
     1.5 +"""\
     1.6 +Инструмент миграции схемы БД.
     1.7 +
     1.8 +DOC: https://ws.a0fs.net/w/ncc.ws/devel/python/modules/db_migrator/
     1.9 +"""
    1.10 +
    1.11 +from os.path import exists, join as p_join, isdir
    1.12 +from os import listdir
    1.13 +
    1.14 +
    1.15 +class MigrateError(Exception): pass
    1.16 +
    1.17 +
    1.18 +class MigrateManager(object):
    1.19 +    def __init__(self, control_table: str, migrate_env: str):
    1.20 +        self.control_table = control_table
    1.21 +
    1.22 +        if not exists(migrate_env):
    1.23 +            raise MigrateError('Migrate enviroment not found')
    1.24 +
    1.25 +        self.schema = p_join(migrate_env, 'schema.sql')
    1.26 +        if not exists(self.schema):
    1.27 +            raise MigrateError('Schema file not found: %s' % self.schema)
    1.28 +
    1.29 +        self.patch_dir = p_join(migrate_env, 'patch')
    1.30 +        if not isdir(self.patch_dir):
    1.31 +            raise MigrateError('Patch dir not found or not directory: %s' % self.patch_dir)
    1.32 +
    1.33 +    def get_patch_files(self, ver: int):
    1.34 +        res = {}
    1.35 +        for f in listdir(self.patch_dir):
    1.36 +            if not f.lower().endswith('.sql'):
    1.37 +                continue
    1.38 +
    1.39 +            _f = f.strip().split('.')
    1.40 +
    1.41 +            try:
    1.42 +                _ver = int(_f[0])
    1.43 +
    1.44 +            except (TypeError, ValueError) as e:
    1.45 +                raise MigrateError('Error on parse version "%(ver)s" of file "%(f)s": %(e)s' % {
    1.46 +                    'ver': _f[0],
    1.47 +                    'f': f,
    1.48 +                    'e': e,
    1.49 +                })
    1.50 +
    1.51 +            except IndexError:
    1.52 +                raise MigrateError('Error on get version from filename: %s' % f)
    1.53 +
    1.54 +            if _ver in res:
    1.55 +                raise MigrateError('Version duplicates on parse file: %s' % f)
    1.56 +
    1.57 +            res[_ver] = p_join(self.patch_dir, f)
    1.58 +
    1.59 +        for i in sorted(res.keys()):
    1.60 +            if i > ver:
    1.61 +                yield i, res[i]
    1.62 +
    1.63 +    @staticmethod
    1.64 +    def get_commands(file):
    1.65 +        buf = []
    1.66 +        with open(file) as IN:
    1.67 +            for l in IN:
    1.68 +                if l.lstrip().startswith('--'):
    1.69 +                    if buf:
    1.70 +                        yield '\n'.join(buf)
    1.71 +                        buf[:] = []
    1.72 +
    1.73 +                else:
    1.74 +                    buf.append(l)
    1.75 +
    1.76 +        if buf:
    1.77 +            yield '\n'.join(buf)
    1.78 +
    1.79 +    def init_db(self, db):
    1.80 +        cursor = db.cursor()
    1.81 +        for c in self.get_commands(self.schema):
    1.82 +           cursor.execute(c)
    1.83 +           db.commit()
    1.84 +
    1.85 +        db.commit()
    1.86 +
    1.87 +    def check(self, db):
    1.88 +        cursor = db.cursor()
    1.89 +        cursor.execute("SELECT version FROM %s" % self.control_table)
    1.90 +        q = cursor.fetchone()
    1.91 +        del cursor
    1.92 +
    1.93 +        if q is None:
    1.94 +            ver = -1
    1.95 +        else:
    1.96 +            ver = int(q[0])
    1.97 +
    1.98 +        new_ver = ver
    1.99 +        cursor = db.cursor()
   1.100 +        for up_ver, patch_file in self.get_patch_files(ver):
   1.101 +            new_ver = up_ver
   1.102 +            for cmd in self.get_commands(patch_file):
   1.103 +                cursor.execute(cmd)
   1.104 +                db.commit()
   1.105 +
   1.106 +        cursor.execute("""
   1.107 +            UPDATE %s
   1.108 +            SET version = %s
   1.109 +        """ % (self.control_table, new_ver))
   1.110 +
   1.111 +