| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
import os |
| 14 |
|
| 15 |
import alembic |
| 16 |
from alembic import config as alembic_config |
| 17 |
import alembic.migration as alembic_migration |
| 18 |
from alembic import script as alembic_script |
| 19 |
|
| 20 |
from oslo_db.sqlalchemy.migration_cli import ext_base |
| 21 |
|
| 22 |
|
| 23 |
class AlembicExtension(ext_base.MigrationExtensionBase): |
| 24 |
"""Extension to provide alembic features. |
| 25 |
|
| 26 |
:param engine: SQLAlchemy engine instance for a given database |
| 27 |
:type engine: sqlalchemy.engine.Engine |
| 28 |
:param migration_config: Stores specific configuration for migrations |
| 29 |
:type migration_config: dict |
| 30 |
""" |
| 31 |
|
| 32 |
order = 2 |
| 33 |
|
| 34 |
@property |
| 35 |
def enabled(self): |
| 36 |
return os.path.exists(self.alembic_ini_path) |
| 37 |
|
| 38 |
def __init__(self, engine, migration_config): |
| 39 |
self.alembic_ini_path = migration_config.get('alembic_ini_path', '') |
| 40 |
self.config = alembic_config.Config(self.alembic_ini_path) |
| 41 |
|
| 42 |
repo_path = migration_config.get('alembic_repo_path') |
| 43 |
if repo_path: |
| 44 |
self.config.set_main_option('script_location', repo_path) |
| 45 |
self.engine = engine |
| 46 |
|
| 47 |
def upgrade(self, version): |
| 48 |
with self.engine.begin() as connection: |
| 49 |
self.config.attributes['connection'] = connection |
| 50 |
return alembic.command.upgrade(self.config, version or 'head') |
| 51 |
|
| 52 |
def downgrade(self, version): |
| 53 |
if isinstance(version, int) or version is None or version.isdigit(): |
| 54 |
version = 'base' |
| 55 |
with self.engine.begin() as connection: |
| 56 |
self.config.attributes['connection'] = connection |
| 57 |
return alembic.command.downgrade(self.config, version) |
| 58 |
|
| 59 |
def version(self): |
| 60 |
with self.engine.connect() as conn: |
| 61 |
context = alembic_migration.MigrationContext.configure(conn) |
| 62 |
return context.get_current_revision() |
| 63 |
|
| 64 |
def revision(self, message='', autogenerate=False): |
| 65 |
"""Creates template for migration. |
| 66 |
|
| 67 |
:param message: Text that will be used for migration title |
| 68 |
:type message: string |
| 69 |
:param autogenerate: If True - generates diff based on current database |
| 70 |
state |
| 71 |
:type autogenerate: bool |
| 72 |
""" |
| 73 |
with self.engine.begin() as connection: |
| 74 |
self.config.attributes['connection'] = connection |
| 75 |
return alembic.command.revision(self.config, message=message, |
| 76 |
autogenerate=autogenerate) |
| 77 |
|
| 78 |
def stamp(self, revision): |
| 79 |
"""Stamps database with provided revision. |
| 80 |
|
| 81 |
:param revision: Should match one from repository or head - to stamp |
| 82 |
database with most recent revision |
| 83 |
:type revision: string |
| 84 |
""" |
| 85 |
with self.engine.begin() as connection: |
| 86 |
self.config.attributes['connection'] = connection |
| 87 |
return alembic.command.stamp(self.config, revision=revision) |
| 88 |
|
| 89 |
def has_revision(self, rev_id): |
| 90 |
if rev_id in ['base', 'head']: |
| 91 |
return True |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
if rev_id: |
| 98 |
if '-' in rev_id or '+' in rev_id: |
| 99 |
return True |
| 100 |
|
| 101 |
script = alembic_script.ScriptDirectory( |
| 102 |
self.config.get_main_option('script_location')) |
| 103 |
try: |
| 104 |
script.get_revision(rev_id) |
| 105 |
return True |
| 106 |
except alembic.util.CommandError: |
| 107 |
return False |