automatically apply new database migrations on startup, and handle the race condition of multiple workers starting up simultaneously
This commit is contained in:
parent
d7039884d2
commit
39ba464237
@ -1,9 +1,8 @@
|
|||||||
import os
|
from os import lockf, F_LOCK
|
||||||
|
from os.path import dirname, realpath
|
||||||
import alembic
|
|
||||||
|
|
||||||
from alembic.config import Config
|
from alembic.config import Config
|
||||||
from alembic.command import upgrade
|
from alembic.command import upgrade, check, util
|
||||||
from pillar_tool.util import config
|
from pillar_tool.util import config
|
||||||
|
|
||||||
|
|
||||||
@ -16,10 +15,12 @@ host = cfg.db.host
|
|||||||
port = cfg.db.port
|
port = cfg.db.port
|
||||||
database = cfg.db.database
|
database = cfg.db.database
|
||||||
alembic_cfg = Config()
|
alembic_cfg = Config()
|
||||||
alembic_cfg.set_main_option('script_location', f'{os.path.dirname(os.path.realpath(__file__))}/migrations')
|
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('sqlalchemy.url', f'postgresql://{user}:{password}@{host}:{port}/{database}')
|
||||||
alembic_cfg.set_main_option('prepend_sys_path', '.')
|
alembic_cfg.set_main_option('prepend_sys_path', '.')
|
||||||
|
|
||||||
|
|
||||||
def run_db_migrations():
|
def run_db_migrations():
|
||||||
|
with open(realpath(__file__), 'a') as f:
|
||||||
|
lockf(f.fileno(), F_LOCK, 0)
|
||||||
upgrade(config=alembic_cfg, revision='head')
|
upgrade(config=alembic_cfg, revision='head')
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user