Package: alembic-offline
alembic-offlinealembic-offline is an extension for alemic to enrich offline functionality of the migrationsContentsalembic-offlineUsagePhased migrationsArbitrary script as operationGet migration dataGet migration data in batchCommand line utilitiesContactLicenseChangelog1.2.01.1.01.0.51.0.41.0.31.0.0UsagePhased migrationsalembic-offline introduces a helper which allows to implement phased migrations, e.g. those which steps
are divided into logical phases. For example, you can have steps to be executed before code deploy and
those after.In your alembic config file (main section):phases = before-deploy after-deploy final
default-phase = after-deployIn your version file:fromsqlalchemyimportINTEGER,VARCHAR,NVARCHAR,TIMESTAMP,Column,funcfromalembicimportopfromalembic_offlineimportphased,execute_scriptfromtests.migrations.scriptsimportscriptrevision='1'down_revision=None@phaseddefupgrade():op.create_table('account',Column('id',INTEGER,primary_key=True),Column('name',VARCHAR(50),nullable=False),Column('description',NVARCHAR(200)),Column('timestamp',TIMESTAMP,server_default=func.now()))yieldop.execute("update account set name='some'")yieldexecute_script(script.__file__)defdowngrade():passWill give the sql output (for sqlite):-- Running upgrade  -> 1-- PHASE::before-deploy::;CREATETABLEaccount(idINTEGERNOTNULL,nameVARCHAR(50)NOTNULL,descriptionNVARCHAR(200),timestampTIMESTAMPDEFAULTCURRENT_TIMESTAMP,PRIMARYKEY(id));-- PHASE::after-deploy::;updateaccountsetname='some';-- PHASE::final::;-- SCRIPT::scripts/script.py::;INSERTINTOalembic_version(version_num)VALUES('1');As you see, phases are rendered as SQL comments to divide migration steps, so those who execute migration
can see which phase’s step it is.
However, if migration procedure is highly customized, you can use alembic-offline API described below.get_migration_datareturns migration phases in special form so you can automate their execution.Arbitrary script as operationFor complex migrations, it’s not enough to execute sql, you might need some script to be executed instead.
For that, there’s special operation:fromalembic_offlineimportexecute_scriptdefupgrade():execute_script('scripts/script.py')If you’ll get migration sql, it will be rendered as SQL comment:-- SCRIPT::scripts/script.py::;For those who execute migrations it will be visible and they can execute the script manually.
However, if migration procedure is highly customized, you can use alembic-offline API described below.get_migration_datareturns script migration steps in special form so you can automate their execution.
For online mode, the script will be executed as subprocess via pythonsubprocessmodule.Get migration dataalembic-offline provides specialized API to get certain migration data as dictionary:fromalembic_offlineimportget_migration_datafromalemic.configimportConfigconfig=Config('path to alemic.ini')data=get_migration_data(config,'your-revision')assertdata=={'revision':'your-revision','phases':{'after-deploy':[{'type':'mysql','script':'alter table account add column name VARCHAR(255)'},{'type':'python','script':'from app.models import Session, Account; Session.add(Account()); Session.commit()','path':'scripts/my_script.py'},]}}get_migration_datarequires bothphasesanddefault-phaseconfiguration options to be set.default-phaseis needed to be able to get migration data even for simple migrations without phases.Get migration data in batchalembic-offline provides an API call to get migration data for all revisions:fromalembic_offlineimportget_migrations_datafromalemic.configimportConfigconfig=Config('path to alemic.ini')data=get_migrations_data(config)assertdata==[{'revision':'your-revision','phases':{'after-deploy':[{'type':'mysql','script':'alter table account add column name VARCHAR(255)'},{'type':'python','script':'from app.models import Session, Account; Session.add(Account()); Session.commit()','path':'scripts/my_script.py'},]}}]Command line utilitiesBecause with alembic revisions it’s sometimes hard to find which the correct down revision should be; especially
when there are multiple heads we added the alembic-offline graph command.The graph command will generate adot fileof
the revisions, this file can then be converted to an image for easy visualization.Usage:alembic-offline graph --filename revisions.dot --alembic-config path/to/alembic.iniThen if you havegraphvizinstalled you can run:dot -Tpng -o revisions.png revisions.dotTo generate a png image.ContactIf you have questions, bug reports, suggestions, etc. please create an issue on
theGitHub project page.LicenseThis software is licensed under theMIT licensePlease refer to thelicense file© 2015 Anatoly Bubenkov, Paylogic International and others.Changelog1.2.0add migration dependency tree generation command (hvdklauw)1.1.0add down_revision to migration data (bubenkoff)reverse migration order to simplify the application (bubenkoff)1.0.5correctly handle multi-phased migration data extraction (bubenkoff)1.0.4online script execution implemented (bubenkoff)get_migrations_dataAPI (bubenkoff)1.0.3Added arbitrary script operation (bubenkoff)Strict phases configuration assertions for phased migration decorator (bubenkoff)get_migration_dataAPI (bubenkoff)1.0.0Initial public release (bubenkoff)