Coverage for manila/db/migrations/alembic/versions/344c1ac4747f_add_share_instance_access_rules_status.py: 100%

36 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2026-02-18 22:19 +0000

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"""Remove access rules status and add access_rule_status to share_instance 

14model 

15 

16Revision ID: 344c1ac4747f 

17Revises: dda6de06349 

18Create Date: 2015-11-18 14:58:55.806396 

19 

20""" 

21 

22# revision identifiers, used by Alembic. 

23revision = '344c1ac4747f' 

24down_revision = 'dda6de06349' 

25 

26from alembic import op 

27from sqlalchemy import Column, String 

28 

29from manila.common import constants 

30from manila.db.migrations import utils 

31 

32 

33priorities = { 

34 'active': 0, 

35 'new': 1, 

36 'error': 2 

37} 

38 

39upgrade_data_mapping = { 

40 'active': 'active', 

41 'new': 'out_of_sync', 

42 'error': 'error', 

43} 

44 

45 

46def upgrade(): 

47 """Transform individual access rules states to 'access_rules_status'. 

48 

49 WARNING: This method performs lossy converting of existing data in DB. 

50 """ 

51 op.add_column( 

52 'share_instances', 

53 Column('access_rules_status', String(length=255)) 

54 ) 

55 

56 connection = op.get_bind() 

57 share_instances_table = utils.load_table('share_instances', connection) 

58 instance_access_table = utils.load_table('share_instance_access_map', 

59 connection) 

60 

61 # NOTE(u_glide): Data migrations shouldn't be performed on live clouds 

62 # because it will lead to unpredictable behaviour of running operations 

63 # like migration. 

64 instances_query = ( 

65 share_instances_table.select() 

66 .where(share_instances_table.c.status == constants.STATUS_AVAILABLE) 

67 .where(share_instances_table.c.deleted == 'False') 

68 ) 

69 

70 for instance in connection.execute(instances_query): 

71 

72 access_mappings_query = instance_access_table.select().where( 

73 instance_access_table.c.share_instance_id == 

74 instance._mapping['id'] 

75 ).where(instance_access_table.c.deleted == 'False') 

76 

77 status = constants.STATUS_ACTIVE 

78 

79 for access_rule in connection.execute(access_mappings_query): 

80 

81 if (access_rule._mapping['state'] == constants.STATUS_DELETING or 

82 access_rule._mapping['state'] not in priorities): 

83 continue 

84 

85 if priorities[access_rule._mapping['state']] > priorities[status]: 

86 status = access_rule._mapping['state'] 

87 

88 # pylint: disable=no-value-for-parameter 

89 op.execute( 

90 share_instances_table.update().where( 

91 share_instances_table.c.id == instance._mapping['id'] 

92 ).values({'access_rules_status': upgrade_data_mapping[status]}) 

93 ) 

94 

95 op.drop_column('share_instance_access_map', 'state') 

96 

97 

98def downgrade(): 

99 op.add_column( 

100 'share_instance_access_map', 

101 Column('state', String(length=255)) 

102 ) 

103 

104 connection = op.get_bind() 

105 share_instances_table = utils.load_table('share_instances', connection) 

106 instance_access_table = utils.load_table('share_instance_access_map', 

107 connection) 

108 

109 instances_query = ( 

110 share_instances_table.select() 

111 .where(share_instances_table.c.status == constants.STATUS_AVAILABLE) 

112 .where(share_instances_table.c.deleted == 'False') 

113 ) 

114 

115 for instance in connection.execute(instances_query): 

116 

117 # NOTE(u_glide): We cannot determine if a rule is applied or not in 

118 # Manila, so administrator should manually handle such access rules. 

119 if instance._mapping['access_rules_status'] == 'active': 

120 state = 'active' 

121 else: 

122 state = 'error' 

123 

124 # pylint: disable=no-value-for-parameter 

125 op.execute( 

126 instance_access_table.update().where( 

127 instance_access_table.c.share_instance_id == 

128 instance._mapping['id'] 

129 ).where(instance_access_table.c.deleted == 'False').values( 

130 {'state': state} 

131 ) 

132 ) 

133 

134 op.drop_column('share_instances', 'access_rules_status')