Coverage for manila/db/migrations/alembic/versions/dda6de06349_add_export_locations_metadata.py: 71%

39 statements  

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

1# Copyright 2015 Mirantis Inc. 

2# All Rights Reserved. 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); you may 

5# not use this file except in compliance with the License. You may obtain 

6# a copy of the License at 

7# 

8# http://www.apache.org/licenses/LICENSE-2.0 

9# 

10# Unless required by applicable law or agreed to in writing, software 

11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 

12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 

13# License for the specific language governing permissions and limitations 

14# under the License. 

15 

16"""Add DB support for share instance export locations metadata. 

17 

18Revision ID: dda6de06349 

19Revises: 323840a08dc4 

20Create Date: 2015-11-30 13:50:15.914232 

21 

22""" 

23 

24# revision identifiers, used by Alembic. 

25revision = 'dda6de06349' 

26down_revision = '323840a08dc4' 

27 

28from alembic import op 

29from oslo_log import log 

30from oslo_utils import uuidutils 

31import sqlalchemy as sa 

32 

33 

34SI_TABLE_NAME = 'share_instances' 

35EL_TABLE_NAME = 'share_instance_export_locations' 

36ELM_TABLE_NAME = 'share_instance_export_locations_metadata' 

37LOG = log.getLogger(__name__) 

38 

39 

40def upgrade(): 

41 try: 

42 meta = sa.MetaData() 

43 

44 # Add new 'is_admin_only' column in export locations table that will be 

45 # used for hiding admin export locations from common users in API. 

46 op.add_column( 

47 EL_TABLE_NAME, 

48 sa.Column('is_admin_only', sa.Boolean, default=False)) 

49 

50 # Create new 'uuid' column as String(36) in export locations table 

51 # that will be used for API. 

52 op.add_column( 

53 EL_TABLE_NAME, 

54 sa.Column('uuid', sa.String(36), unique=True), 

55 ) 

56 

57 # Generate UUID for each existing export location. 

58 el_table = sa.Table( 

59 EL_TABLE_NAME, meta, 

60 sa.Column('id', sa.Integer), 

61 sa.Column('uuid', sa.String(36)), 

62 sa.Column('is_admin_only', sa.Boolean), 

63 ) 

64 for record in op.get_bind().execute(el_table.select()): 

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

66 op.get_bind().execute(el_table.update().values( 

67 is_admin_only=False, 

68 uuid=uuidutils.generate_uuid(), 

69 ).where( 

70 el_table.c.id == record.id, 

71 )) 

72 

73 # Make new 'uuid' column in export locations table not nullable. 

74 op.alter_column( 

75 EL_TABLE_NAME, 

76 'uuid', 

77 existing_type=sa.String(length=36), 

78 nullable=False, 

79 ) 

80 except Exception: 

81 LOG.error("Failed to update '%s' table!", 

82 EL_TABLE_NAME) 

83 raise 

84 

85 try: 

86 op.create_table( 

87 ELM_TABLE_NAME, 

88 sa.Column('id', sa.Integer, primary_key=True), 

89 sa.Column('created_at', sa.DateTime), 

90 sa.Column('updated_at', sa.DateTime), 

91 sa.Column('deleted_at', sa.DateTime), 

92 sa.Column('deleted', sa.Integer), 

93 sa.Column('export_location_id', sa.Integer, 

94 sa.ForeignKey('%s.id' % EL_TABLE_NAME, 

95 name="elm_id_fk"), nullable=False), 

96 sa.Column('key', sa.String(length=255), nullable=False), 

97 sa.Column('value', sa.String(length=1023), nullable=False), 

98 sa.UniqueConstraint('export_location_id', 'key', 'deleted', 

99 name="elm_el_id_uc"), 

100 mysql_engine='InnoDB', 

101 mysql_charset='utf8', 

102 ) 

103 except Exception: 

104 LOG.error("Failed to create '%s' table!", ELM_TABLE_NAME) 

105 raise 

106 

107 

108def downgrade(): 

109 try: 

110 op.drop_table(ELM_TABLE_NAME) 

111 except Exception: 

112 LOG.error("Failed to drop '%s' table!", ELM_TABLE_NAME) 

113 raise 

114 

115 try: 

116 op.drop_column(EL_TABLE_NAME, 'is_admin_only') 

117 op.drop_column(EL_TABLE_NAME, 'uuid') 

118 except Exception: 

119 LOG.error("Failed to update '%s' table!", EL_TABLE_NAME) 

120 raise