This is a pre-production deployment of Warehouse, however changes made here WILL affect the production instance of PyPI.
Latest Version Dependencies status unknown Test status unknown Test coverage unknown
Project Description

alembic-offline

alembic-offline is an extension for alemic to enrich offline functionality of the migrations

Usage

Phased migrations

alembic-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-deploy

In your version file:

from sqlalchemy import INTEGER, VARCHAR, NVARCHAR, TIMESTAMP, Column, func
from alembic import op

from alembic_offline import phased, execute_script

from tests.migrations.scripts import script

revision = '1'
down_revision = None


@phased
def upgrade():

    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())
    )
    yield
    op.execute("update account set name='some'")
    yield
    execute_script(script.__file__)


def downgrade():
    pass

Will give the sql output (for sqlite):

-- Running upgrade  -> 1

-- PHASE::before-deploy::;

CREATE TABLE account (
    id INTEGER NOT NULL,
    name VARCHAR(50) NOT NULL,
    description NVARCHAR(200),
    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id)
);

-- PHASE::after-deploy::;

update account set name='some';

-- PHASE::final::;

-- SCRIPT::scripts/script.py::;

INSERT INTO alembic_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_data returns migration phases in special form so you can automate their execution.

Arbitrary script as operation

For complex migrations, it’s not enough to execute sql, you might need some script to be executed instead. For that, there’s special operation:

from alembic_offline import execute_script

def upgrade():
    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_data returns script migration steps in special form so you can automate their execution. For online mode, the script will be executed as subprocess via python subprocess module.

Get migration data

alembic-offline provides specialized API to get certain migration data as dictionary:

from alembic_offline import get_migration_data

from alemic.config import Config

config = Config('path to alemic.ini')

data = get_migration_data(config, 'your-revision')

assert data == {
    '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_data requires both phases and default-phase configuration options to be set. default-phase is needed to be able to get migration data even for simple migrations without phases.

Get migration data in batch

alembic-offline provides an API call to get migration data for all revisions:

from alembic_offline import get_migrations_data

from alemic.config import Config

config = Config('path to alemic.ini')

data = get_migrations_data(config)

assert data == [
    {
        '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 utilities

Because 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 a dot file of 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.ini

Then if you have graphviz installed you can run:

dot -Tpng -o revisions.png revisions.dot

To generate a png image.

Contact

If you have questions, bug reports, suggestions, etc. please create an issue on the GitHub project page.

License

This software is licensed under the MIT license

Please refer to the license file

© 2015 Anatoly Bubenkov, Paylogic International and others.

Changelog

1.2.0

  • add migration dependency tree generation command (hvdklauw)

1.1.0

  • add down_revision to migration data (bubenkoff)
  • reverse migration order to simplify the application (bubenkoff)

1.0.5

  • correctly handle multi-phased migration data extraction (bubenkoff)

1.0.4

  • online script execution implemented (bubenkoff)
  • get_migrations_data API (bubenkoff)

1.0.3

  • Added arbitrary script operation (bubenkoff)
  • Strict phases configuration assertions for phased migration decorator (bubenkoff)
  • get_migration_data API (bubenkoff)

1.0.0

  • Initial public release (bubenkoff)
Release History

Release History

1.2.0

This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

1.1.0

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

1.0.5

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

1.0.4

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

1.0.3

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

1.0.2

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

1.0.1

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

1.0.0

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

TODO: Brief introduction on what you do with files - including link to relevant help section.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
alembic-offline-1.2.0.tar.gz (8.1 kB) Copy SHA256 Checksum SHA256 Source Aug 28, 2015

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS HPE HPE Development Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting