PillarTool/pillar_tool/db/__init__.py

27 lines
774 B
Python

from os import lockf, F_LOCK
from os.path import dirname, realpath
from alembic.config import Config
from alembic.command import upgrade, check, util
from pillar_tool.util import config
from .models import *
cfg = config()
user = cfg.db.user
password = cfg.db.password
host = cfg.db.host
port = cfg.db.port
database = cfg.db.database
alembic_cfg = Config()
alembic_cfg.set_main_option('script_location', f'{dirname(realpath(__file__))}/migrations')
alembic_cfg.set_main_option('sqlalchemy.url', f'postgresql://{user}:{password}@{host}:{port}/{database}')
alembic_cfg.set_main_option('prepend_sys_path', '.')
def run_db_migrations():
with open(realpath(__file__), 'a') as f:
lockf(f.fileno(), F_LOCK, 0)
upgrade(config=alembic_cfg, revision='head')