| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
from debtcollector import removals |
| 14 |
import sqlalchemy |
| 15 |
from stevedore import enabled |
| 16 |
|
| 17 |
from oslo_db import exception |
| 18 |
|
| 19 |
|
| 20 |
MIGRATION_NAMESPACE = 'oslo.db.migration' |
| 21 |
|
| 22 |
|
| 23 |
def check_plugin_enabled(ext): |
| 24 |
"""Used for EnabledExtensionManager.""" |
| 25 |
return ext.obj.enabled |
| 26 |
|
| 27 |
|
| 28 |
@removals.remove( |
| 29 |
message='Support for sqlalchemy-migrate and with it the migration manager ' |
| 30 |
'is deprecated for removal; consider migrating to and using alembic ' |
| 31 |
'directly', |
| 32 |
version='8.3.0' |
| 33 |
) |
| 34 |
class MigrationManager: |
| 35 |
|
| 36 |
def __init__(self, migration_config, engine=None): |
| 37 |
if engine is None: |
| 38 |
if migration_config.get('db_url'): |
| 39 |
engine = sqlalchemy.create_engine( |
| 40 |
migration_config['db_url'], |
| 41 |
poolclass=sqlalchemy.pool.NullPool, |
| 42 |
) |
| 43 |
else: |
| 44 |
raise ValueError('Either database url or engine' |
| 45 |
' must be provided.') |
| 46 |
|
| 47 |
self._manager = enabled.EnabledExtensionManager( |
| 48 |
MIGRATION_NAMESPACE, |
| 49 |
check_plugin_enabled, |
| 50 |
invoke_args=(engine, migration_config), |
| 51 |
invoke_on_load=True |
| 52 |
) |
| 53 |
if not self._plugins: |
| 54 |
raise ValueError('There must be at least one plugin active.') |
| 55 |
|
| 56 |
@property |
| 57 |
def _plugins(self): |
| 58 |
return sorted(ext.obj for ext in self._manager.extensions) |
| 59 |
|
| 60 |
def upgrade(self, revision): |
| 61 |
"""Upgrade database with all available backends.""" |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
rev_in_plugins = [p.has_revision(revision) for p in self._plugins] |
| 66 |
if not any(rev_in_plugins) and revision is not None: |
| 67 |
raise exception.DBMigrationError('Revision does not exist') |
| 68 |
|
| 69 |
results = [] |
| 70 |
for plugin, has_revision in zip(self._plugins, rev_in_plugins): |
| 71 |
if not has_revision or revision is None: |
| 72 |
results.append(plugin.upgrade(None)) |
| 73 |
else: |
| 74 |
results.append(plugin.upgrade(revision)) |
| 75 |
break |
| 76 |
return results |
| 77 |
|
| 78 |
def downgrade(self, revision): |
| 79 |
"""Downgrade database with available backends.""" |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
rev_in_plugins = [p.has_revision(revision) for p in self._plugins] |
| 84 |
if not any(rev_in_plugins) and revision is not None: |
| 85 |
raise exception.DBMigrationError('Revision does not exist') |
| 86 |
|
| 87 |
|
| 88 |
results = [] |
| 89 |
for plugin, has_revision in zip(reversed(self._plugins), |
| 90 |
reversed(rev_in_plugins)): |
| 91 |
if not has_revision or revision is None: |
| 92 |
results.append(plugin.downgrade(None)) |
| 93 |
else: |
| 94 |
results.append(plugin.downgrade(revision)) |
| 95 |
break |
| 96 |
return results |
| 97 |
|
| 98 |
def version(self): |
| 99 |
"""Return last version of db.""" |
| 100 |
last = None |
| 101 |
for plugin in self._plugins: |
| 102 |
version = plugin.version() |
| 103 |
if version is not None: |
| 104 |
last = version |
| 105 |
return last |
| 106 |
|
| 107 |
def revision(self, message, autogenerate): |
| 108 |
"""Generate template or autogenerated revision.""" |
| 109 |
|
| 110 |
return self._plugins[-1].revision(message, autogenerate) |
| 111 |
|
| 112 |
def stamp(self, revision): |
| 113 |
"""Create stamp for a given revision.""" |
| 114 |
return self._plugins[-1].stamp(revision) |