Coverage for watcher/cmd/dbmanage.py: 61%
64 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# -*- encoding: utf-8 -*-
2#
3# Copyright 2013 Hewlett-Packard Development Company, L.P.
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
18"""
19Run storage database migration.
20"""
22import sys
24from oslo_config import cfg
26from watcher.common import service
27from watcher import conf
28from watcher.db import migration
29from watcher.db import purge
31CONF = conf.CONF
34class DBCommand(object):
36 @staticmethod
37 def upgrade():
38 migration.upgrade(CONF.command.revision)
40 @staticmethod
41 def downgrade():
42 migration.downgrade(CONF.command.revision)
44 @staticmethod
45 def revision():
46 migration.revision(CONF.command.message, CONF.command.autogenerate)
48 @staticmethod
49 def stamp():
50 migration.stamp(CONF.command.revision)
52 @staticmethod
53 def version():
54 print(migration.version())
56 @staticmethod
57 def create_schema():
58 migration.create_schema()
60 @staticmethod
61 def purge():
62 purge.purge(CONF.command.age_in_days, CONF.command.max_number,
63 CONF.command.goal, CONF.command.exclude_orphans,
64 CONF.command.dry_run)
67def add_command_parsers(subparsers):
68 parser = subparsers.add_parser(
69 'upgrade',
70 help="Upgrade the database schema to the latest version. "
71 "Optionally, use --revision to specify an alembic revision "
72 "string to upgrade to.")
73 parser.set_defaults(func=DBCommand.upgrade)
74 parser.add_argument('--revision', nargs='?')
76 parser = subparsers.add_parser(
77 'downgrade',
78 help="Downgrade the database schema to the oldest revision. "
79 "While optional, one should generally use --revision to "
80 "specify the alembic revision string to downgrade to.")
81 parser.set_defaults(func=DBCommand.downgrade)
82 parser.add_argument('--revision', nargs='?')
84 parser = subparsers.add_parser('stamp')
85 parser.add_argument('revision', nargs='?')
86 parser.set_defaults(func=DBCommand.stamp)
88 parser = subparsers.add_parser(
89 'revision',
90 help="Create a new alembic revision. "
91 "Use --message to set the message string.")
92 parser.add_argument('-m', '--message')
93 parser.add_argument('--autogenerate', action='store_true')
94 parser.set_defaults(func=DBCommand.revision)
96 parser = subparsers.add_parser(
97 'version',
98 help="Print the current version information and exit.")
99 parser.set_defaults(func=DBCommand.version)
101 parser = subparsers.add_parser(
102 'create_schema',
103 help="Create the database schema.")
104 parser.set_defaults(func=DBCommand.create_schema)
106 parser = subparsers.add_parser(
107 'purge',
108 help="Purge the database.")
109 parser.add_argument('-d', '--age-in-days',
110 help="Number of days since deletion (from today) "
111 "to exclude from the purge. If None, everything "
112 "will be purged.",
113 type=int, default=None, nargs='?')
114 parser.add_argument('-n', '--max-number',
115 help="Max number of objects expected to be deleted. "
116 "Prevents the deletion if exceeded. No limit if "
117 "set to None.",
118 type=int, default=None, nargs='?')
119 parser.add_argument('-t', '--goal',
120 help="UUID or name of the goal to purge.",
121 type=str, default=None, nargs='?')
122 parser.add_argument('-e', '--exclude-orphans', action='store_true',
123 help="Flag to indicate whether or not you want to "
124 "exclude orphans from deletion (default: False).",
125 default=False)
126 parser.add_argument('--dry-run', action='store_true',
127 help="Flag to indicate whether or not you want to "
128 "perform a dry run (no deletion).",
129 default=False)
131 parser.set_defaults(func=DBCommand.purge)
134command_opt = cfg.SubCommandOpt('command',
135 title='Command',
136 help='Available commands',
137 handler=add_command_parsers)
140def register_sub_command_opts():
141 cfg.CONF.register_cli_opt(command_opt)
144def main():
145 register_sub_command_opts()
146 # this is hack to work with previous usage of watcher-dbsync
147 # pls change it to watcher-dbsync upgrade
148 valid_commands = set([
149 'upgrade', 'downgrade', 'revision',
150 'version', 'stamp', 'create_schema',
151 'purge',
152 ])
153 if not set(sys.argv).intersection(valid_commands):
154 sys.argv.append('upgrade')
156 service.prepare_service(sys.argv, CONF)
157 CONF.command.func()