migration.py 902 Bytes
Newer Older
1
2
3
import importlib
import os

4
CURRENT_VERSION = 39
5
6


7
def get_m_files():
8
9
10
    mypath = os.path.join(os.path.dirname(__file__), 'migrations')
    file_names = [f for f in os.listdir(mypath) if os.path.isfile(os.path.join(mypath, f))]
    m_files = {int(f[:3]): f[:-3] for f in file_names if not f.startswith('__')}
11
12
13
14
    return m_files


M_FILES = get_m_files()
15

16
17
18
19
20
21

def get_migrations(version_start, version_stop):
    global M_FILES
    if version_start == version_stop:
        return []
    migrations = []
22
    for v in range(version_start + 1, version_stop + 1):
23
        migrations.append(importlib.import_module(f'.{M_FILES[v]}', 'prozorro_sale.procedure.migrations'))
24
25
26
27
28
29
30
31
32
    return migrations


def _migrate(data):
    version = data.get('_version', 0)
    for migration in get_migrations(version, CURRENT_VERSION):
        data = migration.apply(data)
    data['_version'] = CURRENT_VERSION
    return data