Debian Codegraph @

oslo_db/sqlalchemy/migration_cli/manager.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
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 # a revision exists only in a single plugin. Until we reached it, we
63 # should upgrade to the plugins' heads.
64 # revision=None is a special case meaning latest revision.
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 # a revision exists only in a single plugin. Until we reached it, we
81 # should upgrade to the plugins' first revision.
82 # revision=None is a special case meaning initial revision.
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 # downgrading should be performed in reversed order
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 # revision should be done only by last plugin
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)