Coverage for manila/db/migrations/alembic/versions/579c267fbb4d_add_share_instances_access_map.py: 100%

28 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"""add_share_instances_access_map 

14 

15Revision ID: 579c267fbb4d 

16Revises: 5077ffcc5f1c 

17Create Date: 2015-08-19 07:51:52.928542 

18 

19""" 

20 

21# revision identifiers, used by Alembic. 

22revision = '579c267fbb4d' 

23down_revision = '5077ffcc5f1c' 

24 

25from alembic import op 

26from sqlalchemy import Column, DateTime, ForeignKey, String 

27from oslo_utils import uuidutils 

28 

29from manila.db.migrations import utils 

30 

31 

32def upgrade(): 

33 """Create 'share_instance_access_map' table and move 'state' column.""" 

34 

35 instance_access_table = op.create_table( 

36 'share_instance_access_map', 

37 Column('created_at', DateTime), 

38 Column('updated_at', DateTime), 

39 Column('deleted_at', DateTime), 

40 Column('deleted', String(length=36), default='False'), 

41 Column('id', String(length=36), primary_key=True, nullable=False), 

42 Column('share_instance_id', String(length=36), 

43 ForeignKey('share_instances.id', name="siam_instance_fk")), 

44 Column('access_id', String(length=36), 

45 ForeignKey('share_access_map.id', name="siam_access_fk")), 

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

47 mysql_engine='InnoDB', 

48 mysql_charset='utf8') 

49 

50 # NOTE(u_glide): Move all states from 'share_access_map' 

51 # to 'share_instance_access_map' 

52 instance_access_mappings = [] 

53 connection = op.get_bind() 

54 access_table = utils.load_table('share_access_map', connection) 

55 instances_table = utils.load_table('share_instances', connection) 

56 

57 for access_rule in connection.execute(access_table.select()): 

58 # pylint: disable=assignment-from-no-return 

59 instances_query = instances_table.select().where( 

60 instances_table.c.share_id == access_rule.share_id 

61 ) 

62 

63 for instance in connection.execute(instances_query): 

64 instance_access_mappings.append({ 

65 'created_at': access_rule.created_at, 

66 'updated_at': access_rule.updated_at, 

67 'deleted_at': access_rule.deleted_at, 

68 'deleted': access_rule.deleted, 

69 'id': uuidutils.generate_uuid(), 

70 'share_instance_id': instance.id, 

71 'access_id': access_rule.id, 

72 'state': access_rule.state, 

73 }) 

74 op.bulk_insert(instance_access_table, instance_access_mappings) 

75 op.drop_column('share_access_map', 'state') 

76 

77 

78def downgrade(): 

79 """Remove 'share_instance_access_map' table and add 'state' column back. 

80 

81 This method can lead to data loss because only first state is saved in 

82 share_access_map table. 

83 """ 

84 op.add_column('share_access_map', Column('state', String(length=255))) 

85 

86 # NOTE(u_glide): Move all states from 'share_instance_access_map' 

87 # to 'share_access_map' 

88 connection = op.get_bind() 

89 access_table = utils.load_table('share_access_map', connection) 

90 instance_access_table = utils.load_table('share_instance_access_map', 

91 connection) 

92 

93 share_access_rules = connection.execute( 

94 access_table.select().where(access_table.c.deleted == "False")) 

95 

96 for access_rule in share_access_rules: 

97 access_mapping = connection.execute( 

98 instance_access_table.select().where( 

99 instance_access_table.c.access_id == 

100 access_rule._mapping['id']) 

101 ).first() 

102 

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

104 op.execute( 

105 access_table.update().where( 

106 access_table.c.id == access_rule._mapping['id'] 

107 ).values({'state': access_mapping._mapping['state']}) 

108 ) 

109 

110 op.drop_table('share_instance_access_map')