Skip to content

Commit

Permalink
Set alembic script_location config at runtime (#84)
Browse files Browse the repository at this point in the history
* fix: set alembic `script_location` config at runtime

* test: alembic `script_location` in unit tests
  • Loading branch information
stefanorosanelli authored Dec 19, 2024
1 parent 84ef00e commit 170cee3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
4 changes: 2 additions & 2 deletions alembic.ini
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# A generic, single database configuration.

[alembic]
# path to migration scripts
script_location = brevia/alembic
# path to migration scripts, will be set by brevia at runtime
#script_location = brevia/alembic

# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
# Uncomment the line below if you want the files to be prepended with date and time
Expand Down
12 changes: 6 additions & 6 deletions brevia/alembic/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
"""DB migrations commands """
from os import getcwd

from os.path import dirname
from alembic.config import Config
from alembic import command


ALEMBIC_CFG = Config(f'{getcwd()}/alembic.ini')
alembic_cfg = Config(f'{getcwd()}/alembic.ini')
alembic_cfg.set_main_option('script_location', dirname(__file__))


def current(verbose=False):
command.current(ALEMBIC_CFG, verbose=verbose)
command.current(alembic_cfg, verbose=verbose)


def upgrade(revision="head"):
command.upgrade(ALEMBIC_CFG, revision)
command.upgrade(alembic_cfg, revision)


def downgrade(revision):
command.downgrade(ALEMBIC_CFG, revision)
command.downgrade(alembic_cfg, revision)
3 changes: 3 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from dotenv import dotenv_values
from brevia.settings import Settings, get_settings
from brevia.index import init_splitting_data
import brevia.alembic


def pytest_sessionstart(session):
Expand Down Expand Up @@ -45,6 +46,8 @@ def env_vars_db():
update_settings()
root_path = Path(__file__).parent.parent
alembic_conf = Config(f'{root_path}/alembic.ini')
script_location = os.path.dirname(brevia.alembic.__file__)
alembic_conf.set_main_option('script_location', script_location)
command.upgrade(alembic_conf, 'head')

yield
Expand Down

0 comments on commit 170cee3

Please sign in to comment.