python - Getting Alembic Database Version Programmatically -
python - Getting Alembic Database Version Programmatically -
i'm trying figure out how version of database using alembic. i've gotten database set utilize alembic , performed both upgrade , downgrade on it. want version own python script.
i attempted create function doing this
def get_current_database_version(): path = os.path.join(os.path.dirname(__file__), os.path.pardir) alembic_cfg = config(os.path.join(path, 'alembic.ini')) current_rev = command.current(alembic_cfg, head_only=true) homecoming current_rev this function returned nosectionerror: no section: 'formatters'
i went alembic.ini file check had formatters area. alembic.ini file:
# generic, single database configuration. [alembic] # path migration scripts script_location = alembic pyramid_config_file = ../../development.ini # template used generate migration files # file_template = %%(rev)s_%%(slug)s # max length of characters apply # "slug" field #truncate_slug_length = 40 # set 'true' run environment during # 'revision' command, regardless of autogenerate # revision_environment = false # set 'true' allow .pyc , .pyo files without # source .py file detected revisions in # versions/ directory # sourceless = false sqlalchemy.url = sqlite:///%(here)s/mgo.sqlite # logging configuration [loggers] keys = root,sqlalchemy,alembic [handlers] keys = console [formatters] keys = generic [logger_root] level = warn handlers = console qualname = [logger_sqlalchemy] level = warn handlers = qualname = sqlalchemy.engine [logger_alembic] level = info handlers = qualname = alembic [handler_console] class = streamhandler args = (sys.stderr,) level = notset formatter = generic [formatter_generic] format = %(levelname)-5.5s [%(name)s] %(message)s datefmt = %h:%m:%s anyone know i'm doing wrong? thanks
edit:
here effort utilize migrationcontext solve problem:
def get_database_revision(): engine = create_engine("sqlite:///../mgo.db") conn = engine.connect() context = migrationcontext.configure(conn) current_rev = context.get_current_revision() homecoming current_rev it connects returns none. using sqlite browser can see version in database not set none.
you can utilize migrationcontext get current version:
from alembic.migration import migrationcontext sqlalchemy import create_engine engine = create_engine("postgresql://mydatabase") conn = engine.connect() context = migrationcontext.configure(conn) current_rev = context.get_current_revision() inside env.py can use:
from alembic import context migration_context = context.get_context() current_rev = context.get_current_revision() lastly, comes downwards connecting database , looking @ alembic_version table. contains migration version value , that's database (according alembic). can write code way want long that's you're doing.
python sqlalchemy alembic
Comments
Post a Comment