Coverage for manila/db/migrations/alembic/versions/2d708a9a3ba9_backup_change_az_to_az_id.py: 95%

31 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"""backup_change_availability_zone_to_availability_zone_id 

14 

15Revision ID: 2d708a9a3ba9 

16Revises: cb20f743ca7b 

17Create Date: 2023-08-24 11:01:41.134456 

18 

19""" 

20 

21# revision identifiers, used by Alembic. 

22revision = '2d708a9a3ba9' 

23down_revision = 'cb20f743ca7b' 

24 

25from alembic import op 

26from sqlalchemy import Column, ForeignKey, String 

27 

28from manila.db.migrations import utils 

29 

30 

31def collect_existing_az(az_table, connection): 

32 az_name_to_id_mapping = dict() 

33 for az in connection.execute(az_table.select()): 

34 if az.name in az_name_to_id_mapping: 34 ↛ 35line 34 didn't jump to line 35 because the condition on line 34 was never true

35 continue 

36 

37 az_name_to_id_mapping[az.name] = az.id 

38 return az_name_to_id_mapping 

39 

40 

41def upgrade(): 

42 connection = op.get_bind() 

43 

44 op.add_column( 

45 'share_backups', 

46 Column('availability_zone_id', String(36), 

47 ForeignKey('availability_zones.id', name='sb_az_id_fk')) 

48 ) 

49 

50 # Collect existing AZs from availability_zones table 

51 availability_zones_table = utils.load_table( 

52 'availability_zones', connection) 

53 az_name_to_id_mapping = collect_existing_az( 

54 availability_zones_table, connection,) 

55 

56 # Map string AZ names to ID's in target table 

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

58 set_az_id_in_table = lambda table, id, name: ( # noqa: E731 

59 op.execute( 

60 table.update().where(table.c.availability_zone == name).values( 

61 {'availability_zone_id': id}) 

62 ) 

63 ) 

64 

65 share_backups_table = utils.load_table('share_backups', connection) 

66 for name, id in az_name_to_id_mapping.items(): 

67 set_az_id_in_table(share_backups_table, id, name) 

68 

69 # Remove old AZ columns from table 

70 op.drop_column('share_backups', 'availability_zone') 

71 

72 

73def downgrade(): 

74 connection = op.get_bind() 

75 

76 # Create old AZ fields 

77 op.add_column('share_backups', 

78 Column('availability_zone', String(length=255))) 

79 

80 # Migrate data 

81 az_table = utils.load_table('availability_zones', connection) 

82 share_backups_table = utils.load_table('share_backups', connection) 

83 

84 for az in connection.execute(az_table.select()): 

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

86 op.execute( 

87 share_backups_table.update().where( 

88 share_backups_table.c.availability_zone_id == az.id 

89 ).values({'availability_zone': az.name}) 

90 ) 

91 

92 # Remove AZ_id columns and AZ table 

93 op.drop_constraint('sb_az_id_fk', 'share_backups', type_='foreignkey') 

94 op.drop_column('share_backups', 'availability_zone_id')