Coverage for watcher/db/sqlalchemy/migration.py: 73%
39 statements
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-17 12:22 +0000
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-17 12:22 +0000
1# Copyright 2010 United States Government as represented by the
2# Administrator of the National Aeronautics and Space Administration.
3# All Rights Reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
17import os
19import alembic
20from alembic import config as alembic_config
21import alembic.migration as alembic_migration
22from oslo_db import exception as db_exc
23from oslo_db.sqlalchemy import enginefacade
25from watcher._i18n import _
26from watcher.db.sqlalchemy import models
29def _alembic_config():
30 path = os.path.join(os.path.dirname(__file__), 'alembic.ini')
31 config = alembic_config.Config(path)
32 return config
35def version(engine=None):
36 """Current database version.
38 :returns: Database version
39 :rtype: string
40 """
41 if engine is None:
42 engine = enginefacade.reader.get_engine()
43 with engine.connect() as conn:
44 context = alembic_migration.MigrationContext.configure(conn)
45 return context.get_current_revision()
48def upgrade(revision, config=None):
49 """Used for upgrading database.
51 :param version: Desired database version
52 :type version: string
53 """
54 revision = revision or 'head'
55 config = config or _alembic_config()
57 alembic.command.upgrade(config, revision)
60def create_schema(config=None, engine=None):
61 """Create database schema from models description.
63 Can be used for initial installation instead of upgrade('head').
64 """
65 if engine is None: 65 ↛ 71line 65 didn't jump to line 71 because the condition on line 65 was always true
66 engine = enginefacade.writer.get_engine()
68 # NOTE(viktors): If we will use metadata.create_all() for non empty db
69 # schema, it will only add the new tables, but leave
70 # existing as is. So we should avoid of this situation.
71 if version(engine=engine) is not None: 71 ↛ anywhereline 71 didn't jump anywhere: it always raised an exception.
72 raise db_exc.DBMigrationError(
73 _("Watcher database schema is already under version control; "
74 "use upgrade() instead"))
76 models.Base.metadata.create_all(engine)
77 stamp('head', config=config)
80def downgrade(revision, config=None):
81 """Used for downgrading database.
83 :param version: Desired database version
84 :type version: string
85 """
86 revision = revision or 'base'
87 config = config or _alembic_config()
88 return alembic.command.downgrade(config, revision)
91def stamp(revision, config=None):
92 """Stamps database with provided revision.
94 Don't run any migrations.
96 :param revision: Should match one from repository or head - to stamp
97 database with most recent revision
98 :type revision: string
99 """
100 config = config or _alembic_config()
101 return alembic.command.stamp(config, revision=revision)
104def revision(message=None, autogenerate=False, config=None):
105 """Creates template for migration.
107 :param message: Text that will be used for migration title
108 :type message: string
109 :param autogenerate: If True - generates diff based on current database
110 state
111 :type autogenerate: bool
112 """
113 config = config or _alembic_config()
114 return alembic.command.revision(config, message=message,
115 autogenerate=autogenerate)