Debian Codegraph @

oslo_db/sqlalchemy/migration_cli/ext_alembic.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 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 # option should be used if script is not in default directory
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 # Although alembic supports relative upgrades and downgrades,
94 # get_revision always returns False for relative revisions.
95 # Since only alembic supports relative revisions, assume the
96 # revision belongs to this plugin.
97 if rev_id: # rev_id can be None, so the check is required
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