sqlalchemy-bot / test_alembic_1

0 stars 0 forks source link

get_current_heads from python #432

Closed sqlalchemy-bot closed 7 years ago

sqlalchemy-bot commented 7 years ago

Migrated issue, originally created by MultiSosnooley (pohmelie)

I'm trying to migrate with alembic from my python program at startup. Documentation said, that I need no config for MigrationContext, but it looks like alembic don't know where is my "heads" if there is no config with versions.

$ alembic current
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
e45972999969
$ alembic heads
3f7889941dbe (head)

I am not at head, and alembic know it.

# sqlalchemy code

from alembic import config, command, migration

with engine.begin() as conn:
    context = migration.MigrationContext.configure(conn)
    current_revision = context.get_current_revision()
    heads = context.get_current_heads()
    print(current_revision, heads)
e45972999969 ('e45972999969',)

How to fix this? What is the order of creation proper environment for alembic from my custom python program?

sqlalchemy-bot commented 7 years ago

Michael Bayer (zzzeek) wrote:

http://alembic.zzzcomputing.com/en/latest/api/runtime.html#alembic.runtime.migration.MigrationContext.get_current_heads

Return a tuple of the current ‘head versions’ that are represented in the target database.

For a migration stream without branches, this will be a single value, synonymous with that of MigrationContext.get_current_revision().

the correct object is ScriptDirectory, however looks like the docs didn't build correctly. I'm working on fixing that.

When I get those docs back up, this is the code to get the heads in your files:

#!python

from alembic.script import ScriptDirectory
from alembic.config import Config
config = Config()
config.set_main_option("script_location", "myapp:migrations")
script = ScriptDirectory.from_config(config)

head_revision = script.get_current_head()

further question on the mailing list at https://groups.google.com/forum/#!forum/sqlalchemy-alembic, thanks!

sqlalchemy-bot commented 7 years ago

Michael Bayer (zzzeek) wrote:

http://alembic.zzzcomputing.com/en/latest/api/script.html

sqlalchemy-bot commented 7 years ago

MultiSosnooley (pohmelie) wrote:

Thanks, work as expected!