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

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. 

17 

18""" 

19Run storage database migration. 

20""" 

21 

22import sys 

23 

24from oslo_config import cfg 

25 

26from watcher.common import service 

27from watcher import conf 

28from watcher.db import migration 

29from watcher.db import purge 

30 

31CONF = conf.CONF 

32 

33 

34class DBCommand(object): 

35 

36 @staticmethod 

37 def upgrade(): 

38 migration.upgrade(CONF.command.revision) 

39 

40 @staticmethod 

41 def downgrade(): 

42 migration.downgrade(CONF.command.revision) 

43 

44 @staticmethod 

45 def revision(): 

46 migration.revision(CONF.command.message, CONF.command.autogenerate) 

47 

48 @staticmethod 

49 def stamp(): 

50 migration.stamp(CONF.command.revision) 

51 

52 @staticmethod 

53 def version(): 

54 print(migration.version()) 

55 

56 @staticmethod 

57 def create_schema(): 

58 migration.create_schema() 

59 

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) 

65 

66 

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='?') 

75 

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='?') 

83 

84 parser = subparsers.add_parser('stamp') 

85 parser.add_argument('revision', nargs='?') 

86 parser.set_defaults(func=DBCommand.stamp) 

87 

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) 

95 

96 parser = subparsers.add_parser( 

97 'version', 

98 help="Print the current version information and exit.") 

99 parser.set_defaults(func=DBCommand.version) 

100 

101 parser = subparsers.add_parser( 

102 'create_schema', 

103 help="Create the database schema.") 

104 parser.set_defaults(func=DBCommand.create_schema) 

105 

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) 

130 

131 parser.set_defaults(func=DBCommand.purge) 

132 

133 

134command_opt = cfg.SubCommandOpt('command', 

135 title='Command', 

136 help='Available commands', 

137 handler=add_command_parsers) 

138 

139 

140def register_sub_command_opts(): 

141 cfg.CONF.register_cli_opt(command_opt) 

142 

143 

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') 

155 

156 service.prepare_service(sys.argv, CONF) 

157 CONF.command.func()